i am working on some code where a timer tick every seconds and and get data from Webservice and Shows in on WInform ,
What is the best approach to not run Multiple operations at same time ?
operation should run if previous operation completed and not running.
the code is
public void loadbalance()
{
try { //Get Data from Internet }
catch { }
}
delegate void loadbalancedelegate();
public void loadBalanceAsync()
{
loadbalancedelegate worker = new loadbalancedelegate(loadbalance);
AsyncCallback LoadbalnceCallBack = new AsyncCallback(loadbalanceCompleted);
AsyncOperation async = AsyncOperationManager.CreateOperation(null);
worker.BeginInvoke(LoadbalnceCallBack,async);
}
public void loadbalanceCompleted(IAsyncResult result)
{
loadbalancedelegate worker = (loadbalancedelegate) ((AsyncResult)result).AsyncDelegate;
AsyncOperation async = (AsyncOperation)result.AsyncState;
worker.EndInvoke(result);
}
delegate void setControlsBalanceDelegate(BalanceOB ball);
void setControlsBalance(BalanceOB ball)
{
if (this.InvokeRequired)
this.Invoke(new setControlsBalanceDelegate(this.setControlsBalance), new
object[] { ball });
else
{ //Update Data on Form (Windows App)
}
}
you can just disable the timer till one operation completes or set some field on completion (maybe better a ManualResetEventSlim because of multithreading).
Or have a look at the reactive extensions – there are a lot of good examples for such things.
You can find many samples here: 101 LINQ Samples, and very good intros here: Beginner’s Guide to the Reactive Extensions.
And finally Somasegar has one blog-post that is very similar to your problem with Rx: Reactive Extensions for .NET (webservice call of bingtranslate with all you ever need 😉 )