Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Using delegate callback in C#

Posted on 2003-02-21
3
Medium Priority
?
1,193 Views
Last Modified: 2012-06-21
This question is kind of related to what I had asked (I've pasted the link below). But there is more to it, so asking altogether a new question.

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20520971.html

I've 3 classes that I am working on. Class1, Class2, Class3. Class2 and Class3 are in the same package which is referred by Class1's package.

Class1 is responsible for displaying a progress bar related to the upload of files carried out by Class2 and Class3. Class2 receives a request from Class1 about the list of files/folders to be uploaded which it forwards to Class3.

I've the main UI thread taking care of the ListView with files and folders. After I pass the request from Class1 to Class2, Class2 creates a delegate and invokes a worker thread which carries out all the upload of the files/folders in Class3.

After all, here is my question. Now I need to somehow convey from Class3 all the way to Class1 when each chunk of file is uploaded so that could reflect in the ProgressBar.

What I was planning to do is make the delegate for the UploadProgress(string, long)(this function is responsible for updating the progress bar) in the ProgressBar and pass this delegate along with the list of files/folders to Class2 and then to the Class3. After each chunk is uploaded the Class3 will invoke a new worker thread and inform the Class1's UploadProgress(string, long) that chunk with this size is uploaded.

What I am planning to do is the right way?? I had asked in the question (I have pasted the link above) that how can I pass the delegate from one class to another which are in different packages. If this is not the right way to achieve, how can I get there. I've done quite a bit of research on this and would appreciate any ideas/suggestions/advice.

I hope I am clear in what I am trying to know. If anything is unclear please post back a question. (really sorry for such a long question but wanted to be very clear).

Thanks in advance..

Siddhartha
0
Comment
Question by:mehtas
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
thedude112286 earned 200 total points
ID: 8000372
I would add an event to Class3 and have Class1 and Class2 subscribe to that event.  
For example:
public delegate void OnFileChunkUploadedDelegate();
public class Class3
{
   event OnFileChunkUploadedDelegate OnFileChunkUploaded;
   
   private void UploadFileChunk()
   {
      ...
      //fire the event
      OnFileChunkUploaded();
   }
   ...
}

public class Class1
{
   Class3 class3;
   public Class1()
   {
     
      class3.OnFileChunkUploaded += new OnFileChunkUploadedDelegate(this.OnUploaded);
   ...
   }
   
   public void OnUploaded()
   {
      // do whatever you have to do when the chunk is uploaded
   }
}
   
0
 
LVL 2

Author Comment

by:mehtas
ID: 8001581
Hi,

Thanks for such a detailed answer. I implemented your suggestion in my code. I am firing the event from within the while loop (SPClient.cs) but the method in ProgressDlg.cs is never invoked. I also added a debugging statement to see but I never see that message. I have pasted below of how my code looks:

public class ProgressDlg
{
     public ProgressDlg()
     {
          // initialize the class variables..
          ...
          ...
          // SPClient.cs and TransferMgr.cs are in different namespace.
          // Using tDelegate, I invoke the Backup() in TransferMgr, which
          // has a while loop and sends 1 file at a time to SPClient to upload.
          // SPClient then uploads each chunk within a whlie loop and that is
          // where I fire the event.

          AsyncCallback cb = new AsyncCallback(UploadProgress);
          transMgr = new SPSoap.TransferMgr(fileFolderList, destination);
               
          spClient = new SPSoap.SPClient(0);
          spClient.FileChunkUploaded += new SPSoap.SPClient.FileChunkUploadedDelegate(this.Test);

          TransManagerDelegate1 tDelegate = new TransManagerDelegate1(transMgr.Backup);
          IAsyncResult iAR = tDelegate.BeginInvoke(cb, null);
     }

     public void UploadProgress()
     {
          this.Close();
          // this will invoke a function in another class FTPDlg
          // which would refresh the ListView of the FTP Explorer.
          // I pass this delegate in the constructor and
          // initialize it there.
          this.ftpDelegate.BeginInvoke(null, null);
     }

     public void Test()
     {
          this.TextBoxFld.Text = "I'm here.";
     }

     ...
     ...
}
************************
And now the SPClient.cs

public class SPClient
{
     public delegate void FileChunkUploadedDelegate();
     public event FileChunkUploadedDelegate FileChunkUploaded;

     ...
     ...

     // this function is called from TransferMgr with each file at a time to be uploaded.
     public void PerformUpload()
     {
          while(!EOF)
          {
               ...
               server.UploadChunk();
               FileChunkUploaded();
               ...
          }
     }
}

I know its kind of long code, but just wanted you to know all the details and there is a possibility of me doing it not correctly. I would really appreciate your reply for this.

Thanks,

Siddhartha
0
 
LVL 2

Author Comment

by:mehtas
ID: 8013420
ANY HELP IS HIGHLY APPRECIATED.

Please do not consider the previous statement. The reason the event was never triggered was because the code never goes into the while loop. It seems that I am running into some other problem here. And I am sure its got to do with the worker thread.

Before the while loop I have the code which reads bytes from the file I am trying to upload and for some reason it does not read at all. If I try this same thing just by calling this function (without using delegates) it works perfectly fine. But when I am using delegate for this function to be invoked, I am not able to read the bytes from the file. The code for this structure looks like this..

public class ProgressDlg
{
     public ProgressDlg()
     {
          // initialize the class variables..
          ...
          ...
          // SPClient.cs and TransferMgr.cs are in different namespace.
          // Using tDelegate, I invoke the Backup() in TransferMgr, which
          // has a while loop and sends 1 file at a time to SPClient to upload.
          // SPClient then uploads each chunk within a whlie loop and that is
          // where I fire the event.

          AsyncCallback cb = new AsyncCallback(UploadProgress);
          transMgr = new SPSoap.TransferMgr(fileFolderList, destination);
               
          spClient = new SPSoap.SPClient(0);
          spClient.FileChunkUploaded += new SPSoap.SPClient.FileChunkUploadedDelegate(this.Test);

          TransManagerDelegate1 tDelegate = new TransManagerDelegate1(transMgr.Backup);
          IAsyncResult iAR = tDelegate.BeginInvoke(cb, null);
     }

     public void UploadProgress()
     {
          this.Close();
          // this will invoke a function in another class FTPDlg
          // which would refresh the ListView of the FTP Explorer.
          // I pass this delegate in the constructor and
          // initialize it there.
          this.ftpDelegate.BeginInvoke(null, null);
     }

     public void Test()
     {
          this.TextBoxFld.Text = "I'm here.";
     }

     ...
     ...
}


public class TransferMgr
{
     ...
     public TransferMgr()
     {
          // intialize variables ListOfFileFolders and Destination path.
          ...
          ...
     }

     // this function is invoked from the constructor in ProgressDlg
        public void BackUp()
     {
          while(List.HasMoreElements())
          {
               if (Element.IsFile())
               {
                    spClient.PerformUpload(file);
               }
               else if (Element.IsFolder())
               {
                    while (eachFile)
                    {
                         spClient.PerformUpload(file);
                    }
               }
          }
     }
     ...
     ...
}


public class SPClient
{
     public delegate void FileChunkUploadedDelegate();
     public event FileChunkUploadedDelegate FileChunkUploaded;

     ...
     ...

     // this function is called from TransferMgr with each file at a time to be uploaded.
     public void PerformUpload()
     {
          while(!EOF)
          {
               ...
               server.UploadChunk();
               FileChunkUploaded();
               ...
          }
     }
}

THANKS IN ADVANCE..

Siddhartha Mehta
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question