user1112906
user1112906

Reputation: 61

Call a method in asynchronously from ASP.NET and then update controls on UI

I have a set of methods that I call on Page_Load event.
Some methods eventually call web services. Most of the methods execute in synchronous manner sequentially [one after another] because output from one method is an input to another.

However, there are few methods that can be ran in between those service calls. Since, all operations take considerable time to execute, I am trying to ran some of them in asynchronous way so that few will run in parallel.

However, I am not succeeding in updating controls on UI if I set those controls inside callback function since, I guess, the callback essentially runs in different thread from the main "Page Postback request" thread. Is there a way I can set my controls inside callback method and then update them on UI asynchronously. Here is the code example:

public partial class _Default : System.Web.UI.Page
{
    public delegate string foo(string param1,  string param2, ArrayList list, out ArrayList outList);  

    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.Sleep(1000);
        txtTextSync.Text = "my control";
        CallOperationAsync();
        Thread.Sleep(1000);

        Thread.Sleep(1000);
    }

    private void CallOperationAsync()
    {
            foo dlgt = new foo (this.LongRunningMethod) ;


            ArrayList inArrList = new ArrayList();
            inArrList.Add(233);

            ArrayList outArrList = new ArrayList();


            // Create the callback delegate.
            AsyncCallback asyncCallback = new AsyncCallback(MyAsyncCallback);

            // Initiate the Asynchronous call passing in the callback delegate
            // and the delegate object used to initiate the call.
            IAsyncResult ar = dlgt.BeginInvoke("zorik", "zorik2", inArrList, out outArrList, asyncCallback, dlgt); 
    }

    public void MyAsyncCallback(IAsyncResult ar)
    {
        string s ;
        ArrayList outArrList;

        // Because you passed your original delegate in the asyncState parameter
        // of the Begin call, you can get it back here to complete the call.
        foo dlgt = (foo) ar.AsyncState;

        // Complete the call.
        try
        {
            s = dlgt.EndInvoke(out outArrList, ar);
        }
        catch 
        {
        }

    }

    private string LongRunningMethod(string param1,  string param2, ArrayList list, out ArrayList outList)
    {
        Thread.Sleep(5000);
        outList = new ArrayList();

        outList.Add(8999);
        list.Add(678);

        // this control is inside update panel
        txtAsync.Text = "Async";
        updPnl1.Update();

        return "Xrensuccess";
    }

    protected void Page_PreRender(object s, EventArgs e)
    {
        string a = "";
    }
}  

Upvotes: 0

Views: 2996

Answers (3)

Madhu Beela
Madhu Beela

Reputation: 2215

function Async() {
    var url = "http://mydomain.com/default.aspx?arr=12345";
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.async = true;
    script.type = 'text/javascript';
    script.src = url;
    head.appendChild(script);

}

Register the function from serverside code using ClientScriptManager

try this !!!

Upvotes: 0

user734028
user734028

Reputation: 1111

Why dont you use ajax to update your controls with data that has to arrive a lil late? Asynchronous flow doesnt work in asp.net as it does on windows forms I would guess. You can show "loading .." at different sections of the page and do whatever (during an ajax call) you want up on the server side code with one thread waiting on another thread and so on.

Upvotes: 1

Eugene
Eugene

Reputation: 2908

You cannot do this way, since when the asyncronous opereation finishes the page that started the operation have been already rendered. Therefore, the server does not have anything to update.

All updates should be initiated by browser. You can create some periodical query request from the page to get updates, for eaxample by using AJAX

Upvotes: 0

Related Questions