简体   繁体   English

.NET 1.0 ThreadPool问题

[英].NET 1.0 ThreadPool Question

I am trying to spawn a thread to take care of DoWork task that should take less than 3 seconds. 我正在尝试生成一个线程来处理应该少于3秒的DoWork任务。 Inside DoWork its taking 15 seconds. 在DoWork内部,耗时15秒。 I want to abort DoWork and transfer the control back to main thread. 我想中止DoWork,然后将控件转移回主线程。 I have copied the code as follows and its not working. 我已将代码复制如下,并且无法正常工作。 Instead of aborting DoWork, it still finishes DoWork and then transfers the control back to main thread. 它仍然可以完成DoWork,然后将控制权转回主线程,而不是终止DoWork。 What am I doing wrong? 我究竟做错了什么?

class Class1
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    /// 

    private static System.Threading.ManualResetEvent[] resetEvents;

    [STAThread]
    static void Main(string[] args)
    {
        resetEvents = new ManualResetEvent[1];

        int i = 0;

        resetEvents[i] = new ManualResetEvent(false);
        ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork),(object)i);


        Thread.CurrentThread.Name = "main thread";

        Console.WriteLine("[{0}] waiting in the main method", Thread.CurrentThread.Name);

        DateTime start = DateTime.Now;
        DateTime end ;
        TimeSpan span = DateTime.Now.Subtract(start);


        //abort dowork method if it takes more than 3 seconds
        //and transfer control to the main thread.
        do
        {
            if (span.Seconds < 3)
                WaitHandle.WaitAll(resetEvents);
            else
                resetEvents[0].Set();


            end = DateTime.Now;
            span = end.Subtract(start);
        }while (span.Seconds < 2);



        Console.WriteLine(span.Seconds);


        Console.WriteLine("[{0}] all done in the main method",Thread.CurrentThread.Name);

        Console.ReadLine();
    }

    static void DoWork(object o)
    {
        int index = (int)o;

        Thread.CurrentThread.Name = "do work thread";

        //simulate heavy duty work.
        Thread.Sleep(15000);

        //work is done..
        resetEvents[index].Set();

        Console.WriteLine("[{0}] do work finished",Thread.CurrentThread.Name);
    }
}

All pooled threads are background threads, meaning they terminate automatically when the application's foreground thread(s) end. 所有池线程均为后台线程,这意味着它们在应用程序的前台线程结束时自动终止。

I changed your loop and removed the resetEvents. 我更改了循环并删除了resetEvents。

     //abort dowork method if it takes more than 3 seconds 
     //and transfer control to the main thread. 
     bool keepwaiting = true;
     while (keepwaiting)
     {
        if (span.Seconds > 3)
        {
           keepwaiting = false;
        }

        end = DateTime.Now;
        span = end.Subtract(start);
     }

[STAThread] is Single Thread Apartment. [STAThread]是单线程单元。 Try [MTAThread] which is Multi Thread Apartment. 尝试[MTAThread] ,它是多线程单元。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM