Note that there are some explanatory texts on larger screens.

plurals
  1. POSynchronous Event Handler thread execution in non-Win Form C# app
    text
    copied!<p>I'm running a number of time\CPU intensive processes (TimeExpensive type) one after another. The Main thread (A) starts TimeExpensive process in another thread (B) asynchronously and becomes inactive. On Completion, Thread B fires caller's completion handler synchronously and starts next TimeExpensive process in thread B. A new thread (C) is created but After starting C, B finishes. So for n processes, n threads are created and most of time, they don't co-exist. </p> <p>One may wish its implementation in linear single threaded way, but TimeExpensive is implemented by third party and while it runs, it uses all system cores and runs for hours. </p> <pre><code>//This will run as console app class Program { static void Main(string[] args) { new Program().StartJobs(); } void StartJobs() { Main mainJob = new Main(); mainJob.MainCompletionEvent += new Action&lt;object, EventArgs&gt;(mainJob_MainCompletionEvent); mainJob.Start(); } void mainJob_MainCompletionEvent(object sender, EventArgs e) { //if(success) Environment.Exit(0); } } class Main { int processCounter = 0; public event Action&lt;object, EventArgs&gt; MainCompletionEvent; public void Start() { //...do other important tasks here... processCounter++; TimeExpensive te = new TimeExpensive(); te.CompletionEvent += new Action(TimeExpensive_CompletionHandler); Thread aThread = new Thread(te.Run); aThread.IsBackground = false; aThread.Name = "TimeExpensive Thread: " + processCounter; aThread.Start(); } void TimeExpensive_CompletionHandler() { Console.WriteLine("current Thread Name: " + Thread.CurrentThread.Name); //Start another Process In Background if if (processCounter &lt; 5) { Start(); } else { Console.ReadKey(); if (JobCompletionEvent != null) JobCompletionEvent(this, new EventArgs()); } } } class TimeExpensive { public event Action CompletionEvent; public void Run() { //doing time expensive task //... //when finish Notify completion Handler... if (CompletionEvent != null) { CompletionEvent(); } } } //Output current Thread Name: TimeExpensive Thread: 1 current Thread Name: TimeExpensive Thread: 2 current Thread Name: TimeExpensive Thread: 3 current Thread Name: TimeExpensive Thread: 4 current Thread Name: TimeExpensive Thread: 5 </code></pre> <p>Above implementation mimics the behavior I described. The thing that is bugging me is the Event Handler running synchronously until the next thread starts and during this time, it's doing lots of tasks that it's not designed for. </p> <p>Not sure if this is good, Is there a way I can go back to thread A while in Thread B's completion handler? or Should I better start event handler execution using another delegate.BeginInvoke?</p> <p>I wish doing it with simple yet safe approach. Any help is greatly appreciated. </p> <p>P.S I read lots of posts but no one was dealing well with this scenario.</p> <h2>Edit</h2> <p>Static main is added to show how to kick start this code in console app. Remember one can also create UI to start "Main" job. It'll definitely create BackgroundWorker thread to create mainJob object and run it. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload