ngm_code
ngm_code

Reputation: 141

How does one manage a mix of synchronous and asynchronous processes in C#?

Desired Outcome:

Define a mix of synchronous and asynchronous method relationships.

Example

Assume 4 methods, each containing a single process where:

  1. Method.ProcessA1 must start and finish before Method.ProcessA2 is invoked (synchronous relationship).
  2. Method.ProcessB1 must start and finish before Method.ProcessB2 is invoked (synchronous relationship).
  3. Method.ProcessA and Method.ProcessB may start and finish irrespective of each other and in parallel (asynchronous relationship).

The flow chart shown in the following link more clearly illustrates the desired synchronicity. Processes flow chart is like the following:

enter image description here

There is an async method, perhaps its usage would be appropriate here.

Note: I am new to SO and open to advice on writing higher quality questions.

Upvotes: 1

Views: 522

Answers (1)

Greg Bahm
Greg Bahm

Reputation: 648

void ProcessA()
{
    ProcessA1();
    ProcessA2();
}

void ProcessB()
{
    ProcessB1();
    ProcessB2();
}

void LaunchProcesses()
{
    Task aTask = Task.Run((Action)ProcessA); //You could also call use Task.Run(() => ProcessA());
    Task bTask = Task.Run((Action)ProcessB);
    aTask.Wait();
    bTask.Wait();
}

Upvotes: 4

Related Questions