-1

I need to wait 2 seconds for a serial port to respond, then return a value. What is the best way to accomplish that without locking the UI thread?

I have a class called Test and I create an instance of it in the main form of my program. I have several event handlers to pass data back to the main form and update the UI. My Test class has a method called runTest that has two loops. The first loop calls conTest. When that is done the second runs and calls hiPotTest. In conTest and HiPotTest I send the command via serial port to start the test. What I want to do is wait 2 seconds (for example) and get the result and return that from conTest and hiPotTest. My first thought was thread.sleep but then I decided that that would probably lock the UI during that time. Is that correct and if so what's the best way to avoid this.

I can't test this part of my program until monday at work. So right now I just have a guess as to what will happen.

TheColonel26
  • 2,618
  • 7
  • 25
  • 50
  • You need to use some sort of timer to wait 2 seconds, then fetch the result, then post an event to the GUI thread to display the serial port data. – millimoose Mar 30 '13 at 01:42
  • I already have event handlers to notify and update the GUI form of things happening in the Test class. – TheColonel26 Mar 30 '13 at 02:21

1 Answers1

4

Having proccess go to sleep for a few seconds should be setting alarm bells off that something is wrong. While it might work now this is the type of code that will cause prod issues down then line. What if the result isn't there after 2 seconds. You'd be better off having your thread notified when the result arrives, and you have the benifit that the UI can respond straight away if the result arrives < 2 seconds.

Having said that if you really want to do it you can spwan a background thread/ timer (system.timers.timer) and have your method sleep on that. But please don't :)

Jizzle
  • 214
  • 1
  • 9
  • See that is why I was asking in the first place, I don't know anything about multithreading. How would I accomplish notifying my thread when the results were there? Have a timer send a query to the serial port every 10th of a second? That just seems EXCESSIVE. – TheColonel26 Mar 30 '13 at 02:21
  • Since I tell the device via the serial port to test for 2 seconds, why wouldn't I assume it will have a result for me after 2 seconds? The only reason I can think of that this wouldn't be the case is if there was a hardware failure in the device. – TheColonel26 Mar 30 '13 at 02:30
  • @TheColonel26 The process scheduler alone runs probably hundreds of times per second. Doing something simple ten times per second is *nothing*. (It probably doesn't even matter if it's "slow", because the delay would be waiting on IO from a device used by no other program, so it's not like it's taking any useful resources away.) – millimoose Mar 30 '13 at 03:20
  • @TheColenel26 Take a look at http://stackoverflow.com/questions/1584062/how-to-wait-for-thread-to-finish-with-net – Jizzle Mar 30 '13 at 03:56
  • hmm but here's the thing though. As far as I can tell the device doesn't return anything until I send it a query. So are you saying I should continuously send it a query asking for a result while the test is running? not sure how it would like that. Sorry I'm not trying to be difficult, just trying to understand. – TheColonel26 Mar 30 '13 at 18:39
  • No problem at all. Can you give some example code with imports so we can take a look at your options. – Jizzle Mar 30 '13 at 20:32
  • Just an FYI I took your suggestion and did not use sleep. I wrote my code to query the device every 10th of a second instead of using sleep. my test now run in it's own thread. – TheColonel26 Jul 25 '13 at 18:41