×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

Use mainform invoke method to call subForm.Show() instead of in the thread

For example

Your current code in the method of the thread looks like

public void ThreadMethod()
{
....
SubForm form = new SubForm();
...
form.Show();
...
}

Rewrite it to

public void ThreadMethod()
{
...
CallFormShow(form)
...
}

protected delegate void CallFormShowDelegate();
protected void CallFormShowInvoke()

protected void CallFormShow()
{
mainForm.Invoke(new CallFormShowDelegate(CallFormShowInvoke), new object[]{});
}

protected void CallFormShowInvoke()
{
SubForm form = new SubForm();
...
form.Show();
}

Hope this could help you.
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / C# 求助: 如何在线程(不是主线程) 中显示窗体?
    我在主程中创建了一个工作线程,
    在工作线程中,当遇到某个特定的条件时, 它将创建一个FORM 对象, 并调用的FORM 的SHOW 方法显示.
    但当调用SHOW 方法时, 窗体被冻结住了.
    • 你用ShowDialog()试试
    • Use mainform invoke method to call subForm.Show() instead of in the thread
      For example

      Your current code in the method of the thread looks like

      public void ThreadMethod()
      {
      ....
      SubForm form = new SubForm();
      ...
      form.Show();
      ...
      }

      Rewrite it to

      public void ThreadMethod()
      {
      ...
      CallFormShow(form)
      ...
      }

      protected delegate void CallFormShowDelegate();
      protected void CallFormShowInvoke()

      protected void CallFormShow()
      {
      mainForm.Invoke(new CallFormShowDelegate(CallFormShowInvoke), new object[]{});
      }

      protected void CallFormShowInvoke()
      {
      SubForm form = new SubForm();
      ...
      form.Show();
      }

      Hope this could help you.
      • 这个可以工作.
    • You should not show UI on non-UI thread.