1

In my C# Windows Forms application , I retrieve some data from WebServices over the Internet. Refresh every second

It works as asynchronous operations and works well but whenever application gets disconnected from Internet, it shows an exception, and when it reconnects to the Internet, program should work automatically and immediately.

Currently, the program takes more then one minute to start working again, and I would like the exception to be ignored when connection drops.

it refreshed every second , it mean there are plenty of threads running at same time and when they all done , then it comes to connecting

What solution i can use so my programs runs ASAP when internet connects?

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)

        }
    }
kawafan
  • 231
  • 1
  • 7
  • 16
  • Still not able to find any solution , there is way if we can check for internet connectivity before each operation but its not efficient – kawafan Sep 06 '11 at 05:33

1 Answers1

0

I would probably do the following:

  • In your timer code which runs every second, I would check if the internet connectivity is available by P/Invoke (which is faster way than having the service throw an exception, and looks like it would suit your cause as well). For some reference look here

    • I would have the P/invoke code also set a flag temporarily somewhere (make sure it is thread safe) and before making any web service calls, i would check if the flag is in a valid state for the client to make that call.
Community
  • 1
  • 1
Raghu
  • 2,678
  • 2
  • 31
  • 38