简体   繁体   English

从另一个线程关闭表单

[英]Close Form from another thread

I have a manager class which launch a Form by using the ShowDialog function. 我有一个管理器类,它使用ShowDialog函数启动一个Form。 Now, I'm starting an event (like a timer) and would like the manager to close the Form while the timer expire. 现在,我正在启动一个事件(如计时器),并希望管理员在计时器到期时关闭表单。

I used 2 classes: 我用了2个班:

namespace ConsoleApplication3
{
class Manager
{
    Timer UpdTimer = null;
    readonly int REFRESH_STATUS_TIME_INTERVAL = 5000;
    Form1 form1;

    public Manager()
    {
    }

    public void ManageTheForms()
    {
        UpdTimer = new Timer(REFRESH_STATUS_TIME_INTERVAL);
        // start updating timer
        //UpdTimer.Interval = REFRESH_STATUS_TIME_INTERVAL;
        UpdTimer.Elapsed += new ElapsedEventHandler(PriorityUpdTimer_Elapsed);
        UpdTimer.Start();

        form1 = new Form1();
        form1.ShowDialog();


    }

    public void PriorityUpdTimer_Elapsed(object source, ElapsedEventArgs e)
    {
        UpdTimer = null;
        form1.closeFormFromAnotherThread();

    }
}
}

Form1 Class: Form1类:

namespace ConsoleApplication3
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {

    }


    delegate void CloseFormFromAnotherThread();

    public void closeFormFromAnotherThread()
    {
        if (this.InvokeRequired)
        {
            CloseFormFromAnotherThread del = new CloseFormFromAnotherThread(closeFormFromAnotherThread);
            this.Invoke(del, new object[] { });
        }
        else
        {
            this.Close();
        }
    }

}

} }

If I'm right you want to close the form when the timer stops. 如果我是对的,你想在计时器停止时关闭表格。

This is the way I do it: 这就是我这样做的方式:

System.Threading.Timer formTimer;

I use a boolean to see if the timer is still active 我使用布尔值来查看计时器是否仍处于活动状态

public Boolean active { get; set; }

Create this function: 创建此功能:

public void timerControl()
{
  if (!active) formTimer = new System.Threading.Timer(new TimerCallback(TimerProc));
  try
  {
    formTimer.Change(REFRESH_STATUS_TIME_INTERVAL, 0);
  }
  catch {}
  active = true;
}

To complete the timer you need the function TimerProc, which is called when there is a new timer created: 要完成计时器,您需要使用TimerProc函数,该函数在创建新计时器时调用:

private void TimerProc(object state)
{
  System.Threading.Timer t = (System.Threading.Timer)state;
  t.Dispose();
  try
  {
    CloseScreen();
  }
  catch{}
}

To make it easier for me to program I made the function CloseScreen(): 为了让我更容易编程,我创建了CloseScreen()函数:

public void CloseScreen()
{
  if (InvokeRequired)
  {
    this.Invoke(new Action(CloseScreen), new object[] { });
    return;
  }
  active = false;
  Close();
}

Put all these functions in your form class and just use timerControl to activat the timer. 将所有这些函数放在表单类中,然后使用timerControl激活计时器。 You can choose to access it from your Manager class: Form1.TimerControl(); 您可以选择从Manager类访问它: Form1.TimerControl(); Or put it in an eventhandler, succes! 或者把它放在一个事件处理程序中,成功!

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

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