C# .net event handler delegate question
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 stri开发者_StackOverflow社区ng _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 */
UpdateOrder(args)
private void UpdateOrder(args)
{
if (UpdateOrderEvent != null)
{
UpdateOrderEvent(this, e);
}
}
}
// This is the class that initates the order (sendOrder()) and then waits for the
// status of the order before processing the next step in the order process
// which could be to send order again to exit
Class C
{
// add event handler to update status and clid from Class B
UpdateOrderEvent += new UpdateOrderDelegate(UpdateOrdersListener);
public void EnterOrder(EnterEventArgs e)
{
// set connectinon to ICurrencyOrder class above OrderA
// this uses a switch statement (omitted) to use correct class (A, B, C etc).
ICurrencyOrder ordVals = Class A;
bool fill = false;
for (int i=0; i<5 && fill=false; i++)
{
sendOrder("Symbol", price, clid, etc)
Thread.Sleep(400);
if (ordVals.Clid = clid) // check to see if order was updated using client id
{
if (ordVals.Status = 'FILLED')
{
filled = true;
}
}
} // end for loop
} // end EnterOrder
private void UpdateOrdersListener(object sender, EventArgs e)
{
OrderUpdateEventArgs ev = (OrderUpdatEventArgs)e;
// set connection to appropriate class using switch (symbol) omitted
ICurrencyOrder ordVal = Class A;
ordVal.Status = e.Status;
ordVal.Clid = e.Clid;
} // end Class C
I think that AutoResetEvent 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.
精彩评论