您如何建议我在使用值> 0更新变量时设置要触发的事件处理程序.
public _price;
public double GetPrice(string item)
{
_table.GetData += new EventHandler<EventArgs>(ExtractData);
// Right now I set it to sleep to give it enough time to return the data.
// But I would like to setup an eventhandler to return the _price when a value is populated
Thread.Sleep(1000);
return _price;
}
void ExtractData(object sender, DataEventArgs e)
{
foreach (PriceRecord rec in e)
{
if (rec.myprc != null)
{
_price = double.Parse(rec.myprc.Value.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我删除Sleep,则在返回错误值之前没有足够的时间来获取数据.我想删除睡眠以提高性能,仅仅是为了我自己的知识.如果有比使用事件处理程序更好的替代方案我可以接受建议.
非常感谢您的任何建议或建议,谢谢.
小智 5
你的设计......它有问题.
你不能用事件阻止方法的执行,所以我认为这不是一个好的解决方案.
它是多线程的//Some code here吗?如果是这样,请检查Thread.Join(如果您正在使用重量级线程)或Monitor(如果您正在使用线程池)阻止执行GetPrice直到设置了值.
如果没有你正在做的细节,我不确定是否有更好的解决方案.
| 归档时间: |
|
| 查看次数: |
4832 次 |
| 最近记录: |