[英]Delegate.BeginInvoke doesn't appear to do anything on a different PC
private void button1_Click(object sender, EventArgs e)
{
this.icon_testLOAD.Visible = true;
this.icon_testOK.Visible = false;
this.icon_testBAD.Visible = false;
this.debug("Test Service Button Clicked");
rabbitmq_test t = new rabbitmq_test(button_rabbitmq_test);
this.debug("Calling BeginInvoke on button_rabbitmq_test delegate");
t.BeginInvoke(null, null);
}
所以我有這個按鈕單擊事件。 前三行打開和關閉包含圖標的PictureBox。
this.debug()僅調用EventLog.WriteEntry()
button_rabbitmq_test方法如下所示:
protected void button_rabbitmq_test()
{
this.debug("Creating new rabbitmq connection factory");
IConnection connection;
try
{
ConnectionFactory rq_factory = new ConnectionFactory();
rq_factory.Port = Convert.ToInt16(this.psistats_config.rabbitmq_port);
rq_factory.HostName = this.psistats_config.rabbitmq_server;
rq_factory.UserName = this.psistats_config.rabbitmq_username;
rq_factory.Password = this.psistats_config.rabbitmq_password;
rq_factory.RequestedConnectionTimeout = 15000;
this.debug("Creating new rabbitmq connection");
connection = rq_factory.CreateConnection();
this.debug("Changing icon to successful");
rabbitmq_icon_delegate d = new rabbitmq_icon_delegate(this.testOK);
connection.Close();
this.test_button.Invoke(d);
}
catch (Exception exc)
{
if (connection != null)
{
connection.Close();
}
this.debug("Failed testing the rabbit server");
this.debug(exc.Message);
this.debug(exc.StackTrace);
rabbitmq_icon_delegate d = new rabbitmq_icon_delegate(this.testFailed);
this.test_button.Invoke(d);
}
}
該代碼可以在我正在開發的計算機上正常工作。 該方法將執行,事件日志將按預期填充。 但是,當我在第二台計算機上運行此應用程序時,BeginInvoke方法似乎根本不執行任何操作,而且我完全不知道為什么。
我在事件日志中看到的最后一條消息是“ Calling BeginInvoke ...”,但是在任何地方都看不到執行實際測試的方法的事件日志。
該應用程序也不會凍結。 我仍然可以使用它。
我對我做錯了事感到茫然,歡迎任何建議。
該代碼從根本上來說是有缺陷的,您必須調用委托的EndInvoke()方法。 最好不要將null作為第一個參數傳遞,使用回調方法 ,然后再調用EndInvoke()。
如果不調用EndInvoke(),則會浪費資源,持續10分鍾。 最終問題是,您看不到該方法引發的異常。 因此,您無法找出為什么它不起作用。 調用EndInvoke()會拋出該異常。
使用委托的BeginInvoke()方法是一種低級編程技術,最好避免這種方法,它太容易產生這樣的錯誤,也很難處理異常。 請改用BackgroundWorker或Task。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.