Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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.
Posted
Updated 28-Apr-16 23:19pm

1 solution

That's not going to have any real effect - in fact it may slow things down as you are creating even more threads that you were (the new exe executions will also be threads).
The problem is that you can only simultaneously execute as many threads in a system as you have free cores: if you have a four core processor then you can genuinely execute four threads simultaneously - if you add a fifth, then one thread in the system will get halted until a core is available (either by a thread ending, or by it's time slice being used up). Starting 100 threads does not mean that 100 tasks will happen at the same time (though it can appear that way as Windows is a pre-emptive multitasking system and when a thread has used its processing quota it gets suspended to give another thread a chance).
And starting more threads than you can process generally adds to the total processing time as the task switching and management also adds overhead.
 
Share this answer
 
Comments
[no name] 29-Apr-16 8:17am    
Hi,

If we make divide 100 concurrent tasks with 10 tasks per process (.exe), there would be less chance of timeout (that generally does timeout when we have single exe processing 100 tasks with 100 threads) ,because 10 processes will execute the task faster than 1 exe with 100 threads..

Please suggest

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900