Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

C# has the .NET framework System.Threading namespace to enable using threads. Using the namespace, one can make auxiliary threads, or “worker threads” that run in addition to the main thread of a C# program.

To create and start a thread that runs a void-returning function named “funcName”:

Image Added

 

Making the thread and inserting the function to run can also be done in one line as so:

Image Added

To make the thread sleep for 2 seconds:

t.Sleep(2000)

 

To abort the thread (this stops the thread entirely):

t.Abort()

 

To yield execution of the current thread to another thread that is ready to run:

t.Yield()

 

To create a mutex, make a global mutex variable in the program, eg:

private static Mutex mutex = new Mutex()

 

To lock the mutex “mutex”, use
mutex.WaitOne()

 

To unlock it, use

mutex.ReleaseMutex()