简体   繁体   中英

c# sleeping thread problem

what is the problem in the code part below? Any ideas? I m sending command to my device through serial port. After each command the device will work for this command and then the other command comes for it and continues like this.

in Button Click event

{
  function1();
  Thread.Sleep(5000);
  function2();
  Thread.Sleep(5000);
  function3();
}

I figured out if i erase second sleep and function3 from the code like below, it does both two function but if i want to continue like this way it does not do the third one.

in Button Click event

{
  function1();
  Thread.Sleep(5000);
  function2();
}

works... Thank you

You're blocking the UI thread. Don't do that. It means your UI can't update itself. Instead, set a System.Windows.Forms.Timer to fire in 5 seconds with the next function to call. Alternatively, do all of this in a different thread entirely (possibly using Sleep , possibly using another kind of timer to fire on a thread-pool thread) and use Control.Invoke/BeginInvoke to marshall back to the UI thread when you need to update the UI itself.

EDIT: Given your "answer", it seems that blocking the UI thread was only one of the problems - and getting the device to respond properly at all is a bigger problem. You shouldn't just rely on sleeping for a certain amount of time. You should detect when the device has completed the previous command. It's possible that it doesn't give any feedback, but that would be horrifically poor design. If at all possible, investigate how to read feedback from the device as to when it's finished (eg reading from the serial port!) and only start the next command when the previous one has finished. Depending on how you receive the feedback, you could use a blocking call on a non-UI thread, or use an asynchronous model to trigger things.

As the code is presented there it is nothing wrong with it. It will:

  • Execute function 1
  • Sleep 5 seconds
  • Execute function 2
  • Sleep 5 seconds
  • Execute function 3

However since this is on a GUI event it will freeze the application while doing so. Consider spinning off the execution into a thread instead.

In .Net 4.0:

Task.Factory.StartNew(() => sendData());

In all .Net versions:

System.Threading.Thread myThread = new System.Threading.Thread(sendData);
myThread.IsBackground = true;
myThread.Start();

And then you have your sendData method:

private void sendData()
{
  function1();
  Thread.Sleep(5000);
  function2();
  Thread.Sleep(5000);
  function3();
}

If you really need to do stuff in the GUI thread you can make it more responsive by regularly calling Application.DoEvents(); , but this is not a good way of solving it.

Also remember that you can't access the GUI from other threads. See http://kristofverbiest.blogspot.com/2007/02/simple-pattern-to-invoke-gui-from.html for sample code on how to invoke the GUI thread from other threads.

The BackgroundWorker might be a solution to solve the blocking of the UI.

Get rid of the Sleeps If the functions are creating their own threads, give them callback methods that trigger the next function after the first has finished.

Thank you guys. I solve it. The problem is i did not make thread sleep enough. 5000 ms do not enough for the second command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM