Skip to content

Instantly share code, notes, and snippets.

@kentkost
Last active April 11, 2020 08:37
Show Gist options
  • Save kentkost/55ab33f9af1a7fcc8d9b5e298d0c47c7 to your computer and use it in GitHub Desktop.
Save kentkost/55ab33f9af1a7fcc8d9b5e298d0c47c7 to your computer and use it in GitHub Desktop.
Threading with parameters automatic delegate casting #threads
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Threading
{
class Program
{
public static Semaphore semaphore = new Semaphore(2, 3);
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++) {
Thread t = StartTheThread(i, i + 1);
}
Console.ReadKey();
}
public static Thread StartTheThread(int param1, int param2)
{
var t = new Thread(() => RealStart(param1, param2));
t.Name = param1.ToString();
t.Start();
return t;
}
private static void RealStart(int param1, int param2)
{
Console.WriteLine("Thread: " + Thread.CurrentThread.Name+" Waiting");
try {
Console.WriteLine("Thread: " + Thread.CurrentThread.Name + " Started");
semaphore.WaitOne();
Thread.Sleep(5000);
Console.WriteLine(param1 + param2);
}
finally {
Console.WriteLine("Thread: " + Thread.CurrentThread.Name + " Ending");
semaphore.Release();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment