First of all, I understand that the ref, in and out parameters are forbidden in asynchronous tasks.
However, as I have this need, I’m trying to find the cleanest, simplest and most effective solution.
And unintentionally I’ve created a case that seems to work (I realised later). But I need your help to better understand why this case works as opposed to a simple string.
I’m in C# / WPF and I’m trying to apply the MVVM pattern.
This is what is called from the ViewModel
await _NetworkUtil.ExecuteNetworkTask(NetworkStep1,NetworkStep2, DomainFQDN, 1000, 4, Gateway, IPAddress, SubNetMask, DNSSrvIPAddress, DomainController);
This is the method called from a utility class
public async Task ExecuteNetworkTask(Result networkStep1, Result networkStep2, string domaineFqdn, int timeout, int nbPing,string gateway, string ipAddress, string subnetMask, string dnsSrvIPAddress, string domainController)
I need to update the parameters NetworkStep1, NetworkStep2, DomainController
Now for some important details. “Result” networkStep1 / networkStep2 is a custom class
public class Result
{
private bool _success;
public bool Success {
get { return _success; }
set { _success = value; } }
private StringBuilder _message;
public StringBuilder Message
{
get { return _message; }
set
{
_message = value;
}
}
private StringBuilder _exception;
public StringBuilder Exception
{
get { return _exception; }
set
{
_exception = value;
}
}
public Result()
{
_message = new StringBuilder();
Exception = new StringBuilder();
}
public void UpdateLog(string message)
{
Message.AppendLine(message);
OnLogUpdated(message);
}
public void UpdateEx(string message)
{
Exception.AppendLine(message);
OnLogUpdated(message);
}
public event Action<string> LogUpdated;
protected virtual void OnLogUpdated(string updatedLog)
{
LogUpdated?.Invoke(updatedLog);
}
}
This is how the Result is initialised before the task is called (in ViewModel)
NetworkStep1 = new Result();
NetworkStep1.LogUpdated += UpdateTextBoxLog;
NetworkStep2 = new Result();
NetworkStep2.LogUpdated += UpdateTextBoxLog;
await _NetworkUtil.ExecuteNetworkTask(NetworkStep1,NetworkStep2, DomainFQDN, 1000, 4, Gateway, IPAddress, SubNetMask, DNSSrvIPAddress, DomainController);
The .LogUpdated += UpdateTextBoxLog is used to update a log window in real time when a Result is updated.
Example within the ExecuteNetworkTask :
networkStep1.UpdateLog(‘Connected to network’)
These are the declarations in ViewModel for NetworkStep1/2. “Session” is an instantiation of the M_Session Model.
public Result NetworkStep1
{
get { return Session.NetworkEtape1; }
set
{
Session.NetworkEtape1 = value;
OnPropertyChanged(nameof(NetworkStep1));
}
}
public string DomainController
{
get { return Session.DomainController; }
set
{
Session.DomainController = value;
OnPropertyChanged(nameof(DomainController));
}
}
That’s it for the summary, in the hope that it’s not too confusing and at the same time sufficiently complete.
To summarise now, I need to update the ViewModel’s DomainControler property via the ExecuteNetworkTask async task. I could, of course, set it as a return, but as I could potentially have several properties to update, I thought it would be more practical to set them as parameters. As the DomainController update didn’t work, I finally realised that I couldn’t use a REF in an async task.
BUT … surprisingly the result class networkStep1 and 2 update very well! Since the log is updated in real time and the control to move on works well.
if(NetworkStep1.Success && NetworkStep2.Success)
{
CurrentStepView = new UserAuthentification();
}
Basically, I didn’t create the Result class for this purpose at all, but the way I’m using it, it seems (unless it’s a big illusion) that the way I’m using it allows it to be updated within an async task.
Could you help me understand how? So that I can update the DomainController.
Thank you all for your help
2
The reasons the Result
class is updating and DomainController
is not is because Result
is a standard reference type and DomainController
is a string
, which is immutable.
Standard reference types pass references to specified parameter value to methods whereas value types (structs and primitive types) pass copies of specified parameter values to methods. This means that if you pass a class to a method and edit it, the original class will be updated but if you pass an integer to a method and update it, the original integer will not be updated.
Strings are immutable reference types so will pass references of specified values to methods but when the values are edited, the original value will not change but a new instance will be created.
So you would need the ref
keyword for any string parameters whose original instances you want to be updated but you don’t need it for any class parameter (e.g. Result
) whose original instances you want to be updated.
Unfortunately you cannot use the keyword ref
with async methods so you will need to return your updated string. If you need to return multiple value type values, you can create data type for the return values (similar to a parameter object but for your return value);
See the following resource for more info on reference type vs value type.
2