views:

183

answers:

5

I am working on a project in C#.NET using the .NET framework version 3.5.

My project has a class called Focuser.cs which represents a physical device, a telescope focuser, that can communicate with a PC via a serial (RS-232) port. My class (Focuser) has properties such as CurrentPosition, CurrentTemperature, ect which represents the current conditions of the focuser which can change at any time. So, my Focuser class needs to continually poll the device for these values and update its internal fields. My question is, what is the best way to perform this continual polling sequence? Occasionally, the user will need to switch the device into a different mode which will require the ability to stop the polling, perform some action, and then resume polling.

My first attempt was to use a time that ticks every 500ms and then calls up a background worker which polls for one position and one temperature then returns. When the timer ticks if the background worker isBusy then it just returns and tries again 500ms later. Someone suggested that I get rid of the background worker all together and just do the poll in the timer tick event. So I set the AutoReset property of the timer to false and then just restart the timer every time a poll finishes. These two techniques seemed to behave the exact same way in my application so I am not sure if one is better than the other. I also tried creating a new thread every time I want to do a poll operation using a new ThreadStart and all that. This also seemed to work fine.

I should mention one other thing. This class is part of a COM object server which basically means that the class library that is produced will be called upon via COM. I am not sure if this has any influence on the answer but I just thought I should throw it out there.

The reason I am asking all of this is that all of my test harness runs and debug builds work just fine but when I do a release build and try to make calls to my class from another application, that application freezes up and I am having a hard time determining the cause.

Any advice, suggestions, comments would be appreciated.

Thanks, Jordan

A: 

How about starting a background thread with ThreadPool? Then enter a loop based on a bool (While (bContinue)) that loops and does your work and then a Thread.Sleep at the end of the loop - exiting the program would include setting bContinue to false so the thread stops - perhaps hook it up to the OnStop event in a windows service

bool bRet = ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadFunc));
private void ThreadFunc(object objState) 
{ 
  // enter loop 
  bContinue = true; 
  while (bContinue) { 
     // do stuff 
     // sleep 
     Thread.Sleep(m_iWaitTime_ms); 
  } 
}
bigtang
Something like:
bigtang
bool bRet = ThreadPool.QueueUserWorkItem(new WaitCallback(ReLoadThreadFunc));
bigtang
private void ThreadFunc(object objState) { bool bMessageAvailable = false; object message = null; string strLabel = string.Empty; int iCurChunk = 0; int iTotalChunk = 0; int iCount = 0; Result ret; // enter loop bContinue = true; while (bContinue) { // do stuff // sleep Thread.Sleep(m_iWaitTime_ms); } }
bigtang
@bigtang - you can edit this code into your post and format it using '101010' icon.
Steve Townsend
@Steve - I had a hard time putting the pseudo code in - browser actually crashed a couple of times - I'm new here - what do ya mean the 101010 icon?
bigtang
@Steve - I see the 101010 code button when you answer a question - thanks for that - can I do it in a comment (like this) as well?
bigtang
@bigtang - nope - you have limited code access in comments using the `backquote \`` char to bracket code terms. I have edited the code for you but looks like you see what I meant now - 'edit' your answer and then enter the code, select it and click '101010' icon. Preview window should show up as above.
Steve Townsend
@Steve - thanks for the help
bigtang
@bigtang, yr wlcm
Steve Townsend
+1  A: 

Is the process of contacting the telescope and getting the current values actually take long enough to warrant polling? Have you tried dropping the multithreading and just blocking while you get the current value?

To answer your question, however, I would suggest not using a background worker but an actual Thread that updates the properties continuously.

If all these properties are read only (can you set the temp of the telescope?) and there are no dependencies between them (e.g., no transactions are required to update multiple properties at once) you can drop all the blocking code and let your thread update willy-nilly while other threads access the properties.

I suggest a real, dedicated Thread rather than the thread pool just because of a lack of knowledge of what might happen when mixing background threads and COM servers. Also, apartment state might play into this; with a Thread you can try STA but you can't do that with a threadpool thread.

Will
+2  A: 

Remember that the timer hides its own background worker thread, which basically sleeps for the interval, then fires its Elapsed event. Knowing that, it makes sense just to put the polling in Elapsed. This would be the best practice IMO, rather than starting a thread from a thread. You can start and stop Timers as well, so the code that switches modes can Stop() the Timer, perform the task, then Start() it again, and the Timer doesn't even have to know the telescope IsBusy.

However, what I WOULD keep track of is whether another instance of the Elapsed event handler is still running. You could lock the Elapsed handler's code, or you could set a flag, visible from any thread, that indicates another Elapsed() event handler is still working; Elapsed event handlers that see this flag set can exit immediately, avoiding concurrency problems working with the serial port.

KeithS
+1  A: 

So it looks like you have looked at 2 options:

  1. Timer. The Timer is non-blocking while waiting (uses another thread), so the rest of the program can continue running and be responsive. When the timer event kicks off, you simply get/update the current values.

  2. Timer + BackgroundWorker. The background worker is also simply a separate thread. It may take longer to actually start the thread than to simply get the current values. Unless it takes a long time to get the current values and causes your program to become unresponsive, this is unnecessary complexity.

If getting values is fast enough, stick to #1 for simplicity.

If getting values is slow, #2 will work but unnecessarily has a thread start a thread. Instead, do it with only a BackgroundWorker (no Timer). Create the BackgroundWorker once and store in a variable. No need to recreate it every time. Make sure to set WorkerSupportsCancellation to true. Whenever you want to start checking values, on your main program thread do bgWorker.RunWorkerAsync(). When you want to stop, do bgWorker.CancelAsync(). Inside your DoWork method, have a loop that checks the values and does a Thread.Sleep(500). Since it's a separate thread, it won't make your program unresponsive. In the loop conditions, also check to see if the polling was cancelled and break out. You'll probably need a way to get the values back to the main thread. You can use ReportProgress() if an integer is good enough. Otherwise you can create an object to hold the content, but make sure to lock (object) { } before reading and modifying it. This is a quick summary, but if you go this route I would recommend you read: http://www.albahari.com/threading/part3.aspx#_BackgroundWorker

Nelson
The basic idea of using just the BackgroundThread can be used for a regular Thread as well if you need more control.
Nelson
+1  A: 

You say the app freezes up in a release build? To eliminate extra variables, I'd take all the timer/multi-threaded code out of the application(just comment it out), and try it with a straightforward blocking method.

i.e. You click a button, it calls a function, that function hits the COM object for data, and then updates the UI. All in a blocking, synchronous fashion. This will tell you for sure whether it's the multi-threading code that's freezing you up, or if it's the COM interaction itself.

LoveMeSomeCode
I like this idea! I'll give it a try
Jordan S