Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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# - Fatal编程技术网

C# 如何等待直到委托事件完成?

C# 如何等待直到委托事件完成?,c#,C#,如何等待委托方法完成 await webBrowser1.DocumentCompleted += delegate { //finished }; //when finished do some things 那么如何做到这一点呢 谢谢您应该调用它。调用块,直到委托返回 Invoke((webBrowser1.DocumentCompleted)delegate { //Yo

如何等待委托方法完成

  await webBrowser1.DocumentCompleted += delegate
            {

                //finished 
            }; 

        //when finished do some things 
那么如何做到这一点呢


谢谢

您应该调用它。调用块,直到委托返回

Invoke((webBrowser1.DocumentCompleted)delegate
{
    //Your code here
); 

//This will run after 'Your code here'

就我个人而言,我建议您使用我认为可以满足您需求的解决方案

WebBrowser类:

您可以如何举办活动:

WebBrowser w = new WebBrowser();

w.DocumentCompleted += delegate (object o, EventArgs e)
{
   var result = " hello event ";
};
//this part invoke all the delegates or method registered to this event .
await w.RaiseEvent(this, New EventArgs());

该行代码不执行
DocumentCompleted
。它将委托注册为事件的事件处理程序
DocumentCompleted
。正如@John所说,您的事件不会运行,它只是一个注册。该代码在UI线程上运行,您不能等待。尝试等待会导致死锁,DocumentCompleted事件无法激发。只需将这一行代码之后的所有代码移动到事件处理程序中即可。不明智的是,事件是由WebBrowser引发的。我认为您的建议无法回答这个问题
WebBrowser w = new WebBrowser();

w.DocumentCompleted += delegate (object o, EventArgs e)
{
   var result = " hello event ";
};
//this part invoke all the delegates or method registered to this event .
await w.RaiseEvent(this, New EventArgs());