Skip to content

Instantly share code, notes, and snippets.

@musoftware
Created October 7, 2022 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musoftware/bb9c83f0cc28013379b3cbfd24a9d514 to your computer and use it in GitHub Desktop.
Save musoftware/bb9c83f0cc28013379b3cbfd24a9d514 to your computer and use it in GitHub Desktop.
Sequence Thread Starting and wait
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
private static Semaphore _pool = new Semaphore(initialCount: 1, maximumCount: 1);
static void Main(string[] args)
{
DoA();
DoB();
}
private static void DoA()
{
new Thread(() =>
{
Thread.Sleep(1000);
try
{
_pool.WaitOne();
Console.WriteLine("A Begin");
Thread.Sleep(1000);
Console.WriteLine("A Process");
Thread.Sleep(1000);
}
finally
{
_pool.Release();
Console.WriteLine("A End");
}
}).Start();
}
private static void DoB()
{
new Thread(() =>
{
Thread.Sleep(1000);
try
{
_pool.WaitOne();
Console.WriteLine("B Begin");
Thread.Sleep(1000);
Console.WriteLine("B Process");
Thread.Sleep(1000);
}
finally
{
_pool.Release();
Console.WriteLine("B End");
}
}).Start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment