0

I have created a console app in which i want to trigger multiple links at a specific time.after searching I have done something like this:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace cronjob_Test_App
{
    class Program
    {
         static void Main(string[] args)
        {
            StartProcess();
        }
        public static void StartProcess()
        {
            // Process.Start("https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe");

            var psi = new ProcessStartInfo("chrome.exe");
            string a, b;
            a = "https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe";
            b = "https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe";
            psi.Arguments = a;
            Process.Start(psi);
            psi.Arguments = b;
            Process.Start(psi);
 }

    }
}

it starts all the links simultaneously.I want the first link to complete and then start the second one.how can I do it or if there is some other good way please suggest. I am using windows scheduler along with this console app to start the console app at a specific time.

Sadia
  • 171
  • 1
  • 5
  • 25
  • Why will you do that? Did you mind use `WebClint `class? – D-Shih Jul 20 '18 at 07:15
  • if there is a way doing it with web-client please guide in it.i can do it with web client as well. – Sadia Jul 20 '18 at 07:18
  • I wrote an answer you can try it. – D-Shih Jul 20 '18 at 07:24
  • You can't do it this way. You have no real means to schedule those downloads (well, you could, but it becomes absurdly complicated for nothing). As already suggested, use [WebClient.DownloadFile()](https://msdn.microsoft.com/en-us/library/ez801hhe(v=vs.110).aspx), it was built more ol less for this. See the example there. Also (one of the many): [How to download a file from a URL](https://stackoverflow.com/questions/307688/how-to-download-a-file-from-a-url-in-c). – Jimi Jul 20 '18 at 07:49
  • thanks let me try this – Sadia Jul 20 '18 at 07:53

2 Answers2

1

You can try this. use use Process.Start and set url as second parameter.

string a = "https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe";
string b = "https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe";
Process.Start("chrome.exe", a);
Process.Start("chrome.exe", b);
D-Shih
  • 44,943
  • 6
  • 31
  • 51
1

Use the Process.WaitForExit() Method

var psi = new ProcessStartInfo("chrome.exe");
string a, b;
a = "http://www.google.com/";
b = "http://www.bing.com/";
psi.Arguments = a;
var p1=  Process.Start(psi);
p1.WaitForExit();
psi.Arguments = b;
var p2 = Process.Start(psi);
p2.WaitForExit();
Console.ReadLine();

Also you can add a time delay to the method that takes (int) milliseconds as parameter.

example : p1.WaitForExit(500)

PS : The process won't wait for the entire web page to load.

Edited :

If you are trying to download a file then please make use of WebClient

using (WebClient client = new WebClient())
{              
    Task taskA = Task.Factory.StartNew(() => client.DownloadFile("https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe",
                                    @"F:\Installer.exe"));
    taskA.Wait();
    Console.WriteLine("Task A has completed.");
    Task taskB = Task.Factory.StartNew(() => client.DownloadFile("https://notepad-plus-plus.org/repository/7.x/7.5.7/npp.7.5.7.Installer.exe",
                                    @"F:\Installer.exe"));
    taskA.Wait();
    Console.WriteLine("Task B has completed.");

}
IteratioN7T
  • 363
  • 8
  • 21
  • your response is positive but the WaitforExit() is not working properly it just waits for few seconds and the second link starts working.is there any way that second link doesn't start until the first one is finished? – Sadia Jul 20 '18 at 10:16
  • @SadiaRashid thats exactly what i mentioned in the answer, it will not wait until the entire page is loaded/your file is being downloaded, that is entirely different thing that is handled by the chrome.exe itself – IteratioN7T Jul 20 '18 at 10:21
  • @SadiaRashid i have updated the answer, the edit might help you. – IteratioN7T Jul 20 '18 at 10:45
  • @SadiaRashid glad to help, if this has solved your problem then please mark it as the accepted answer! :) – IteratioN7T Jul 20 '18 at 10:50