Home > Net >  How to solve the System. The Timers. The Timer thread unlimited increase cyclic query.
How to solve the System. The Timers. The Timer thread unlimited increase cyclic query.

Time:09-22

Using System. The Timers. The Timer to do a query regularly, because it is called a third-party DLL in the query interface, sometimes only the next query interface doesn't return results, lead to the Timer query each time the loop will generate a thread will not be turned off, the last crash directly, what's the solution?

CodePudding user response:

 {
Private int processInProgress=0;
Private void Timer_Elapsed (object sender, ElapsedEventArgs e)
{
If (System.Threading.Interlocked.Com pareExchange (ref processInProgress, 1, 0).=0)
{
System. Diagnostics. Debug. WriteLine (" the current process is not over, ignore the timer event, ");
return;
}
Try
{
//here to write you now processing routines
}
The finally
{
Interlocked. Exchange (ref processInProgress, 0);
}
}
}

CodePudding user response:

If Interlocked.Com pareExchange bad to understand, you can also use a lock:
 {
Private readonly object lockObj=new object ();
Private bool processInProgress=false;
Private void Timer_Elapsed (object sender, ElapsedEventArgs e)
{
The lock (lockObj)
{
If (processInProgress)
{
System. Diagnostics. Debug. WriteLine (" the current processing is still in operation, ignore the timer event, ");
return;
}
ProcessInProgress=true;
}
Try
{
//here to write you now processing routines
}
The finally
{
The lock (lockObj) processInProgress=false;
}
}
}

CodePudding user response:

You every time is to create a thread? You can try the thread pool,

CodePudding user response:

reference 1st floor github_36000833 response:
 {
Private int processInProgress=0;
Private void Timer_Elapsed (object sender, ElapsedEventArgs e)
{
If (System.Threading.Interlocked.Com pareExchange (ref processInProgress, 1, 0).=0)
{
System. Diagnostics. Debug. WriteLine (" the current process is not over, ignore the timer event, ");
return;
}
Try
{
//here to write you now processing routines
}
The finally
{
Interlocked. Exchange (ref processInProgress, 0);
}
}
}

At the end of they can not deal with, to how to get to close this thread? I tried the System. The Timers. Timer the Dispose () method and is still running after the Stop () method,

CodePudding user response:

reference 4 floor qq_40569597 response:
...
At the end of they can not deal with, to how to get to close this thread? I tried the System. The Timers. Timer the Dispose () method and is still running after the Stop () method,


I don't recommend "close this thread", forced closure may cause such problems as dead-lock disclosure or resources,

CodePudding user response:

Get a public variables recorded cycles, add a lock, and cycle number are no longer cycle,

CodePudding user response:

Start from the business process is the fundamental,
Query timeout, to analyze the cause of the timeout,
1. If it is a large amount of data query of the timeout, then to optimize the business process
Or time period or different constraint conditions, avoid timeout
2. If is the problem that a third-party DLL, and provider DLL technology communication,
Boundary conditions, clear call
If a DLL is invoked synchronously, which can be required DLL provider provides asynchronous invocation interface




CodePudding user response:

Use Interlocked Increment/decrement determines if=0

CodePudding user response:

I doubt is the inside of the DLL, which leads to the thread can't quit, only new manually a thread, and end forced overtime thread, or thread pool is swallowed up in it
  •  Tags:  
  • C#
  • Related