Open webpage from console (C #) after 10 seconds

I am writing a console application in C #. How do I open a web page after 10 seconds? I already found something like

System.Diagnostics.Process.Start("http://www.stackoverflow.com")

      

but how to add a timer?

+3


source to share


3 answers


You can choose from the following options depending on the application:



For example:

System.Threading.Thread.Sleep((int)System.TimeSpan.FromSeconds(10).TotalMilliseconds);

      

0


source


If you want to open this page every 10 seconds try this

Timer timer = new Timer();
timer.Interval = 10000;
timer.Tick += timer_Tick;
timer.Start();

void timer_Tick(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("http://www.stackoverflow.com");
    timer.Stop();   //If you don't want to show page every 10 seconds stop the timer once it has shown the page.
}

      



And if you want it to only show the page once, than you can stop the timer using the Stop()

timer class method .

0


source


Since you are trying to open a url in C # using System.Diagnostics.Process.Start

I suggest you read this , I will copy the code posted on this web page in case the link gets broken on the same day:

public void OpenLink(string sUrl)
{
    try
    {
        System.Diagnostics.Process.Start(sUrl);
    }
    catch(Exception exc1)
    {
        // System.ComponentModel.Win32Exception is a known exception that occurs when Firefox is default browser.  
        // It actually opens the browser but STILL throws this exception so we can just ignore it.  If not this exception,
        // then attempt to open the URL in IE instead.
        if (exc1.GetType().ToString() != "System.ComponentModel.Win32Exception")
        {
            // sometimes throws exception so we have to just ignore
            // this is a common .NET bug that no one online really has a great reason for so now we just need to try to open
            // the URL using IE if we can.
            try
            {
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("IExplore.exe", sUrl);
                System.Diagnostics.Process.Start(startInfo);
                startInfo = null;
            }
            catch (Exception exc2)
            {
                // still nothing we can do so just show the error to the user here.
            }
        }
    }
}

      

For suspended execution, use Task.Delay

:

  var t = Task.Run(async delegate
          {
             await Task.Delay(TimeSpan.FromSeconds(10));
             return System.Diagnostics.Process.Start("http://www.stackoverflow.com");
          });

  // Here you can do whatever you want without waiting to that Task t finishes.

  t.Wait();// that is a barrier and the code after t.Wait() will be executed only after t had returned.
  Console.WriteLine("Task returned with process {0}, t.Result); // in case System.Diagnostics.Process.Start fails t.Result should be null 

      

0


source







All Articles