Home > database >  How do I make a windows form close after 30 minutes? | Visual Studio C# Windows Forms App .Net Frame
How do I make a windows form close after 30 minutes? | Visual Studio C# Windows Forms App .Net Frame

Time:02-28

I want a code that makes 'Form 2' close after 30 minutes and 'Form 1' to show up.

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();

CodePudding user response:

One method is to setup a Timer and Action.

Action in your form

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

In the same form, your altered Click event

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);
}

Create a new class named ActionContainer in the same project.

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

Now for the worker class named TimerHelper. Note the event Message is completely optional, used more so to see when code is executed. Change the namespace to your project's namespace.

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();
        }
    }
}

CodePudding user response:

Another example, simply moving the second form and timer declarations out to form/class level in the first form:

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();
    }
}
  • Related