I have two function calls that are run in my code, and when both are done, the function ProcessFiles is run. Like this:
byte[] file1 = doSomething();
byte[] file2 = doSomethingElse();
ProcessFiles(file1, file2);
These two functioncalls DoSomething
and DoSomethingElse
are completely seperate, and I was thinking of running these in a different thread so they are run simultaneously.
However, I need to process the results (file1 and file2) when both are done.
I guess async await (or its VB.NET equivalent) is the way to go here, but for the life of me, I can't find any good examples that showcase this. Maybe I'm using the wrong search queries, but so far I haven't been able to get a good example. Can anyone point me in the right direction please?
Yes, you can do this easily with async/await. For example:
// If you don't have async equivalents, you could use Task.Run
// to start the synchronous operations in separate tasks.
// An alternative would be to use Parallel.Invoke
Task<byte[]> task1 = DoSomethingAsync();
Task<byte[]> task2 = DoSomethingElseAsync();
byte[] file1 = await task1;
byte[] file2 = await task2;
ProcessFiles(file1, file2);
The important thing is that you don't await either task until you've started both of them.
See more on this question at Stackoverflow