Builder now support specifying max job count, less than 0 will use processor count.
This commit is contained in:
@@ -10,7 +10,7 @@ namespace Progrart.Cmd
|
||||
{
|
||||
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
|
||||
string? project_file = null;
|
||||
bool isParallel = false;
|
||||
int jobCount = 1;
|
||||
string? configuration = null;
|
||||
for (int i = 0; i < args.Length; i++)
|
||||
{
|
||||
@@ -18,7 +18,19 @@ namespace Progrart.Cmd
|
||||
switch (item)
|
||||
{
|
||||
case "-j":
|
||||
isParallel=true;
|
||||
i++;
|
||||
if (i >= args.Length)
|
||||
{
|
||||
jobCount = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!int.TryParse(args[i], out jobCount))
|
||||
{
|
||||
jobCount = -1;
|
||||
i--;
|
||||
}
|
||||
|
||||
break;
|
||||
case "-c":
|
||||
case "--config":
|
||||
@@ -58,16 +70,7 @@ namespace Progrart.Cmd
|
||||
{
|
||||
Environment.Exit(0);
|
||||
};
|
||||
Console.WriteLine("Start build...");
|
||||
//Task.Run(async () =>
|
||||
//{
|
||||
//});
|
||||
//while (true)
|
||||
//{
|
||||
// Thread.Sleep(1000);
|
||||
//}
|
||||
|
||||
await builder.Build(configuration, isParallel);
|
||||
await builder.Build(configuration, jobCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,41 +40,38 @@ namespace Progrart.Core.ProjectSystem
|
||||
img.DrawingCore.ToData().SaveTo(img_stream);
|
||||
img_stream.Flush();
|
||||
}
|
||||
public async Task Build(string targetConfig, bool isParallel = false)
|
||||
public async Task Build(string targetConfig, int maxJobCount = 1)
|
||||
{
|
||||
int finalJobCount= Math.Max(maxJobCount < 0 ? Environment.ProcessorCount : maxJobCount, 1);
|
||||
foreach (var config in project.Configurations)
|
||||
{
|
||||
Console.WriteLine(config.Name);
|
||||
if (config.Name == targetConfig)
|
||||
{
|
||||
int index = 0;
|
||||
List<Task> tasks = new List<Task>();
|
||||
//List<Task> tasks = new List<Task>();
|
||||
if (finalJobCount == 1)
|
||||
{
|
||||
foreach (var item in config.Items)
|
||||
{
|
||||
if (!isParallel)
|
||||
{
|
||||
|
||||
await Execute(config, item);
|
||||
index++;
|
||||
OnProgressUpdate?.Invoke(config.Items.Count, index);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
var t = Task.Run(async () =>
|
||||
var options = new ParallelOptions
|
||||
{
|
||||
MaxDegreeOfParallelism = finalJobCount
|
||||
};
|
||||
|
||||
await Parallel.ForEachAsync(config.Items, options, async (item, token) =>
|
||||
{
|
||||
index++;
|
||||
int i = index;
|
||||
await Execute(config, item);
|
||||
OnProgressUpdate?.Invoke(config.Items.Count, i);
|
||||
});
|
||||
tasks.Add(t);
|
||||
}
|
||||
|
||||
}
|
||||
if (isParallel)
|
||||
{
|
||||
await Task.WhenAll(tasks);
|
||||
int currentCount = Interlocked.Increment(ref index);
|
||||
OnProgressUpdate?.Invoke(config.Items.Count, currentCount);
|
||||
});
|
||||
}
|
||||
OnCompleted?.Invoke();
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user