Running two functions concurrently
Sounds like 2 tasks with a lock should do what you want:
class Program{ static void Main(string[] args) { var task1 = Task.Run(() => Func1()); var task2 = Task.Run(() => Func2()); Task.WaitAll(task1, task2); } static object lockObj = new object(); static void Func1() { for (int i = 0; i < 10; i++) { Func3("Func1"); Thread.Sleep(1); } } static void Func2() { for (int i = 0; i < 10; i++) { Func3("Func2"); Thread.Sleep(1); } } static void Func3(string fromFn) { lock(lockObj) { Console.WriteLine("Called from " + fromFn); } }}
The lock prevents the enclosed code from running in more than one thread at once. (The Sleep
statements are there purely to slow the functions down for demonstration purposes - they will have no place in your final code).
Output is:
Called from Func2Called from Func1Called from Func2Called from Func1Called from Func2Called from Func1Called from Func1Called from Func2Called from Func1Called from Func2