简体   繁体   中英

raise event in UI thread

I need to raise events in UI thread, all solution I have found are only for full framework: for example very simple solution with GetInvocationList and ISynchronizeInvoke, but NET CF does not support ISynchronizeInvoke. Is there any way to raise events in UI thread for CF? My code is below. I use .NET CF 3.5

class Publisher {
  public event EventHandler OnEventHandler;
  private void OnEvent()
  {
    var handler = OnEventHandler;
    if (handler != null) handler(this, EventArgs.Empty); 
  }
}

I use MethodInvoker , which has to be declared (because it does not exist under the compact framework), then include a link to the parent control (usually the main form):

class Publisher {
  #if PocketPC // MethodInvoker delegate is not declared in CF!
  public delegate void MethodInvoker(); 
  #endif
  private Form _parent;

  public Publisher(Form parent) {
    _parent = parent;
  }

  public event EventHandler OnEventHandler;
  private void OnEvent() {
    if (OnEventHandler != null) {
      MethodInvoker mi = delegate { OnEventHandler(this, EventArgs.Empty); };
      try {
        _parent.BeginInvoke(methInvoker);
      } catch (ObjectDisposedException) {
      } catch (NullReferenceException err) {
        LogError("OnEvent NullReference", err);
      } catch (InvalidOperationException err) {
        LogError("OnEvent InvalidOperation", err);
      }
    }
  }
}

Hopefully, you are able to do something with that. ;)

~Joe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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