My Scheduler run each minute and fetch tasks to be executed at that time....
Currently it create as much threads as many tasks , so if there are 100 tasks to be executed at 6:00 AM, it creates 100 threads inside a single taskscheduler.exe process...
Its very time consuming sometimes as well the exe timeouts for long running tasks as it defined in Windows task scheduler.
I want to divide the work-load over multiple exe's running at same instant.
So instead of one process exe (instance) with 100 threads , i want to create a batch of 10 threads and span the work over 10 processes.
This is the current main() method
public static void Main()
{
Logger logger = LogManager.GetCurrentClassLogger();
System.Globalization.CultureInfo vCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
vCulture.DateTimeFormat.FullDateTimePattern = "MM/dd/yyyy hh:mm tt";
vCulture.DateTimeFormat.ShortDatePattern = "MM/dd/yyyy";
vCulture.DateTimeFormat.DateSeparator = "/";
System.Threading.Thread.CurrentThread.CurrentCulture = vCulture;
DateTime ExecutionStartTime = DateTime.Now;
ApiTaskComponent apiTaskComponent = new ApiTaskComponent();
ScheduledTasks.TaskList = apiTaskComponent.GetTaskListToExecute();
long totalTasksToExecute = ScheduledTasks.TaskCount;
long maxBatchSize = totalTasksToExecute; ;
System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();
logger.Info("TotalTasksToExecute : " + totalTasksToExecute.ToString());
logger.Info("MaxBatchSize : " + maxBatchSize.ToString());
logger.Info("Current Process ID: " + current.Id.ToString());
if (totalTasksToExecute > 0)
{
Thread[] thread = new Thread[totalTasksToExecute];
for (long i = 0; i < maxBatchSize; i++)
{
Runtask task = new Runtask();
thread[i] = new Thread(new ThreadStart(task.RunTaskApi));
thread[i].CurrentCulture = vCulture;
thread[i].Name = "Thread_" + current.Id.ToString() + "_" + (i + 1).ToString();
thread[i].Start();
}
for (int j = 0; j < thread.Length; j++)
{
thread[j].Join();
}
}
TimeSpan ElapsedTime = DateTime.Now.Subtract(ExecutionStartTime);
logger.Info(string.Format("Task Execution Done in {0} Hour(s) {1} Minute(s) and {2} Second(s).", ElapsedTime.Hours.ToString(), ElapsedTime.Minutes.ToString(), ElapsedTime.Seconds.ToString()));
Console.WriteLine(string.Format("Task Execution Done in {0} Hour(s) {1} Minute(s) and {2} Second(s).", ElapsedTime.Hours.ToString(), ElapsedTime.Minutes.ToString(), ElapsedTime.Seconds.ToString()));
}
}
Please suggest if this approach will work!!
How can i do this??
What I have tried:
I want to divide the work-load over multiple exe's running at same instant.
So instead of one process exe (instance) with 100 threads , i want to create a batch of 10 threads and span the work over 10 processes.