Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET 1.0线程池问题_C#_Multithreading_.net 1.0 - Fatal编程技术网

C# .NET 1.0线程池问题

C# .NET 1.0线程池问题,c#,multithreading,.net-1.0,C#,Multithreading,.net 1.0,我正在尝试生成一个线程来处理需要不到3秒时间的DoWork任务。里面的道具需要15秒。我想中止DoWork并将控件转移回主线程。我复制的代码如下,它不工作。它没有中止DoWork,而是完成DoWork,然后将控件传输回主线程。我做错了什么 class Class1 { /// <summary> /// The main entry point for the application. /// </summary> /// pr

我正在尝试生成一个线程来处理需要不到3秒时间的DoWork任务。里面的道具需要15秒。我想中止DoWork并将控件转移回主线程。我复制的代码如下,它不工作。它没有中止DoWork,而是完成DoWork,然后将控件传输回主线程。我做错了什么

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);
    }
}
Class1类
{
/// 
///应用程序的主要入口点。
/// 
/// 
私有静态系统.Threading.ManualResetEvent[]resetEvents;
[状态线程]
静态void Main(字符串[]参数)
{
重置事件=新的手动重置事件[1];
int i=0;
resetEvents[i]=新的ManualResetEvent(错误);
QueueUserWorkItem(新的WaitCallback(DoWork),(对象)i);
Thread.CurrentThread.Name=“主线程”;
WriteLine(“[{0}]正在主方法中等待”,Thread.CurrentThread.Name);
DateTime start=DateTime.Now;
日期时间结束;
TimeSpan=DateTime.Now.Subtract(开始);
//如果时间超过3秒,则中止dowork方法
//并将控制转移到主线程。
做
{
如果(跨度秒<3)
WaitHandle.WaitAll(重置事件);
其他的
重置事件[0]。设置();
end=DateTime.Now;
span=结束。减去(开始);
}而(跨度秒<2);
控制台写入线(跨度秒);
WriteLine(“[{0}]全部在主方法中完成”,Thread.CurrentThread.Name);
Console.ReadLine();
}
静态空心榫钉(对象o)
{
int指数=(int)o;
Thread.CurrentThread.Name=“执行工作线程”;
//模拟繁重的工作。
睡眠(15000);
//工作完成了。。
resetEvents[index].Set();
WriteLine(“[{0}]完成工作”,Thread.CurrentThread.Name);
}
}

[STAThread]
是单线程单元。请尝试使用多线程单元。

所有线程都是后台线程,这意味着它们在应用程序的前台线程结束时自动终止

我更改了你的循环并删除了重置事件

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

如果你真的在使用VS2002,那么有一件事你做错了,那就是使用八年前的软件。你为什么不至少运行.NET 1.1 SP1?我不知道使用的是哪个版本的.NET。我尝试了[MTAThread],但它不起作用。我仍然无法中止DoWork方法。