C#从另一个线程上调用代理捕获异常 [英] C# Catching Exception From Invoked Delegate on another Thread

查看:262
本文介绍了C#从另一个线程上调用代理捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码如下。这是在线程2上运行

  WebBrowser browser = this.webBrowser 
browser.Invoke(new MethodInvoker {browser.Document.GetElementById(somebutton)。InvokeMember(click);}));
Thread.Sleep(500);
browser.Invoke(new MethodInvoker(delegate {browser.Document.GetElementById(username)。SetAttribute(value,username);}));
//由几个更相似的统计资料

本质上我在WebBrowser上调用一些方法如果在浏览器中加载的当前页面上的元素不包含元素somebtn或username,则在另一个线程上创建的控件Thread 1。



有一个例外是从线程1抛出的。



有没有办法在线程2上捕获该异常?我知道我可以在代表中使用try catch,并有一个特殊的代理返回一些值(如异常),但是有什么办法可以选择吗?



注意*:我需要Thread.Sleep作为特定页面需要某些事件之间的延迟。如果有一些方法将这些事件组合成一个代理(同时保留某种形式的非阻塞延迟),我认为这可以工作,我将把它们全部包装在单个try catch中,并创建一个返回异常的委托

解决方案

尽管 Control.Invoke()通过UI线程执行委托 - 它仍然是一个同步调用。同步含义调用将不会返回,直到委托完成执行(或抛出异常)。您可以简单地捕获那里抛出的异常。

  WebBrowser browser = this.webBrowser; 
try {
browser.Invoke(new MethodInvoker(delegate {browser.Document.GetElementById(somebutton)。InvokeMember(click);}));
Thread.Sleep(500);
browser.Invoke(new MethodInvoker(delegate {browser.Document.GetElementById(username)。SetAttribute(value,username);}));
} catch(Exception e)
{
// catch in Thread 2
}


I have some code as follows. This is running on "Thread 2"

WebBrowser browser = this.webBrowser    
browser.Invoke(new MethodInvoker(delegate { browser.Document.GetElementById("somebutton").InvokeMember("click"); }));
Thread.Sleep(500);
browser.Invoke(new MethodInvoker(delegate { browser.Document.GetElementById("username").SetAttribute("value", username); }));
//folowed by several more similar statments

Essentially I am Invoking some methods on a WebBrowser control created on a different thread, "Thread 1".

If the element on the current page loaded in browser does not contain an element "somebtn" or "username", an exception is thrown from "Thread 1".

Is there any way to catch that exception on "Thread 2"? I know I could use try catches within the delegates and have a special delegate that returns some value(like an exception), but is there any way around that options?

Note*: I require the Thread.Sleep as the particular page requires some delay between certain events. If there was some way to combine these events into a single delegate(while retaining some form of non-blocking delay), I think that could work and I would just wrap all of them in single try catch and create a delegate that returns an exception.

解决方案

Although Control.Invoke() executes delegate over UI thread - it is still a synchronous call. Synchronous meaning Invoke will not return until the delegate has completed execution (or exception thrown). You can simply catch the exceptions thrown there.

WebBrowser browser = this.webBrowser;
try {    
    browser.Invoke(new MethodInvoker(delegate { browser.Document.GetElementById("somebutton").InvokeMember("click"); }));
    Thread.Sleep(500);
    browser.Invoke(new MethodInvoker(delegate { browser.Document.GetElementById("username").SetAttribute("value", username); }));
} catch(Exception e) 
{
    //catch in Thread 2
}

这篇关于C#从另一个线程上调用代理捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆