How can I wait for a thread to finish with .NET?

前端 未结 10 2233
有刺的猬
有刺的猬 2020-11-22 10:27

I\'ve never really used threading before in C# where I need to have two threads, as well as the main UI thread. Basically, I have the following.

public void S         


        
10条回答
  •  攒了一身酷
    2020-11-22 10:55

    If using from .NET 4 this sample can help you:

    class Program
    {
        static void Main(string[] args)
        {
            Task task1 = Task.Factory.StartNew(() => doStuff());
            Task task2 = Task.Factory.StartNew(() => doStuff());
            Task task3 = Task.Factory.StartNew(() => doStuff());
            Task.WaitAll(task1, task2, task3);
            Console.WriteLine("All threads complete");
        }
    
        static void doStuff()
        {
            // Do stuff here
        }
    }
    

    From: Create multiple threads and wait all of them to complete

提交回复
热议问题