[英]Why this MessageBox does not appear?
我有下面這樣的EventHandler代碼。
void ConnectionManager_Error(object sender, EventArgs<string> e)
{
BeginInvoke((MethodInvoker)delegate()
{
State = ConnectState.NotFound;
MessageBox.Show(e.Value);
});
}
我的問題:
即使設備未連接到計算機,MessageBox也永遠不會出現。
我認為MessageBox應該顯示錯誤消息。 有人可以告訴我怎么了嗎?
注意:
我有一段我認為會觸發ConnectionManager錯誤EventHandler的代碼。
private void LogError(string error)
{
if (Error != null)
Error(this, new EventArgs<string>(error));
}
我也有這段代碼,該錯誤代碼給出了包含LogError方法的字符串的錯誤消息。
int lasterror = Marshal.GetLastWin32Error();
if (lasterror != 0)
LogError("Bluetooth API returned: " + lasterror.ToString());
要么
if (BluetoothSetServiceState(IntPtr.Zero, ref device, ref HumanInterfaceDeviceServiceClass_UUID, BLUETOOTH_SERVICE_ENABLE) != 0)
LogError("Failed to connect to wiimote controller");
另一個提示
更具體地說,我已經有了以下代碼:
public event EventHandler<EventArgs<string>> Error;
和
ConnectionManager.Error += new EventHandler<EventArgs<string>>(ConnectionManager_Error);
還有這個類:
public class EventArgs<T> : EventArgs
{
public T Value
{
get;
set;
}
public EventArgs(T value)
: base()
{
Value = value;
}
}
在單獨的線程中執行代碼的標准BeginInvoke是嗎?
您只需要在GUI線程中使用GUI方法。 要切換到GUI線程,請嘗試使用Control.IsInvokeRequired和Control.Invoke方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.