Created
October 30, 2018 11:34
-
-
Save hkucuk/7a44569835a427ac097d5931820a64db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static List<T>[] Partition<T>(List<T> list, int totalPartitions) | |
{ | |
if (list == null) | |
throw new ArgumentNullException("list"); | |
if (totalPartitions < 1) | |
throw new ArgumentOutOfRangeException("totalPartitions"); | |
List<T>[] partitions = new List<T>[totalPartitions]; | |
int maxSize = (int)Math.Ceiling(list.Count / (double)totalPartitions); | |
int k = 0; | |
for (int i = 0; i < partitions.Length; i++) | |
{ | |
partitions[i] = new List<T>(); | |
for (int j = k; j < k + maxSize; j++) | |
{ | |
if (j >= list.Count) | |
break; | |
partitions[i].Add(list[j]); | |
} | |
k += maxSize; | |
} | |
return partitions; | |
} | |
private static string Serialize<T>(T obj) | |
{ | |
using (var stream = new MemoryStream()) | |
{ | |
var formatter = new BinaryFormatter(); | |
formatter.Serialize(stream, obj); | |
stream.Flush(); | |
stream.Position = 0; | |
return Convert.ToBase64String(stream.ToArray()); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
string fileName = "urllist.txt"; | |
int subProcessCount = 10; | |
var urlList = File.ReadAllLines(fileName).ToList(); | |
var jobList = Partition(urlList, subProcessCount); | |
var workingDirectory = Environment.CurrentDirectory; | |
var subProcessName = "ChildApp.exe"; | |
Console.WriteLine("Start"); | |
Parallel.For(0, subProcessCount, i => | |
{ | |
var arg = new ProcessArg(); | |
arg.UrlList = jobList[i]; | |
var argString = Serialize(arg); | |
Console.WriteLine("Child process {0} started", i); | |
CreateChildProcess(workingDirectory, subProcessName, argString); | |
}); | |
Console.WriteLine("All processes are completed"); | |
Console.Read(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
Console.Read(); | |
} | |
private static void CreateChildProcess(string workingDirectory, | |
string jobProcessName, string jobProcessArg) | |
{ | |
var process = new Process(); | |
process.StartInfo.FileName = jobProcessName; | |
process.StartInfo.WorkingDirectory = workingDirectory; | |
process.StartInfo.CreateNoWindow = true; | |
process.StartInfo.Arguments = jobProcessArg; | |
process.Start(); | |
process.WaitForExit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment