Swati
Swati

Reputation: 2918

implement callback in user defined libraries using C#

Hi i have some question related to call back feature in libraries created by user in c#

  1. i have created a winform application named "Sample"
  2. i have also created a class library named "Library"
  3. Sample contains only one form that has a button say "CALL"
  4. i have implemented all the coding part in a library
  5. when i click on the call button on form then a method "ACTIVATE CALL" of the library is called.
  6. this method performs some work on a thread.
  7. What i want is when thread work if finished then "CALLBACK" method placed in my winform must be called.
  8. To achieve this i have passed "this" reference of the form to the library
  9. i collected "this" as obj "Object" type in formal arguement in library.

can anybody suggest me how to call callback method?

i tried this:

if(obj.GetType()== typeOf(what to specify here))
{
   obj.callback();
}

hope somebody can provide me help. note: both library and sample are different projects how to achieve callback feature?

Upvotes: 1

Views: 1351

Answers (3)

Swati
Swati

Reputation: 2918

Finally i achieved this using Delegate+Event

*****************Sample class**************************


    call()
        {
          //activate method of library is called
          libraryObject.stop += new LibraryClass.callback(setCallbackMethod);
          libraryObject.activate();
        }

        public void setCallbackMethod(String str)
         {
// most important to be back on main thread

 this.Invoke((MethodInvoker)delegate
                {
                    btn.Enabled = true;
                });
         }



*******************Library***************************

public delegate void callback(String str);
public event callback stop;
activate()
{
   //instantiates a thread/timer
            aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);
            aTimer.Interval = 1000;
            aTimer.Start();
}


   public void CheckForMessage(object source, ElapsedEventArgs e)
    {
    //performs some work

    //calls callback method of ui thread in sample code
        if (stop != null)
            {
                stop("COMPLETED");
            }
    }

Upvotes: 0

Russell Troywest
Russell Troywest

Reputation: 8776

Have the user of the ActivateCall function supply a callback so in you library:

function void ActivateCall(Action callback){
  //Do Stuff
  if (null != callback){
     callback();
   }
}

and then in your main form:

function button1_Click(object sender, EventArgs e){
  library.ActivateCall(DoStuff);
}

There are a number of things to look out for though since you say you are doing stuff in a separate thread within the library call. If you are altering the GUI at all in the callback you will need to make sure you do the work in the GUI thread. You will also need to make sure you run the callback once all the work in the thread has been completed (I suspect).

To make sure your callback is run in the GUI thread (if required) do something like this:

function button1_Click(object sender, EventArgs e){
  library.ActivateCall(DoStuff());
}

function void DoStuff(){
  if (InvokeRequired(){
    Invoke(DoStuff);
    return;
  }

  //Do stuff here....
}

Upvotes: 1

Tomas McGuinness
Tomas McGuinness

Reputation: 7691

Define your library method with a callback.

public void ACTIVATE(object arg, object arg, Action callback)
{
// Do what you have to do here.
callback.Invoke();
}

Then, in your Sample WinForms client you can call something like this.

public void MethodInSample()
{
Library lib = new Library();

Action callback = () => { DoSomethingHere };
Lib.ACTIVATE(1,1,callback);
}

If you want the callback to return some parameters, you could use a Func<> instead and define the arguments that way.

Alternatively, you could use an event. The BackgroundWorker object is a good example of this. This class has a method called RunWorkerAsync(), which causes some work to be done on a background thread. There is then an event called RunWorkerCompleted which you listen on to indicate when the background thread has completed.

Both methods are valid I think, but the second has the benefit of allowing more than one party to listen for completion of the work.

Upvotes: 3

Related Questions