代码之家  ›  专栏  ›  技术社区  ›  Tobi

有什么陷阱吗?

  •  2
  • Tobi  · 技术社区  · 16 年前

    this question about exception handling . 我在那里找到的解决方法包括滚动我自己的消息循环。

    [STAThread]
    static void Main() {
      // this is needed so there'll actually an exception be thrown by
      // Application.Run/Application.DoEvents, instead of the ThreadException
      // event being raised.
      Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
    
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
    
      Form form = new MainForm();
      form.Show();
    
      // the loop is here to keep app running if non-fatal exception is caught.
      do {
        try {
          Application.DoEvents();
          Thread.Sleep(100);
        }
        catch (Exception ex) {
          ExceptionHandler.ConsumeException(ex);
        }
      }
      while (!form.IsDisposed);
    }
    

    '应用程序。运行(new MainForm());'

    3 回复  |  直到 8 年前
        1
  •  2
  •   ima    16 年前

    陷阱1:

    Thread.Sleep(100);
    

    从未。使用WaitMessage()。

    否则,可能会推出自己的消息循环,但在您的场景中,这似乎有些毫无意义。

    你也可以检查一下应用程序。运行()代码(例如使用.Net Reflector)。

        2
  •  2
  •   Mike Dimmick    16 年前

    如果要自定义消息处理,请考虑实现IMessageFilter,然后调用Application.AddMessageFilter告诉标准消息泵调用过滤函数。

        3
  •  0
  •   Quibblesome    16 年前

    对。。。我认为有些组件无法使用该代码。他们中的一些人需要生活在一个线程中应用程序。运行有效地接收他们的信息。