In c #, how does one set a method event to be automatically executed at intervals? Can I use a thread to implement it? How to implement it?
For example, the SendToService method is automatically executed every 10 seconds.
The following is my summary:
[Csharp]
// Define the thread
Thread LogThread = new Thread (new ThreadStart (DoService ));
// Set the thread to a background thread so that no program is closed in the process.
LogThread. IsBackground = true;
If (bStop = true)
{
LogThread. Start (); // Start thread
}
Private static void DoService ()
{
While (true)
{
BStop = false;
SendToService ();
System. Threading. Thread. Sleep (10000 );
}
}
// Define the thread
Thread LogThread = new Thread (new ThreadStart (DoService ));
// Set the thread to a background thread so that no program is closed in the process.
LogThread. IsBackground = true;
If (bStop = true)
{
LogThread. Start (); // Start thread
}
Private static void DoService ()
{
While (true)
{
BStop = false;
SendToService ();
System. Threading. Thread. Sleep (10000 );
}
}