views:

16

answers:

1

Hello All,

I have a winapp running with a DLL referenced to it. all the major operations are done in the DLL.

I have like 1000 xml files stored in a location which i have to read and store data into another format.

From the UI, i click on an import button, the call is passed to the DLL layer which converts one xml after another stored at the location in a foreach loop.

In case one xml fails because it is not in proper format, the tool has to display a message in an textbox in my UI but the exception occurs in the dll. The tool has to continue with the other XML's.

I know through a delegate its possible but am confused on how to use it between the DLL and the UI as the DLL does not have the textbox handle.

thanks...

A: 

You need to use events - the relevant class in DLL can define event to signal success/failure of each xml file that UI can handle. Another way would be to accept callback function (delegate) in the method to inform the UI. Here'e the simple sample code:

In DLL:

// delegate that will inform UI
public delegate void FileProcessedHandler(string filePath, bool success);

...

// Method that process files
public void Process(FileProcessedHandler callback)
{

   // loop processing file one by one
   for(..)
   {
      // process one file 
      var success = processFile(filePath);

      // Notify UI
      if (null != callback)
      {
         callback(filePath, success);
      }
   }
}

In UI:

...
// code that invokes DLL for processing file
// must invoked on the different thread so that UI will remain responsive
ThreadPool.QueueUserWorkItem(o => { [object from DLL].Process(OnFileProcessed); });

....

// Callback method (assuming inside control/form)
public void OnFileProcessed(string filePath, bool success)
{
   // Its important to marshal call to UI thread for updating UI
   this.Invoke(() => {
      Text1.Text = string.Format("File: {0}, Processed: {1}", filePath, success? "Success": "Failure");
   });
}
VinayC
Hey thanks, it worked like a breeze, only one change this.Invoke((MethodInvoker)delegate ... :-)
srivatsayb