none
C# "System.InvalidOperationException" Trying to set button visible inside a timer method (noob) RRS feed

  • Question

  • My program crashes when 5 seconds hits. I'm just practicing and trying to figure out why I can't change the button to visible with a timer. I tried it with private in the first place.

    namespace Practice
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                button1.Visible = false;
                System.Timers.Timer aTimer = new System.Timers.Timer();
                aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
                aTimer.Interval = 5000;
                aTimer.Enabled = true;
            }
            public void OnTimedEvent(object source, ElapsedEventArgs e)
            {
                button1.Visible = true;
            }
        }
    }



    • Edited by Armqus Saturday, October 8, 2016 11:51 AM
    Saturday, October 8, 2016 11:36 AM

Answers

  • There is another kind of timers which is more suitable in these circumstances, I think:

    private void Form1_Load( object sender, EventArgs e )
    {
    	button1.Visible = false;
    	var aTimer = new System.Windows.Forms.Timer();
    	aTimer.Tick += OnTimedEvent;
    	aTimer.Interval = 5000;
    	aTimer.Enabled = true;
    }
    
    private void OnTimedEvent( object sender, EventArgs e )
    {
    	var aTimer = (System.Windows.Forms.Timer)sender;
    	aTimer.Stop();
    	button1.Visible = true;
    }

    This timer can be easily inserted and configured in the Form Designer, where it can be found in Components group of Toolbox.

    The previous timer was based on multi-threading. If you are interested in solving the problem of threads: https://msdn.microsoft.com/en-us/library/ms171728%28v=vs.110%29.aspx




    • Edited by Viorel_MVP Saturday, October 8, 2016 6:22 PM
    • Marked as answer by Armqus Sunday, October 9, 2016 1:42 PM
    Saturday, October 8, 2016 6:12 PM

All replies

  • There is another kind of timers which is more suitable in these circumstances, I think:

    private void Form1_Load( object sender, EventArgs e )
    {
    	button1.Visible = false;
    	var aTimer = new System.Windows.Forms.Timer();
    	aTimer.Tick += OnTimedEvent;
    	aTimer.Interval = 5000;
    	aTimer.Enabled = true;
    }
    
    private void OnTimedEvent( object sender, EventArgs e )
    {
    	var aTimer = (System.Windows.Forms.Timer)sender;
    	aTimer.Stop();
    	button1.Visible = true;
    }

    This timer can be easily inserted and configured in the Form Designer, where it can be found in Components group of Toolbox.

    The previous timer was based on multi-threading. If you are interested in solving the problem of threads: https://msdn.microsoft.com/en-us/library/ms171728%28v=vs.110%29.aspx




    • Edited by Viorel_MVP Saturday, October 8, 2016 6:22 PM
    • Marked as answer by Armqus Sunday, October 9, 2016 1:42 PM
    Saturday, October 8, 2016 6:12 PM
  • That worked out. Thanks!

    I'll check out multithreading later.

    Sunday, October 9, 2016 1:42 PM
  • Since you're experimenting, another thing you could play around with is await Task.Delay(). To use an await though, your method (Form1_Load() in your case) needs to have an async keyword:

    private async void Form1_Load(object sender, EventArgs e)
    {
        button1.Visible = false;
        await Task.Delay(5000);
        button1.Visible = true;
    }

    Note that nothing else can be put after that Task.Delay (because it won't run until after the 5 second delay), so it's not a Timer at all. Just something else that you might want to learn about (the rest of the UI is responsive, which is important).


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, October 9, 2016 5:03 PM