开发者

Global Variable not incrementing on callback functions

开发者 https://www.devze.com 2022-12-17 04:29 出处:网络
i am using Webclient to upload data using Async call to a server, WebClient webClient = new WebClient();

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  开发者_JAVA技巧,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
  • 0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    关注公众号