Reputation:
I'm looking for a way to start a method as soon as another has finished. So this is the idea:
1st Parallel.Invoke
will start, as soon as for example GetGroups()
is done I would like to start InsertGroup()
and after that Insert_EventGroups()
.
Parallel.Invoke(() => { GetGroup(); },
() => { GetProfile(); });
Parallel.Invoke(() => { InsertGroup(); },
() => { InsertProfile(); });
Parallel.Invoke(() => { Insert_EventGroups(); },
() => { Insert_GroupProfiles(); });
How can I do this?
Upvotes: 2
Views: 140
Reputation: 24545
If these methods are non-task returning then the correct approach is to simply put them in the order you'd like. By default, C# will execute sequentially so the order of the method calls are the order in which they are to execute.
private void PerformGroupUpdate()
{
GetGroup();
InsertGroup();
Insert_EventGroups();
}
private void PerformProfileUpdate()
{
GetProfile();
InsertProfile();
Insert_GroupProfiles();
}
If you were to use Parallel.Invoke
it would run these methods in parallel and not necessarily keep the order. So you could now call the two perform methods above to execute them as you see fit. Additionally, if you'd like them to run concurrently that could be achieved by using the Parallel.Invoke
and passing in these two new methods.
Parallel.Invoke(() => PerformGroupUpdate(), () => PerformProfileUpdate());
If these methods were async
methods, then you could use Task.WhenAll
:
private async Task PerformGroupUpdateAsync()
{
await GetGroup();
await InsertGroup();
await Insert_EventGroups();
}
private async TaskPerformProfileUpdateAsync()
{
await GetProfile();
await InsertProfile();
await Insert_GroupProfiles();
}
private async Task InvokeAllAsync()
=> await Task.WhenAll(PerformGroupUpdateAsync(), TaskPerformProfileUpdateAsync());
Upvotes: 3
Reputation: 6744
Just call them sequentially, if you want multi threading for group and profile you can do something like the below (renaming the methods to something more appropriate):
Parallel.Invoke(() => { GroupStuff(); },
() => { ProfileStuff(); });
void GroupStuff()
{
GetGroup();
InserGroup();
Insert_EventGroups();
}
void ProfileStuff()
{
GetProfile();
InserProfile();
Insert_GroupProfiles();
}
Upvotes: 4