Table of contents
  1. Create Thread
  2. Thread Pooling
  3. Synchronization
  4. Async and Await
  5. Parallel
  6. Thread Safety Limited Resource Pool




Create Thread

Thread newThread = new Thread(MyThreadMethod);
        newThread.Start();

Thread Pooling

ThreadPool.QueueUserWorkItem(MyThreadMethod);

Synchronization

private static object LockObject ncw object();
lock (lockobject)
{ // Ihread-safe code
}

Async and Await

public async Task<int> MyAsyncMethod()
{
// Asynchronous code
return await SomeAsyncOperation();
}

Parallel

Parallel. For(0, 10, 1 MyParallel Method(i));

Thread Safety Limited Resource Pool

// Creain Semaphore whith allow 5 sreads to enter sy
private static Semaphore semaphore = new Semaphore(5, 5);
ThreadPool.QueueUserWorkItem(_ =>
{
semaphore.WaitOne();
//Critical section: Only five thruads can chocote this block simultaneously 
semaphore.Release();
}