开发者

C#: My callback function gets called twice for every Sent Request

开发者 https://www.devze.com 2022-12-29 03:24 出处:网络
I\'ve Got a program that uploads/downloads files into an online server,Has a callback to report progress and log it into a textfile, The program is built with the following structure:

I've Got a program that uploads/downloads files into an online server,Has a callback to report progress and log it into a textfile, The program is built with the following structure:

public void Upload(string source, string destination)
{     
        //Object containing Source and destination to pass to the threaded function
        KeyValuePair<string, string> file = new KeyValuePair<string, string>(source, destination);
       //Threading to make sure no blocking happens after calling upload Function
        Thread t = new Thread开发者_如何学Python(new ParameterizedThreadStart(amazonHandler.TUpload));
        t.Start(file);
}

  private void TUpload(object fileInfo)
{
    KeyValuePair<string, string> file = (KeyValuePair<string, string>)fileInfo;
    /*
    Some Magic goes here,Checking The file and Authorizing Upload
    */
    var ftiObject = new FtiObject () 
        { FileNameOnHDD = file.Key,
            DestinationPath = file.Value,
            //Has more data used for calculations.
        };
          //Threading to make sure progress gets callback gets called.
        Thread t = new Thread(new ParameterizedThreadStart(amazonHandler.UploadOP));
            t.Start(ftiObject);
            //Signal used to stop progress untill uploadCompleted is called.
            uploadChunkDoneSignal.WaitOne();
         /*
          Some Extra Code
         */
}

 private void UploadOP(object ftiSentObject)
{
  FtiObject ftiObject = (FtiObject)ftiSentObject;
  /* 
   Some useless code to create the uri and prepare the ftiObject.
  */  

   // webClient.UploadFileAsync will open a thread that 
   // will upload the file and report
   // progress/complete using registered callback functions.

  webClient.UploadFileAsync(uri, "PUT", ftiObject.FileNameOnHDD, ftiObject);
}

I got a callback that is registered to the Webclient's UploadProgressChanged event , however it is getting called twice per sent request.

 void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
    {
        FtiObject ftiObject = (FtiObject )e.UserState;
                Logger.log(ftiObject.FileNameOnHDD, (double)e.BytesSent ,e.TotalBytesToSend);
    }

Log Output:

  Filename: C:\Text1.txt  Uploaded:1024 TotalFileSize: 665241
  Filename: C:\Text1.txt  Uploaded:1024 TotalFileSize: 665241
  Filename: C:\Text1.txt  Uploaded:2048 TotalFileSize: 665241
  Filename: C:\Text1.txt  Uploaded:2048 TotalFileSize: 665241
  Filename: C:\Text1.txt  Uploaded:3072 TotalFileSize: 665241
  Filename: C:\Text1.txt  Uploaded:3072 TotalFileSize: 665241
  Etc...

I am watching the Network Traffic using a watcher, and only 1 request is being sent.

Some how i cant Figure out why the callback is being called twice, my doubt was that the callback is getting fired by each thread opened(the main Upload , and TUpload), however i dont know how to test if thats the cause.

Note: The reason behind the many /**/ Comments is to indicate that the functions do more than just opening threads, and threading is being used to make sure no blocking occurs (there a couple of "Signal.WaitOne()" around the code for synchronization)


If you put a breakpoint on the callback, can’t you check the callstack to see where the call came from? If you use Visual Studio that is.


Have you checked that the API doesn't indeed call back twise with different info in UploadProgressChangedEventArgs?

0

精彩评论

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

关注公众号