I"m trying to update a value, passed to me by ref, in an async method callback.
// this Main method parameters are given and can not be changed.
public static void Main(ref System.String msg)
{
// here we should invoke an async code,
// which updates the msg parameter.
}
Now, I know you can not pass ref values to async methods. But I would still like to update that ref value, somehow, without blocking my UI thread. sounds unreasonable, to me, that it can not be done.
What I tried:
// Our entry point
public static void Main(ref System.String msg)
{
Foo(msg);
}
// calls the updater (can't use 'await' on my entry point. its not 'async method')
static async void Foo(ref System.String m)
{
var progress = new Progress<string>(update => { m = update; });
await Task.Run(() => MyAsyncUpdaterMethod(progress));
}
// update the variable
static void MyAsyncUpdaterMethod(IProgress<string> progress)
{
Thread.Sleep(3000);
if (progress != null)
{
progress.Report("UPDATED");
}
}
Obviously this won't work due to not being able to the msg
parameter out of scope for async method lambda expressions. My question is: what will? How can this be achieved?
Is it possible to set a global static variable which will hold the ref param, and use that in the callback instead ?