/// <summary> /// This function MUST BE IMPLEMENTED... /// </summary> /// <param name="arg"></param> public override void OnJob(ThreadHandlerEventArgs arg) { // THE WORKING FUNCTION... while (_cnt > 0) { System.Console.WriteLine("Counter = " + _cnt.ToString()); _cnt--; System.Threading.Thread.Sleep(500); } }
/// <summary> /// TH_s the on job. /// </summary> /// <param name="arg">The <see cref="T:Rainbow.Framework.Threading.ThreadHandlerEventArgs"/> instance containing the event data.</param> private static void th_OnJob(ThreadHandlerEventArgs arg) { // Example 1 - Not used... }
public override void OnTerminate(ThreadHandlerEventArgs arg) { base.OnTerminate(arg); System.Console.WriteLine("Terminate"); }
/// <summary> /// Represents the method that will handle the OnFinish event. /// </summary> public virtual void OnFinish(ThreadHandlerEventArgs arg) { }
// The following functions are only support functions. They must not be overrided. public override void OnFinish(ThreadHandlerEventArgs arg) { base.OnFinish(arg); System.Console.WriteLine("Finish"); }
/// <summary> /// Represents the method that will handle the OnTerminate event. /// </summary> public virtual void OnTerminate(ThreadHandlerEventArgs arg) { }
public override void OnAbort(ThreadHandlerEventArgs arg) { base.OnAbort(arg); System.Console.WriteLine("Abort"); }
/// <summary> /// Represents the method that will handle the OnJob event. /// </summary> public abstract void OnJob(ThreadHandlerEventArgs arg);
/// <summary> /// Represents the method that will handle the OnAbort event. /// </summary> public virtual void OnAbort(ThreadHandlerEventArgs arg) { }
/// <summary> /// Threads main function. /// </summary> void Work() { // Create the event-arg-object which will be used in each event. ThreadHandlerEventArgs evArgs = null; lock (this) { evArgs = new ThreadHandlerEventArgs(this); } // NOTE: // Locking is not necessary if you assume, that all the events are assigned before starting the thread. // ALSO even if locking is implemented, there can a PROBLEM still arise if the last delegate function is removed after the lock on the thread is released and before the event is raised. try { try { // MAIN FUNCTION Monitor.Enter(this); if (OnJob != null) { Monitor.Exit(this); OnJob(evArgs); Monitor.Enter(this); } Monitor.Exit(this); // NOTE: It is also possible to abort the finish event !!! Monitor.Enter(this); if (OnFinish != null) { Monitor.Exit(this); OnFinish(evArgs); Monitor.Enter(this); } Monitor.Exit(this); } catch (ThreadAbortException) { // WHEN ABORTING // NOTE: If you call ResetAbort the ThreadAbortException will not be re-throw-ed after each catch block... Code after block finally will also be executed... // System.Threading.Thread.ResetAbort(); Monitor.Enter(this); if (OnAbort != null) { Monitor.Exit(this); OnAbort(evArgs); Monitor.Enter(this); } Monitor.Exit(this); } } catch (ThreadAbortException) { // Ignore this king of exception, it was already handled...but if not reseted, it is automatically re-thrown. } catch (Exception ex) { // HANDLING EXCEPTIONS Monitor.Enter(this); if (OnException != null) { var exArgs = new ThreadHandlerExceptionArgs(this, ex); Monitor.Exit(this); OnException(exArgs); Monitor.Enter(this); } Monitor.Exit(this); } finally { // CLEAN-UP - NOTE: Exceptions are not handled here, so be careful! Monitor.Enter(this); if (OnTerminate != null) { Monitor.Exit(this); OnTerminate(evArgs); Monitor.Enter(this); } Monitor.Exit(this); } // This line executes only, when ThreadAbortException is Reset-ed... Console.WriteLine("HALO..."); }