none
event delegate (beginInvoke)

    Question

  • Hello

             I am developed an Application(windows).I am using event and delegate and find that I very slow in working. So want to use BEGIN.INVOKE( This method create a new thread ) which speed the process BUT want to know

     

     

    class testingbeginInvoke

    {

    public delegate void passstring(string Message);

    public static event passstring strEvent;

    public void name()

    {

    if (strEvent != null)

         strEvent.BeginInvoke("This is the Message", new AsyncCallback(Form2.Fun), null);

    }

    }

     

     

    Now in my other Class I want to subscribe to the Event in a above class

     

    testingbeginInvoke.strEvent += new testingbeginInvoke.passstring(testingbeginInvoke_strEvent);

     

    void testingbeginInvoke_strEvent(string Message)

    {

    }

    public static void Fun(IAsyncResult ar)

    {

    }

     

    Now want to know when i run the program it run "testingbeginInvoke_strEvent" method So what happen new AsyncCallback(Form2.Fun), this method is never called

     

    Can some body tell me How to use beginInvoke using Event and delegate and Subscribe  to that event in other class

     

     

    Monday, May 05, 2008 6:11 PM

Answers

  • You need four things.

    1 - A delegate with the signature of the method you want to call (same parameters and return type)

    2 - A method to call the delegate.

    3 - The method that does all the work.

    3 - A call back method which will be called when the method is completed.

    The following pseudocode should get you started.

    1 class DelegateTest  
    2 {  
    3     delegate ReturnValue MethodDelegate();  
    4  
    5     ...  
    6  
    7     public void CallDelegate()  
    8     {  
    9         MethodDelegate delegateInstance = new 
    10         MethodDelegate(BeginClientSynchronization);  
    11         delegateInstance.BeginInvoke(new AsyncCallback(LongRunningMethodFinished), null);  
    12     }  
    13  
    14     private ReturnValue LongRunningMethod()  
    15     {  
    16         ReturnValue returnValue;  
    17  
    18         ...  
    19  
    20         return returnValue;  
    21     }  
    22  
    23     private void LongRunningMethodFinished(IAsyncResult result)  
    24     {  
    25         ReturnValue longRunningMethodReturnValue;  
    26         AsyncResult asyncResult;  
    27  
    28         asyncResult = result as AsyncResult;  
    29  
    30         longRunningMethodReturnValue = (asyncResult.AsyncDelegate as ReturnValue).EndInvoke(result);  
    31  
    32         ...  
    33     }  
    34  
    35     ...  
    36 }  
    37  

     Bro Num

    • Edited by Bronumski Friday, June 13, 2008 2:48 PM Fix code layout after Forum change
    Tuesday, May 06, 2008 3:23 PM
  • If my previous post did not answer your question it is because I read your question wrong. I think what you are trying to do is fire an event from one object and have other objects respond and listen to your events. If this is the case you want to do something like this:

     

    Code Snippet

     

    1 public class MessageEventArgs : EventArgs  
    2 {  
    3     public string Message { getprivate set; }  
    4  
    5     public MessageEventArgs(string message)  
    6     {  
    7         this.Message = message;  
    8     }  
    9 }  
    10  
    11 public class EventObject  
    12 {  
    13     public event EventHandler<MessageEventArgs> SomethingHappened;  
    14  
    15     public DoSomething()  
    16     {  
    17         ...  
    18  
    19         OnSomethingHappened(new MessageEventArgs("wibble"));  
    20  
    21         ...  
    22     }  
    23  
    24     private OnSomethingHappened(MessageEventArgs e)  
    25     {  
    26         if (SomethingHappened != null)  
    27         {  
    28             SomethingHappened(this, e);  
    29         }  
    30     }  
    31  
    32     ...  
    33 }  
    34  
    35 public class ListenerObject  
    36 {  
    37     ...  
    38  
    39     public ListenerObject()  
    40     {  
    41         ...  
    42  
    43         eventObjectInstance += EventObject_SomethingHappened  
    44         ...  
    45     }  
    46  
    47     ...  
    48  
    49     EventObject_SomethingHappened(object sender, MessageEventArgs e)  
    50     {  
    51         //Do something here  
    52     }  
    53  
    54     ...  
    55

    The following article gives an excellent presentation on custom events and event handling:

    http://www.codeproject.com/KB/cs/event_fundamentals.aspx

    Bro Num

    • Edited by Bronumski Friday, June 13, 2008 2:51 PM Fix code layout after forum move
    Tuesday, May 06, 2008 3:39 PM

All replies

  • What exactly do you want to accomplish? Take a look at this thread. Perhaps it is all you need:

     

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3290810&SiteID=1

     

    Perhaps you should approach your problem from the other end: formulate it first.

    Monday, May 05, 2008 7:44 PM
  • You need four things.

    1 - A delegate with the signature of the method you want to call (same parameters and return type)

    2 - A method to call the delegate.

    3 - The method that does all the work.

    3 - A call back method which will be called when the method is completed.

    The following pseudocode should get you started.

    1 class DelegateTest  
    2 {  
    3     delegate ReturnValue MethodDelegate();  
    4  
    5     ...  
    6  
    7     public void CallDelegate()  
    8     {  
    9         MethodDelegate delegateInstance = new 
    10         MethodDelegate(BeginClientSynchronization);  
    11         delegateInstance.BeginInvoke(new AsyncCallback(LongRunningMethodFinished), null);  
    12     }  
    13  
    14     private ReturnValue LongRunningMethod()  
    15     {  
    16         ReturnValue returnValue;  
    17  
    18         ...  
    19  
    20         return returnValue;  
    21     }  
    22  
    23     private void LongRunningMethodFinished(IAsyncResult result)  
    24     {  
    25         ReturnValue longRunningMethodReturnValue;  
    26         AsyncResult asyncResult;  
    27  
    28         asyncResult = result as AsyncResult;  
    29  
    30         longRunningMethodReturnValue = (asyncResult.AsyncDelegate as ReturnValue).EndInvoke(result);  
    31  
    32         ...  
    33     }  
    34  
    35     ...  
    36 }  
    37  

     Bro Num

    • Edited by Bronumski Friday, June 13, 2008 2:48 PM Fix code layout after Forum change
    Tuesday, May 06, 2008 3:23 PM
  • If my previous post did not answer your question it is because I read your question wrong. I think what you are trying to do is fire an event from one object and have other objects respond and listen to your events. If this is the case you want to do something like this:

     

    Code Snippet

     

    1 public class MessageEventArgs : EventArgs  
    2 {  
    3     public string Message { getprivate set; }  
    4  
    5     public MessageEventArgs(string message)  
    6     {  
    7         this.Message = message;  
    8     }  
    9 }  
    10  
    11 public class EventObject  
    12 {  
    13     public event EventHandler<MessageEventArgs> SomethingHappened;  
    14  
    15     public DoSomething()  
    16     {  
    17         ...  
    18  
    19         OnSomethingHappened(new MessageEventArgs("wibble"));  
    20  
    21         ...  
    22     }  
    23  
    24     private OnSomethingHappened(MessageEventArgs e)  
    25     {  
    26         if (SomethingHappened != null)  
    27         {  
    28             SomethingHappened(this, e);  
    29         }  
    30     }  
    31  
    32     ...  
    33 }  
    34  
    35 public class ListenerObject  
    36 {  
    37     ...  
    38  
    39     public ListenerObject()  
    40     {  
    41         ...  
    42  
    43         eventObjectInstance += EventObject_SomethingHappened  
    44         ...  
    45     }  
    46  
    47     ...  
    48  
    49     EventObject_SomethingHappened(object sender, MessageEventArgs e)  
    50     {  
    51         //Do something here  
    52     }  
    53  
    54     ...  
    55

    The following article gives an excellent presentation on custom events and event handling:

    http://www.codeproject.com/KB/cs/event_fundamentals.aspx

    Bro Num

    • Edited by Bronumski Friday, June 13, 2008 2:51 PM Fix code layout after forum move
    Tuesday, May 06, 2008 3:39 PM
  • Perhaps change the name method to:

     

    Code Snippet

    public void name()

    {

    foreach (EventHandler eh in strEvent.GetInvocationList())

    {

    eh.BeginInvoke(thisnew AsyncCallback(Form2.Fun), null);

    }

    }

     

     

     

     

    Wednesday, May 07, 2008 12:42 AM
  • Yay! I'm not the only person in the world who uses 'wibble'!!!!
    Thursday, April 08, 2010 1:24 PM
  • 'Wibble' is good but what about 'Wobble'? I save it for special occasions as it is a little too powerful for day to day use.
    Bronumski
    LIFE'S TOO SHORT FOR BLOGGING...
    Tuesday, July 06, 2010 9:22 AM
  • I think you should do this way:


    class testingbeginInvoke

    {

    public delegate void passstring ( string Message);

    public static event passstring strEvent;

    public void name()

    {

    if (strEvent != null )

         strEvent.BeginInvoke( "This is the Message" , new AsyncCallback ( this .Fun ), null );

    }

     

    public void Fun( IAsyncResult ar)

    {

    strEvent.EndInvoke(ar);

    }

    }

     


     

    Now in my other Class I want to subscribe to the Event in a above class

     

    testingbeginInvoke .strEvent += new testingbeginInvoke . passstring (testingbeginInvoke_strEvent);

     

    void testingbeginInvoke_strEvent( string Message)

    {

    }


     

    Thursday, November 25, 2010 10:52 PM
  • Recently I was working on a project using several threads in C#. I had four threads; one for the Main UI, one for serial communication with some force sensors, one operating a motion controller, and the final one used to perform user defined scripts. When using multiple threads it is important to control the method of information transfer between threads. Although some older software required you to create your own flags or methods to prevent multiple access to the same point, C# allows you access to several ways to do this that are both easy to use and very robust.

    There are several different ways you can correctly use delegates, however I have found the easiest and most robust way to send information from my slave threads to my Main UI is to use an event structure. In this way I have my Main UI ‘subscribe’ to events from all my other threads and the event handler invokes a delegate. Each thread then raises an event anytime there is information that the Main UI needs.
    Friday, June 17, 2011 8:19 PM