Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.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/1/vue.js/6.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# 如何使用(委托方法)在C中等待线程进程完成_C#_Callback_Wait - Fatal编程技术网

C# 如何使用(委托方法)在C中等待线程进程完成

C# 如何使用(委托方法)在C中等待线程进程完成,c#,callback,wait,C#,Callback,Wait,请帮帮我。我的想法是使用线程概念连续打印从0到1000的数值。如果我的应用程序无一例外地关闭,我如何编写等待当前正在运行的线程任务完成的代码 在这里我提到了示例代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using

请帮帮我。我的想法是使用线程概念连续打印从0到1000的数值。如果我的应用程序无一例外地关闭,我如何编写等待当前正在运行的线程任务完成的代码

在这里我提到了示例代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.Remoting.Messaging;
using System.IO;

namespace Test_AsyncFactorCaller
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public bool Work()
        {
            int nSleep = 100;
            WriteMessage(string.Format("Going to Thread Sleep State for {0} sec", nSleep));
            for (int i = 0; i < nSleep; i++)
            {
                WriteMessage(string.Format("Sleeping = {0}", i));
                Thread.Sleep(1000);
            }
            WriteMessage("Going to Thread Wakeup State");
            return true;
        }
        public void Work_Done(IAsyncResult result)
        {
            WriteMessage("Work_Done");
            AsyncFactorCaller t = (AsyncFactorCaller)((AsyncResult)result).AsyncDelegate;
            bool bResult = t.EndInvoke(result);
            WriteMessage(string.Format("Result {0}",bResult));
            result.AsyncWaitHandle.Close();
        }
        public void WriteMessage(string sMessage)
        {
                using (StreamWriter sw = new StreamWriter(@"C:\ThreadLog.txt", true))
                {
                    sw.WriteLine(sMessage);
                    sw.Close();
                }
        }
        private void btn_asyncCaller_Click(object sender, EventArgs e)
        {
            try
            {
                AsyncFactorCaller dGate_caller = new AsyncFactorCaller(Work);
                AsyncCallback Completed_callBack = new AsyncCallback(Work_Done);
                AsyncOperation asyncOperation = AsyncOperationManager.CreateOperation(null);
               IAsyncResult result = dGate_caller.BeginInvoke(Completed_callBack, "Test thread");
             }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
       public delegate bool AsyncFactorCaller();
    }
}

如果你真的确定这是你需要做的,试着使用一个


您是否考虑过使用任务并行库?看见触发一个异步操作然后等待它完成有什么用呢?这毫无意义!您应该使用async Wait Thomas Weller:i触发异步操作。在处理期间,如果我杀死application.exe,异步操作也会停止,而不会完成它的当前进程。但是我想知道,如果我停止station或Application.exe异步操作,等待完成它的当前线程进程。抱歉我的英语不好…Thomas Weller:为什么我触发一个异步操作,然后等待它完成,这意味着想象工作函数做了一个非常大的过程,所以它需要更多的执行时间线程睡眠时间
AutoResetEvent _blocker = new AutoResetEvent(false);

//In background thread
_blocker.Set();

//Where you want to wait for it
_blocker.WaitOne();