簡體   English   中英

如何從我的控制台應用程序打開多個http鏈接?

[英]How to open multiple http links from my console app?

我創建了一個控制台應用程序,其中我想在特定時間觸發多個鏈接。搜索后,我做了如下操作:

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);
 }

    }
}

它會同時啟動所有鏈接。我希望第一個鏈接完成,然后啟動第二個鏈接。我該怎么辦?或者是否有其他好的方法,請提出建議。 我正在使用Windows Scheduler與此控制台應用程序一起在特定時間啟動控制台應用程序。

你可以試試看 使用use Process.Start並將url設置為第二個參數。

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);

使用Process.WaitForExit() 方法

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();

您還可以向方法添加一個時間延遲,該時間延遲以(int)毫秒為參數。

例如: p1.WaitForExit(500)

PS:該過程不會等待整個網頁加載。

編輯

如果您要下載文件,請使用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.");

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM