IThreadMain

Description

interface Tinman.Core.Threading.IThreadMain

Derived from

IDisposable

Extended by

ThreadMainBase abstract
Tutorial_02_Mandelbrot sealed

Base interface for classes that provide a main method for a background thread.

The Run method provides special semantics that support a single-threaded emulation mode (see Thread.SingleThreadedMode):

class MyThreadMain : IThreadMain
{
  public bool Run(ThreadContext context)
  {
    if (context.ShallInit)
    {
      // One-time initialization may be performed here.
    }
    else
    {
      // Normal thread work may be performed here, one cycle at a time.

      monitor.Begin();
      try
      {
        while (!MyConditionExpression)
        {
          // Avoid deadlocks by exiting the thread while waiting for a condition.

          if (context.ShallQuit)
            return false;

          // This will give back control to the sole application thread.

          if (Thread.IsSingleThreaded)
            return true;

          monitor.WaitForNotify();
        }
      }
      finally
      {
        monitor.End();
      }
    }

    // This thread cycle has ended but the thread is still alive. Another call to this
    // method will be made later.

    return true;
  }
}

Obeying the rules above will improve responsiveness when running in single-threaded emulation mode. When running only in regular multi-threaded mode, the Run method may of course be implemented in the traditional way, for example:

class MyThreadMain : IThreadMain
{
  public bool Run(ThreadContext context)
  {
    // One-time initialization may be performed here.

    while (true)
    {
      // Normal thread work may be performed here, one cycle at a time.

      monitor.Begin();
      try
      {
        while (!MyConditionExpression)
        {
          // Avoid deadlocks by exiting the thread while waiting for a condition.

          if (context.ShallQuit)
            return false;

          monitor.WaitForNotify();
        }
      }
      finally
      {
        monitor.End();
      }
    }

    return false;
  }
}

Public / Methods

Request​Stop


public method RequestStop → ()

Signals the running thread main method to terminate gracefully.

This method is called exactly once; either when the Thread.Stop method is invoked for the first time or when the thread is disposed.

Run


[ThrowAny]
public method Run → (1)

context in : ThreadContext

[not-null]
The thread context object. When this method is called for the first time, the ThreadContext.ShallInit property is true. If the method returns true, it will be called again by the same native thread. Then, the property will be false. If the method returns false, the native thread will terminate.

returns → bool

true if this method shall be called again,
false if the thread shall terminate.

This is the main method of the thread.

If this method throws an exception, the thread is terminated; the exception details can then be obtained via Thread.Error.

Waiting​For​Stop


public method WaitingForStop → ()

This method is invoked regularly when one thread has requested another one to stop and is waiting for its termination.

This callback allows a thread to perform actions while being blocked inside Thread.Join resp. IDisposable.Dispose (which implicitly joins on the thread), for example flushing buffers in order to avoid dead locks.