tags:

views:

221

answers:

2

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(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)

+1  A: 

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.

Martín Marconcini
Tried the Callstack it didnt help.. The callback gets called by mscorlib.dll ..and if i try to breakpoint it..it stops the 2nd point.. however +1 for helping me read and learn a new trick :)
Madi D.
+1  A: 

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

Preet Sangha
It seems that the webclient itself is doing the double calls, somehow it slipped my mind to check all the properties of "UploadProgressChangedEventArgs", i only checked the sent and they were the same,however when i check the recieved args, they change from -1 to 0 .. obviously 2 different calls,nothing to do with my threading..
Madi D.