Sixth Attempt, Await Asynchronously |
In our prior attempts, we accessed the Result property of the Task returned by the ExecuteAsync method. Is there a downside of calling Data Conveyer this way?
Imagine again a long running process being carried out by Data Conveyer. While waiting for the completion of the process, our application is blocked and remains unresponsive. What can be done about it?
In order to avoid blocking and truly take advantage of the asynchronous nature of Data Conveyer, we need to call the ExecuteAsync method from within an asynchronous method and await a completion of the task returned by this call.
There are many ways to launch an asynchronous method. The easiest one (although not very robust) is to define a method with a signature similar to: async Task MyProcessAsync() and call it synchronously like this: MyProcessAsync().Wait().
Note |
---|
If you have followed the pages of this primer so far using a Console Application project, this may a good moment to pause and consider a different project type. Majority of programming frameworks have built-in support for asynchronous methods. For example, in a Windows Forms Application, we can provide an asynchronous handler of a button click event like this: async void myButton_Click(object sender, EventArgs e). |
using Mavidian.DataConveyer; using Mavidian.DataConveyer.Orchestrators; var config = new OrchestratorConfig() { InputDataKind = KindOfTextData.Delimited, InputFileName = "input.csv", HeadersInFirstInputRow = true, OutputDataKind = KindOfTextData.Keyword, OutputFileName = "output.kw" }; ProcessResult rslt; using (var orchtr = OrchestratorCreator.GetEtlOrchestrator(config)) { rslt = await orchtr.ExecuteAsync(); } if (rslt.CompletionStatus == CompletionStatus.IntakeDepleted) { Console.WriteLine($"Successfully converted {rslt.RowsWritten} records!"); //In a Windows Forms Application, provide feedback using a message box or text box instead }
We now have the best of both worlds: our application is simple and responsive as well. Simple, because we delegated all the hard work to Data Conveyer. Responsive, because we called Data Conveyer asynchronously from within an asynchronous method.