繁体   English   中英

如何在 30 分钟后关闭 windows 表单? | 视觉工作室 C# Windows Forms App.Net 框架

[英]How do I make a windows form close after 30 minutes? | Visual Studio C# Windows Forms App .Net Framework

我想要一个代码,使“表格 2”在 30 分钟后关闭并显示“表格 1”。

private void button1_Click_1(object sender, EventArgs e)

MessageBox.Show("Thank you for using Crystal X!", "Success");
this.Hide();
CrystalX main = new CrystalX();
main.Show();

(Code to wait 30 minutes)

this.Hide();
Form1 main = new Form1();
main.Show();

一种方法是设置定时器和动作。

表单中的操作

public void FormWork()
{
    this.Hide();
    Form1 main = new Form1();
    main.Show();
    TimerHelper.Stop();
}

以相同的形式,您更改的 Click 事件

private void button1_Click_1(object sender, EventArgs e)
{
    MessageBox.Show("Thank you for using Crystal X!", "Success");
    this.Hide();
    CrystalX main = new CrystalX();
    main.Show();
    TimerHelper.Start(FormWork);
}

在同一项目中新建一个名为 ActionContainer 的 class。

public class ActionContainer
{
    /// <summary>
    /// Action to perform
    /// </summary>
    public Action Action { get; set; } = () => { };
}

现在为名为 TimerHelper 的工作人员 class。 请注意事件消息是完全可选的,更多地用于查看代码何时执行。 将命名空间更改为项目的命名空间。

using System;
using System.Threading;
using Timer = System.Threading.Timer;

namespace WorkingWithTimer.Classes
{
    public class TimerHelper
    {
        /// <summary>
        /// How long between intervals, currently 30 minutes
        /// </summary>
        private static int _dueTime = 1000 * 60 * 30;
        private static Timer _workTimer;
        public static ActionContainer ActionContainer;

        /// <summary>
        /// Text to display to listener 
        /// </summary>
        /// <param name="message">text</param>
        public delegate void MessageHandler(string message);
        /// <summary>
        /// Optional event 
        /// </summary>
        public static event MessageHandler Message;
        /// <summary>
        /// Flag to determine if timer should initialize 
        /// </summary>
        public static bool ShouldRun { get; set; } = true;

        /// <summary>
        /// Default initializer
        /// </summary>
        private static void Initialize()
        {
            if (!ShouldRun) return;
            _workTimer = new Timer(Dispatcher);
            _workTimer.Change(_dueTime, Timeout.Infinite);
        }

        /// <summary>
        /// Initialize with time to delay before triggering <see cref="Worker"/>
        /// </summary>
        /// <param name="dueTime"></param>
        private static void Initialize(int dueTime)
        {
            if (!ShouldRun) return;
            _dueTime = dueTime;
            _workTimer = new Timer(Dispatcher);
            _workTimer.Change(_dueTime, Timeout.Infinite);
        }
        /// <summary>
        /// Trigger work, restart timer
        /// </summary>
        /// <param name="e"></param>
        private static void Dispatcher(object e)
        {
            Worker();
            _workTimer.Dispose();
            Initialize();
        }

        /// <summary>
        /// Start timer without an <see cref="Action"/>
        /// </summary>
        public static void Start()
        {
            Initialize();
            Message?.Invoke("Started");
        }
        /// <summary>
        /// Start timer with an <see cref="Action"/>
        /// </summary>
        public static void Start(Action action)
        {
            ActionContainer = new ActionContainer();
            ActionContainer.Action += action;
            
            Initialize();

            Message?.Invoke("Started");

        }
        /// <summary>
        /// Stop timer
        /// </summary>
        public static void Stop()
        {
            _workTimer.Dispose();
            Message?.Invoke("Stopped");
        }

        /// <summary>
        /// If <see cref="ActionContainer"/> is not null trigger action
        /// else alter listeners it's time to perform work in caller
        /// </summary>
        private static void Worker()
        {
            Message?.Invoke("Performing work");
            ActionContainer?.Action();
        }
    }
}

另一个例子,简单地将第二种形式和计时器声明移出第一种形式的形式/类级别:

private CrystalX main = null;
private System.Windows.Forms.Timer tmr = null;

private void button1_Click(object sender, EventArgs e)
{
    this.Hide();
    if (main == null || main.IsDisposed)
    {
        main = new CrystalX();
        main.FormClosed += Main_FormClosed;
    }
    if (tmr == null)
    {
        tmr = new System.Windows.Forms.Timer();
        tmr.Tick += Tmr_Tick;
        tmr.Interval = (int)TimeSpan.FromMinutes(30).TotalMilliseconds;
    }
    main.Show();
    tmr.Start();
}

private void Main_FormClosed(object sender, FormClosedEventArgs e)
{
    if (tmr != null && tmr.Enabled)
    {
        tmr.Stop();
    }
    this.Show();
    main = null;
}

private void Tmr_Tick(object sender, EventArgs e)
{
    if (main != null && !main.IsDisposed)
    {
        main.Close();
    }
}

暂无
暂无

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

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