I am currently rewriting my code to add asynchronous execution. However, I'm having trouble with adapting a group of methods.
I looked at a few questions like this one before asking but often time the problem was using Task.Run() instead of async/await.
=======Abstract=======
My program will be used to automate a test bench with multiple valves. As such I have written methods like open/closeValve() that open or close a valve to a certain threshold, increase/decreaseFlow() to select which valve to open or close and a regulate() method that calls increqse/decreaseFlow().
It's just recently that I decided, after reading about it, that adding asynchronous execution was a good idea to display data about the bench in real time. But my methods were all thought out to work in a synchronous way so I'm having trouble adapting them.
=======Code=======
For example here is the openValve() method
public int openValve(int limit)
{
if(_masterFlowrate >= _instructions - 0.1 && _masterFlowrate <= _instructions + 0.1)
{
//flowrate ~= instructions => no need to manipulate the valves
return 1;
}
else
{
if(valveOpening < limit)
{
//flowrate < instructions & valve opened less than the limit
// => opening valve untill the limit or untill flowrate = instructions
valveOpening++;
//Opening the valve by writing the value in a command computer via Modbus
master.WriteSingleRegister(1, _mdbAdr, Convert.ToUInt16(ouvertureValve));
return 0;
}
else
{
//flowrate < instructions & valve already opened beyond the limit
// => not manipulating the valve
return -1;
}
}
To adapt this method to asynchronous execution I changed the returned value type from int to Task<int> and changed return 1 to return Task.FromResult(1) for example.
Now the increaseFlow() method that directly uses the previous one looks like this (Vp, Vm and Vg are instances of the Valve class)
public int increaseFlow()
{
int retVal = 0;
switch(openingStep)
{
case 1:
retVal = Vp.openValve(60);
if(retVal > 0) openingStep = 4;
else
{
if(retVal < 0) openingStep = 2;
}
break;
case 2:
retVal = Vg.openValve();
if(retVal > 0) openingStep = 4;
else
{
if(retVal < 0) openingStep = 3;
}
break;
case 3:
retVal = Vm.openValve();
if(retVal > 0) openingStep = 4;
else
{
if(retVal < 0) openingStep = 4;
}
break;
}
return retVal;
}
This is where I'm starting to have troubles. I've tried changing int retVal = 0; into var retVal = Task.FromResult(0) which doesn't give me any compiler error.
Then adding awaits keywords before each openValve() but this gives me the following :
Error CS0029 Cannot implicitly convert type 'int' to 'System.Threading.Tasks.Task<int>'
And then the regulate() method calls increaseFlow().
=======Conclusion=======
That's where I began to think that maybe the code wasn't thought out to work asynchronously and that it needed to be rearranged. Is this really my problem or is there a solution that wouldn't require to completely redo the methods. I'm not saying that I wouldn't do it but I'm interested in knowing if the structure is wrong or if I'm just not using the right keywords/methods etc...
WriteSingleRegisterandopenValve, so it's a bit more complicated.