Multiple threads in windows service

心已入冬 提交于 2019-12-07 02:21:29

I recommend you look at the .NET 4.0 Task class. Firing full threads every time might be overkill. Tasks, together with timers use the underlying thread pool to execute work in parallel.

Using a Task is as simple as:

Task t = Task.Factory.StartNew(
       () => 
       {
           // task code here
       });
using System;
using System.Threading;

class Program
{
    static void Main()
    {
    Thread thread1 = new Thread(new ThreadStart(A));
    Thread thread2 = new Thread(new ThreadStart(B));
    thread1.Start();
    thread2.Start();
    thread1.Join();
    thread2.Join();
    }

    static void A()
    {
    Thread.Sleep(100);
    Console.WriteLine('A');
    }

    static void B()
    {
    Thread.Sleep(1000);
    Console.WriteLine('B');
    }
}

Threading Tutorial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!