c# execute 2 threads simultaneously

前端 未结 1 1043
轮回少年
轮回少年 2021-01-19 06:11

I am trying to reproduce a threading error condition within an HTTP Handler.

Basically, the ASP.net worker procecss is creating 2 threads which invoke the HTTP hand

相关标签:
1条回答
  • 2021-01-19 06:56

    You could just set a static time to start your work like this.

    private static DateTime startTime = DateTime.Now.AddSeconds(5); //arbitrary start time
    
    static void Main(string[] args)
    {
        ThreadStart threadStart1 = new ThreadStart(DoSomething);
        ThreadStart threadStart2 = new ThreadStart(DoSomething);
        Thread th1 = new Thread(threadStart1);
        Thread th2 = new Thread(threadStart2);
    
        th1.Start();             
        th2.Start();
    
        th1.Join();
        th2.Join();
    
        Console.ReadLine();
    }
    
    private static void DoSomething()
    {
        while (DateTime.Now < startTime)
        {
            //do nothing
        }
    
        //both threads will execute code here concurrently
    }
    
    0 讨论(0)
提交回复
热议问题