×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

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.