Public Class Worker
{
// This method will be called when the thread is started.
Public Void Dowork ()
{
Console. writeline ( " The main thread is started successfully! " );
While ( ! _ Shouldstop)
{
Console. writeline (datetime. Now. tostring ( " Yyyymmddhhmmss " ) + " : The main process is working! " );
Guard. guardid = True ;
Getnewemails ();
System. Threading. thread. Sleep ( 1000 * Refreshinterval );
}
Console. writeline ( " The main thread is disabled! " );
}
Public Void Requeststop ()
{
_ Shouldstop = True ;
}
Public Void Requestopen ()
{
_ Shouldstop = False ;
}
Public Bool Shouldstop
{
Get { Return _ Shouldstop ;}
Set {_ Shouldstop = Value ;}
}
Private Bool _ Shouldstop = False ;
}
Guard. CS
Class Guard
{
Static Public Bool Guardid = False ;
Static Public Int Guardinterval = Convert. toint32 (configurationmanager. receivettings [ " Guardinterval " ]);
Static Public Void Guardthread ()
{
While ( True )
{
System. Threading. thread. Sleep ( 1000 * Guardinterval );
If ( ! Guardid)
{
Console. writeline ( " The main thread is abnormal! " );
Try
{
// Program. workerthread. Abort ();
Program. workerobject. requeststop ();
Program. workerthread. Join ();
Thread. Sleep ( 6000 );
Program. workerthread = Null ;
Console. writeline ( " The value of the main thread is null! " );
Program. workerthread = New Thread (program. workerobject. dowork );
Console. writeline ( " The main thread is re-created! " );
Program. workerthread. Start ();
Program. workerobject. requestopen ();
}
Catch (Exception E)
{
Console. writeline ( " Daemon: " + E. Message );
}
}
Else
{
Console. writeline ( " The main thread is normal! " );
}
Guardid= False;
}
}
}
Static Class Program
{
/// <Summary>
/// ApplicationProgram.
/// </Summary>
Static Public Worker workerobject = New Worker ();
Static Public Guard guardobject = New Guard ();
Static Public Thread workerthread = Null ;
Static Public Thread guardthread = Null ;
Static Void Main ()
{
Workerthread = New Thread (workerobject. dowork );
Guardthread = New Thread (guard. guardthread );
Workerthread. Start ();
Guardthread. Start ();
}
}