Best practice for a continuous process in C #

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

My project has a Focuser.cs class, which is a physical device focusing a telescope that can communicate with a PC via a serial port (RS-232). My class (Focuser) has properties such as CurrentPosition, CurrentTemperature, ect, which represents the current focus conditions, which can change at any time. So, my Focuser class needs to constantly poll the device for these values ​​and update its internal fields. My question is: what is the best way to perform this continuous polling sequence? Sometimes the user needs to switch the device to another mode, which will require the ability to stop polling, perform an action, and then resume polling.

My first attempt was to use a time that ticks every 500 ms and then call a background worker who will poll one position and then one temperature will return. When the timer loops, if the background worker isBusy, then it just returns and tries again 500ms later. Someone suggested I completely get rid of the background worker and just do a poll in the timer event. Therefore, I set the timer’s AutoReset property to false, and then simply restart the timer every time the poll ends. These two methods seemed to behave exactly the same in my application, so I'm not sure if it is better than the other. I also tried to create a new thread every time I want to perform a polling operation using a new ThreadStart and all that. This seemed to work fine too.

I have to mention one more thing. This class is part of the COM object server, which basically means that the generated class library will be called through COM. I'm not sure if this influenced the answer, but I just thought I should throw it away.

The reason I ask for all this is because all my test harnesses work, and the debug builds work fine, but when I build releases and try to make calls to my class from another application, this application freezes, and I am it’s difficult to determine the cause.

Any advice, suggestions, comments would be appreciated.

Thank you Jordan

+5
source share
5 answers

, , , Elapsed. , Elapsed. IMO, . , , , () , , (), IsBusy.

, , Elapsed. Elapsed, , , , Elapsed() ; , , , concurrency, .

+2

, , 2 :

  • . ( ), . , / .

  • + BackgroundWorker. - . , . , , .

, # 1 .

, # 2 , , . BackgroundWorker ( ). BackgroundWorker . . , WorkerSupportsCancellation true. , , bgWorker.RunWorkerAsync(). , bgWorker.CancelAsync(). DoWork , Thread.Sleep(500). , . , . , . ReportProgress(), . , lock (object) { }. , , : http://www.albahari.com/threading/part3.aspx#_BackgroundWorker

+2

, ? , ?

, , , .

( ?), (, ), -, .

Thread, - , COM-. , ; Thread STA, threadpool.

+1

, ? , / ( ) .

. , , COM , . , . , , , COM.

+1

ThreadPool? bool (While (bContinue)), , Thread.Sleep - bContinue false, - , OnStop windows

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); 
  } 
}
0

All Articles