Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have made a timer control in
C#
main()
{
  System.Timers.Timer aTimer = new System.Timers.Timer();
  aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
  aTimer.Start();
  aTimer.Interval = 1000;
  aTimer.Enabled = true;
}

and call it
C#
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
  try
  {
    foreach (System.Int32 i in Enum.GetValues(typeof(Keys)))
    {
      if (GetAsyncKeyState(i) == -32767)
      {
        if (Keys.Control == Control.ModifierKeys) Form1.crtl = 1;
        if (Keys.K == (Keys)i) Form1.k = 1;
      }
      if (Form1.k == 1 && Form1.crtl == 1)
      {
        //i am not able to access atimer here?
        string response = ShowDialog("Password Required: ", "Password");
                        
        if (response.Equals("###########"))
        {
          Form1 frm = new Form1();
          frm.Show();
          Form1.k = 0;
          Form1.crtl = 0;
        }
      }
      else
      {
        Form1.k = 0;
        Form1.crtl = 0;
      }
    }
  }
  catch (Exception)
  {
  }
  finally
  {
  }
}

but in this case my show dialog form is open but again and again can any one help for this
my simple task is after show Dialog my timer will be stop
Posted
Updated 10-Mar-15 20:57pm
v3

The problem is that the timer event will be fired every second so a form will pop every second...
If you want to wait for user input upon timer event (which is VERY bad idea!!!), then stop the timer when entering the event handler and then restart if need when leaving the event handler (in such a case I ask myself why the timer at the first place)...
 
Share this answer
 
Comments
Aditya Chauhan 11-Mar-15 3:10am    
as you see i want to open a form with a combination of keys?
if i am not using timer then the keys combination is not wrking proper
Kornfeld Eliyahu Peter 11-Mar-15 3:11am    
I do not understand your goal, so I can not suggest anything...
(for me your code looks like a big mess...)
Aditya Chauhan 11-Mar-15 3:35am    
Hello Kornfeld .,
as i already explained i want to open a form with a key combination in program.cs
if you have any alternative of timer then tell me .,how can call a form with a key combination
Your code as shown never stops the Timer. Also, to use a System.Timers.Timer and raise an event on a UI thread, you must set the System.Timers.Timer 'SynchronizeObject property.

Here's an example that is based on working code in a WinForms Application:

0. create a reference to a new 'SecondForm:
C#
private SecondForm secondForm = new SecondForm();
1. over-ride OnKeyDown in the main Form code-behind so when Control-K is pressed you present the second Form:
C#
protected override void OnKeyDown(KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.K)
    {
        if (secondForm.ShowDialog() == DialogResult.OK)
        {
            MessageBox.Show("Valid Login");
        }
        else
        {
            MessageBox.Show("Invalid Login");
        }

        e.Handled = true;
        return;
    }

    base.OnKeyDown(e);
}
2. Here's a sample of the code in the 'SecondForm ... which is shown modally ... that uses a System.Timers.Timer to close the Form after 15 seconds:
C#
namespace YourNameSpace
{
    public partial class SecondForm : Form
    {
        public SecondForm()
        {
            InitializeComponent();
        }

        private void SecondForm_Load(object sender, EventArgs e)
        {
            TheTimer = new System.Timers.Timer();

            // required in order to stop the Timer and return
            // control to the UI Thread !
            TheTimer.SynchronizingObject = this;

            TheTimer.Elapsed += TheTimer_Elapsed;
            TheTimer.Enabled = true;

            timerStart = DateTime.Now;
            timerEnd = timerStart.AddSeconds(timeLimit);

            TheTimer.Start();
            TheTimer.Interval = 1000;
        }

        // how many seconds to keep the Form open ?
        private int timeLimit = 15;

        private System.Timers.Timer TheTimer;
        private DateTime timerStart;
        private DateTime timerEnd;

        private void TheTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (e.SignalTime >= timerEnd)
            {
                TheTimer.Stop();
                this.DialogResult = DialogResult.Abort;
                this.Close();
            }
        }

        private void CloseThisForm()
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        // if the user closes the Form by the ControlBox
        private void SecondForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason != CloseReason.UserClosing)
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }
    }
}
Notes:

1. note the use of the 'DialogResult returned when the 'SecondForm is closed in the main Form.

2. note the use of 'e.CloseReason in the 'SecondForm's FormClosing event handler.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900