1

i want realize a simple application that in a specific and precise time, with webbrowser control, go to the webpage.

    public partial class Form1 : Form
{
    System.DateTime timeStart = new System.DateTime(2016, 05, 25, 19, 30, 00, 00);
    TimeSpan sub;
    bool timeExpires = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {


        timer1.Interval = 100;
        timer1.Start();

        while(timeExpires)
        {
            webBrowser1.Navigate("https://www.google.it/");
        }

    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        System.DateTime Now = System.DateTime.Now;

        sub = timeStart.Subtract(Now);

        if ((int)sub.TotalSeconds == 0)
        {
            this.timer1.Stop();
            MessageBox.Show("ok, Time is up!");
            timeExpires = true;
        }
        else
        {
            textBox1.Text = sub.ToString();
        }


    }
}

after the timecount, when timer1.stop() is set, the messagebox is show.

But webbrowser don't run .

I know that i use a bool variable timeExpires is an "antiquated" method.

I have two question :

  • where is the best pratice or best way to "notify" at webbrowser or any other thing that countdown is finish and now is time to run.
  • where is problem with this approch(boolean variable)? how I can run with this approch even if isn't the best way?

Thanks so much

rul3z
  • 173
  • 1
  • 16

1 Answers1

1

You main thread is blocked by the while loop, so the messages/events aren't being processed. That way, the value of timeExpires never changes inside the loop. To you understand, you can Application.DoEvents() to force the events to be processed but it might not be a good unless you really understand how this works and how evil it can be.

You should open the browser inside the Timer's Tick event (just like where you're calling MessageBox.Show()) but be carefully on doing too many things on tick event, if you statements take more time to run than the Timer's interval, the Tick event will run again and might mess up everything. So, to solve this, whatever you enter in the Tick event, pause the timer and start again where you're done.

 private void timer1_Tick(object sender, EventArgs e) {
           timer1.Stop(); // prevent event to fire again, until we get some stuff done
           if(timeStart >= DateTime.Now) {
                openBrowser();
           } else {
              timer1.Start();
              textBox1.Text = sub.ToString();
           }
 }
Community
  • 1
  • 1
Jack
  • 16,276
  • 55
  • 159
  • 284
  • ok, thanks. In general , where is the best way to "notify" or signal at other object or method that is in pause and wait that other operation (for example countdown) is finish ?! "hey, i have finish, now if you want, you can run" ... Use a thread(I should learn) or semaphore,monitor or lock ? Where is the best way? Semaphore,monitor or lock is used only if i use thread?! – rul3z May 25 '16 at 18:16
  • 1
    It depends on each case, but for "notify" when something running on external thread is done, we use an `Event` and call it (just like a method) when the job in the thread is done (think `BackgroundWorker.RunWorkerCompleted`). But if you want to block current thread until something is done on another thread, you can use `AutoResetEvent` class: https://msdn.microsoft.com/en-us/library/system.threading.autoresetevent%28v=vs.110%29.aspx Your specific case of open browser after a whie, I think a `Timer` is perfectly fine. If you use `AutoReseteEvent` for example [continue below] – Jack May 25 '16 at 18:27
  • 1
    blocking UI might be annoying to the user, it's avoidable as possible. So whatever a long running task (2-7 seconds, according to MSDN docs) you should run in an external thread. Depending on what you doing, you may use a background worker, async/await (if you it's a IO operation, for example) and so on... – Jack May 25 '16 at 18:28
  • you have been kind, I thanks u. I would like to learn all these things but I turn a little difficult . I know the basics of c # and oop but thread,delegates and events are a bit hard for me. I am using C # 6.0 in a nutshell [book](http://shop.oreilly.com/product/0636920040323.do?green=6A499B1F-3D75-5023-B36E-7F9901F5E7FF&intcmp=af-mybuy-0636920040323.IP) for studing.. If u have link , book or other to suggest me ... I'm happy ! Thanks again ! :) – rul3z May 25 '16 at 18:44
  • 1
    Glad for help :) Thread might be a really difficult in the beginning but just keep studying and everything will look like clear. Check out some code examples too, sometimes it's a boost in the level of understanding. About delegates and events it's really use, check out this article (http://csharpindepth.com/Articles/Chapter2/Events.aspx) and (http://stackoverflow.com/questions/623451/how-can-i-make-my-own-event-in-c). Also the book you're reading, which is already a pretty good one, check out C# in Depth by Jon Skeet – Jack May 26 '16 at 18:01