Home > DeveloperSection > Forums > C# .net event handler delegate question
Dag Hammarskjold
Dag Hammarskjold

Total Post:24

Posted on    August-10-2013 5:17 AM

 C# C# 

 1 Reply(s)
 1150  View(s)
Rate this:
Class A includes a variable A to hold the status of an event that is triggered in Class C ( doEvent() ). The thread sleeps for 500 ms as it waits for Class B to kick off an Event which send the EventArg data back to Class C and calls an Event Handler UpdateClassVar(EventArgs e) - which is what it waits for.

Question: How do I used Event Handler to trigger next step in Class C ( // do something ) that is dependent on Status Update without relying on a Thread.Sleep... ??

Disclaimer: Consider me a novice programmer. Consider this my best attempt and a fairly non-elegant solution. Please feel free to re-design and re-code, this should be considered as only one prototype effort to solve above issue.

NOTE: Code Exampled Edited for 'real - world' example that should actually work. ( not fully tested )

Additional Question - Thread.Sleep(400) in example below stops main thread... halting all processes, although looking for an alternative, is this even a viable solution??

 public class OrderA : ICurrencyOrder


     private int _clid;

     private string _status;

     public int Clid


         get { return _clid; }

         set { _clid = value; }


     public string Status


         get { return _status; }

         set { _status = value; }



 Class B


      // event delegate for UpdateOrder

      public event UpdateOrderDelegate UpdateOrderEvent;

      /* Code Here to receive and process order assign status etc */

      /* An Event Arguments (args)  Class is created that hold e.Status and e.Clid values */

Pravesh Singh

Total Post:412

Posted on    August-10-2013 5:21 AM


I think that AutoResetEvent(http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx) is the class you are looking for if you want to wait for something to happen before continuing doing something else.

There is an example provided at the class description page, basically you call WaitOne where you want to wait, and Set when you want to continue.

Don't want to miss updates? Please click the below button!

Follow MindStick