Thursday, February 10, 2011

Global Variable not incrementing on callback functions

i am using Webclient to upload data using Async call to a server,

    WebClient webClient = new WebClient();
   webClient.UploadDataAsync(uri , "PUT", buffer, userToken);

i've attached DatauploadProgress and DatauploadCompleted Events to appropriate callback functions

        // Upload Date Completed 
        webClient.UploadDataCompleted += new
                UploadDataCompletedEventHandler(UploadDataCallback2);

        // Upload Date Progress
        webClient.UploadProgressChanged += new 
                 UploadProgressChangedEventHandler(UploadProgressCallback);

and in the functions i am trying to show some MessageBoxes:

      // Upload Date Progress
     void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
    {
        MessageBox.Show( this,"Upload Progress  ,x =" +x);
        x++;
        MessageBox.Show(e.BytesSent.ToString());
    }



        // Upload Date Completed 
     void UploadDataCallback2(object sender, UploadDataCompletedEventArgs e)
    {

        MessageBox.Show(this, "Upload Done,x =" +x);
        x++;
        MessageBox.Show(ASCIIEncoding.UTF8.GetString(e.Result));
    }

Where x is a global variable, However for some reason x is not getting incremented, and all the message boxes show x=0..

any explanation would be much appreciated..

  • Oh found the problem, well apparently the problem was a 2-parts problem , and i hope someone would confirm my conclusion :

  • the MessageBox.show on the progress, blocked the function from progressing resulting in x staying at zero until i pressed ok.
  • The files i was uploading were too small, so the datauploadcompleted event was called before i got enough time to click the ok on the messagebox from the progress event
  • Rubens Farias : seems right to me; replace that messabe by `"Upload Progress ,x =" + ++x` and try to upload a larger file to confirm
    Madi D. : Yep, after editing the "messabe" ;) to ++x , and with a bigger file i solved the problem, thamks Rubens :)
    Eclipsed4utoo : The `MessageBox` is a dialog box that stops the execution of the code until it is dismissed.
    From Madi D.

0 comments:

Post a Comment