Multiple threads in windows service

大兔子大兔子 提交于 2019-12-08 06:50:47

问题


I have windows project and one form which have timer for each 5 seconds.

It calls and processes methods from request named table time wise and condition wise.

But I have some methods types which takes too much time to respond and want those methods in separate thread. So that I can run those both request types in separate threads and syncs.

How can I do separate those both using thread -- multi async threads?


回答1:


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
       });



回答2:


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



来源:https://stackoverflow.com/questions/11985308/multiple-threads-in-windows-service

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