Search code examples
c#.netmultithreadingbackgroundworker

Multiple background worker in chain


i have a sequence of 3 time consuming functions, i would like to execute them in background worker or whatever else tool, my problem is that each function should wait for the precedent function to complete, so this is my pseudo-code :

open_session() // during the execution of this function i like to display a loading-window 

open_session() //this function opens a process another windows app , while opening this app i would like to keep the current app responsive.

close_session()// during the execution of this function i like to display a loading-window 

i imagined to do this using background worker, but do function are asynchrounous.

Any help please


Solution

  • Just start each consecutive worker in the RunWorkerCompleted event handler of the previous one...

    Adressing the downvotes: note that it's a possibility. Whether or not it's a good choice, obviously depends on the context, like always. I don't believe in golden rules such as "it's better to just only use one BGW". I can imagine plenty of circumstances, under which using a single BW for all tasks wouldn't simplify matters (for example, if each operation requires ProgressChanged to be handled differently).

    The OP asked about multiple background workers, not having given enough context to judge what approach is best - for all I know, he could be just theoretically wondering how one can "chain" BWs, in order to gain a better understanding of how BW works.