/// <summary>
        /// Threads main function.
        /// </summary>
        private void Worker()
        {
            // 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
                    System.Threading.Monitor.Enter(this);
                    if (OnJob != null)
                    {
                        System.Threading.Monitor.Exit(this);
                        OnJob(evArgs);
                        System.Threading.Monitor.Enter(this);
                    }
                    System.Threading.Monitor.Exit(this);

                    // NOTE: It is also possible to abort the finish event !!!
                    System.Threading.Monitor.Enter(this);
                    if (OnFinish != null)
                    {
                        System.Threading.Monitor.Exit(this);
                        OnFinish(evArgs);
                        System.Threading.Monitor.Enter(this);
                    }
                    System.Threading.Monitor.Exit(this);
                }
                catch (System.Threading.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();

                    System.Threading.Monitor.Enter(this);
                    if (OnAbort != null)
                    {
                        System.Threading.Monitor.Exit(this);
                        OnAbort(evArgs);
                        System.Threading.Monitor.Enter(this);
                    }
                    System.Threading.Monitor.Exit(this);
                }
            }
            catch (System.Threading.ThreadAbortException)
            {
                // Ignore this king of exception, it was already handled...but if not reseted, it is automatically re-thrown.
            }
            catch (Exception ex)
            {
                // HANDLING EXCEPTIONS
                System.Threading.Monitor.Enter(this);
                if (OnException != null)
                {
                    ThreadHandlerExceptionArgs exArgs = new ThreadHandlerExceptionArgs(this, ex);

                    System.Threading.Monitor.Exit(this);
                    OnException(exArgs);
                    System.Threading.Monitor.Enter(this);
                }
                System.Threading.Monitor.Exit(this);
            }
            finally
            {
                // CLEAN-UP - NOTE: Exceptions are not handled here, so be careful!
                System.Threading.Monitor.Enter(this);
                if (OnTerminate != null)
                {
                    System.Threading.Monitor.Exit(this);
                    OnTerminate(evArgs);
                    System.Threading.Monitor.Enter(this);
                }
                System.Threading.Monitor.Exit(this);
            }

            // This line executes only, when ThreadAbortException is Reset-ed...
            System.Console.WriteLine("HALO...");
        }
Beispiel #2
0
 /// <summary>
 /// Represents the method that will handle the OnFinish event.
 /// </summary>
 public virtual void OnFinish(ThreadHandlerEventArgs arg)
 {
 }
Beispiel #3
0
 /// <summary>
 /// Represents the method that will handle the OnAbort event.
 /// </summary>
 public virtual void OnAbort(ThreadHandlerEventArgs arg)
 {
 }
Beispiel #4
0
 /// <summary>
 /// Represents the method that will handle the OnTerminate event.
 /// </summary>
 public virtual void OnTerminate(ThreadHandlerEventArgs arg)
 {
 }
Beispiel #5
0
 /// <summary>
 /// Represents the method that will handle the OnJob event.
 /// </summary>
 public abstract void OnJob(ThreadHandlerEventArgs arg);