First off, I am targeting .Net Core 3.1 and C#8.
I want something like this.
public static async Task<MyDataObj> GetData()
{
var dataObj = new MyDataObj();
var args = ArgsHelperFunction(...);
await foreach(string result in RunProcessAsync(args))
{
// Process result and store it in dataObj
}
return dataObj;
}
private static async IAsyncEnumerable<string> RunProcessAsync(string args)
{
await using (var myProcess = new Process())
{
myProcess.StartInfo.FileName = @"path\to\file.exe"
myProcess.StartInfo.Arguments = args;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.ErrorDataReceived += (s, e) =>
{
yield return e.Data;
}
myProcess.Start();
myProcess.BeginErrorReadLine();
process.WaitforExit();
}
}
When I try this setup I get an errors from await foreach(string result in RunProcessAsync(args))
CS8417 'Process': type used in an asynchronous using statement must be implicitly convertible to 'System.IAsyncDisposable' or implement a suitable 'DisposeAsync' method.
and this error from yield return e.Data;
CS1621 The yield statement cannot be used inside an anonymous method or lambda expression
The goal is this. I have an exe that does some stuff and writes information to the error output stream (not sure if that's its real name). I want to take in those writes as they are made, parse them for the information I want and store them in an object for later use.
I am a pretty novice coder and very new to asynchronous coding. I tested the functionality of RunProcessAsync
but in a synchronous manner; where it was called and just wrote all the raw data to the output window without returning any of it to the calling method. That worked just fine. Also, I have gotten a test asyc stream working with IAsyncEnumerable
, but it just used Task.Delay
and returned some integers. Now I am trying to combine the to things and my lack of experience is getting in my way.
Thank you for any help you all might give and for helping to increase skill and knowledge of C#.