• Task多线程进行多进程


    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace ConsoleApp
    {
    class Program
    {
    [DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
    public static extern Int32 GetCurrentWin32ThreadID();
    static void Main(string[] args)
    {
    int maxConnectionCount = 1;
    string[] urlarr = File.ReadAllLines(@"D:sitecheckurllist.txt");
    int num = urlarr.Length;
    var SiteCheck = new SiteCheck();
    SiteCheck.Run(urlarr, num, maxConnectionCount);
    }
    
    }
    
    public class SiteCheck
    {
    
    Random rand = new Random();
    Queue<int> queueUrl = new Queue<int>();
    int EachCount = 1;
    int Cur = 0;
    
    int LoadData(int Max)
    {
    if (Cur >= Max)
    {
    //when Cur>=Max,return
    Console.WriteLine("Reached max value!");
    return -1;
    }
    Console.WriteLine("Loading data...");
    for (int i = Cur; i < EachCount + Cur; i++)
    {
    Thread.Sleep(10);
    Console.Write(i + " ");
    //insert data into Queue<Book>
    queueUrl.Enqueue(i);
    }
    Console.WriteLine("");
    Cur += EachCount;
    Console.WriteLine("Data loaded successful,{0}...", queueUrl.Count);
    return Cur;
    }
    
    public void Run(string[] urlarr, int Max, int maxConnectionCount)
    {
    int cur = LoadData(Max);
    
    Process process = new Process();
    Console.WriteLine("Tasks have started...");
    do
    {
    
    if (cur == -1)
    {
    break;
    }
    else
    {
    cur = queueUrl.Count;
    }
    Task[] tasks = new Task[maxConnectionCount];
    Stopwatch sw = new Stopwatch();
    sw.Start();
    LogHelper.WriteLog(typeof(Program), string.Format("StartTaskTime taskCount{0}:", maxConnectionCount) + DateTime.Now.ToString());
    for (int i = 0; i < maxConnectionCount; i++)
    {
    process = new Process();
    
    tasks[i] = Task.Factory.StartNew(()
    =>
    {
    process.StartInfo.FileName = string.Format(@"D:siteCheckpublish{0}lphash.exe", i); //准备启动lphash
    process.StartInfo.Arguments = urlarr[cur - maxConnectionCount + i];
    process.Start();
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = true;
    });
    Thread.Sleep(500);
    
    }
    process.WaitForExit();
    sw.Stop();
    LogHelper.WriteLog(typeof(Program), string.Format("endTaskTime taskCount{0}:", maxConnectionCount) + DateTime.Now.ToString());
    
    
    cur =LoadData(Max);
    
    } while (queueUrl.Count > 0);
    
    //when Cur>=Max,break the while loop
    Console.WriteLine("All tasks have completed.");
    
    Console.ReadKey();
    }
    }
    }
    

      

  • 相关阅读:
    体验用yarp连接websocket
    从 ASP.NET Core 5.0 迁移到.NET 6
    对接网易云信音视频2.0呼叫组件集成到vue中,实现web端呼叫app,视频语音通话。
    .NET6 WebAPI 自定义过滤器
    .NET6 WebApi 获取访问者IP地址
    .NET6 部署到 IIS
    .NET6 WebApi JSON传到前台默认变成小驼峰
    开发环境 测试环境 生产环境
    .NET6 WebApi 使用 log4net
    .NET6 WebApi 解决跨域问题
  • 原文地址:https://www.cnblogs.com/c-x-a/p/7463917.html
Copyright © 2020-2023  润新知