简体   繁体   English

为什么此MessageBox没有出现?

[英]Why this MessageBox does not appear?

I Have Code for EventHandler like this below. 我有下面这样的EventHandler代码。

void ConnectionManager_Error(object sender, EventArgs<string> e)
{
    BeginInvoke((MethodInvoker)delegate()
    {
         State = ConnectState.NotFound;
         MessageBox.Show(e.Value);
    });
}

My Problem: 我的问题:

MessageBox never appears even when the device is not connected to the computer. 即使设备未连接到计算机,MessageBox也永远不会出现。

I think that comes MessageBox supposed that show error messages. 我认为MessageBox应该显示错误消息。 Can someone show me what is wrong? 有人可以告诉我怎么了吗?

Note: 注意:

I have this code that I thought would trigger ConnectionManager Error EventHandler. 我有一段我认为会触发ConnectionManager错误EventHandler的代码。

private void LogError(string error)
{
    if (Error != null)
        Error(this, new EventArgs<string>(error));
}

I also have this code that gives an error message containing the string to LogError method. 我也有这段代码,该错误代码给出了包含LogError方法的字符串的错误消息。

int lasterror = Marshal.GetLastWin32Error();
    if (lasterror != 0)
        LogError("Bluetooth API returned: " + lasterror.ToString());

or 要么

 if (BluetoothSetServiceState(IntPtr.Zero, ref device, ref HumanInterfaceDeviceServiceClass_UUID, BLUETOOTH_SERVICE_ENABLE) != 0)
     LogError("Failed to connect to wiimote controller");

Another Hint 另一个提示

To be more specific, I also already have the code below: 更具体地说,我已经有了以下代码:

public event EventHandler<EventArgs<string>> Error;

and

ConnectionManager.Error += new EventHandler<EventArgs<string>>(ConnectionManager_Error);

And also this class: 还有这个类:

public class EventArgs<T> : EventArgs
{
    public T Value
    {
         get;
         set;
    }

     public EventArgs(T value)
            : base()
     {
        Value = value;
     }
}

Is it standard BeginInvoke that executes code in the separate thread? 在单独的线程中执行代码的标准BeginInvoke是吗?

You have to work with GUI methods only in GUI thread. 您只需要在GUI线程中使用GUI方法。 To switch to the GUI thread please try Control.IsInvokeRequired and Control.Invoke methods. 要切换到GUI线程,请尝试使用Control.IsInvokeRequired和Control.Invoke方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM