After googeling for ages, and reading some stuff about async task in books. I made a my first program with an async task in it. Only to find out, that i can only start one task. I want to run the task more then once. This is where i found out that that doesn’t seem to work. to be a little bit clearer, here are some parts of my code:
InitFunction(var);
This is the Task itself
public async Task InitFunction(string var)
{
_VarHandle = await _AdsClient.GetSymhandleByNameAsync(var);
_Data = await _AdsClient.ReadAsync<T>(_VarHandle);
_AdsClient.AddNotificationAsync<T>(_VarHandle, AdsTransmissionMode.OnChange, 1000, this);
}
This works like a charm when i execute the task only once.. But is there a possibility to run it multiple times. Something like this?
InitFunction(var1);
InitFunction(var2);
InitFunction(var3);
Because if i do this now (multiple tasks at once), the task it wants to start is still running, and it throws an exeption.
if someone could help me with this, that would be awesome!
~ Bart
async/awaitcan work perfectly with multiple tasks, and multiple tasks at once. However, sometimes different objects can place restrictions on how many asynchronous operations can be outstanding at one time.For example, the
Pingclass can only be used to send one ping at a time. If you want to send multiple pings at once, you need to use multiplePinginstances.I suspect the same problem is at play here:
_AdsClientprobably is restricted to a single asynchronous operation at a time. So, if you want multipleInitFunctions to be run at once, you’ll have to use multiple instances of whatever type that is.On the other hand, if you wanted to run
InitFunctionmultiple times, one at a time, then you just need to add someawaits to your calling code:This will probably work – unless
_AdsClienthas “one-time use” semantics. Some classes do have this restriction.