我有一个具有事件和自定义EventArgs的类。有意义的代码:Cross.Thread违规
public void OnTickReceived(TickReceivedEventArgs e)
{
EventHandler<TickReceivedEventArgs> handler = TickReceived;
if (handler != null)
handler(this, e);
}
public event EventHandler<TickReceivedEventArgs> TickReceived = delegate { };
和消费在UI的Windows窗体订阅事件这样
private void button4_Click(object sender, EventArgs e)
{
bool esito;
t = new T3OpenStockReader();
esito = t.Connect();
textBox1.Text += "Connection: " + esito.ToString() + "\r\n";
Application.DoEvents();
if (esito)
{
esito = t.Subscribe("MI.EQCON.2552");
textBox1.Text += "Subscription: " + esito.ToString() + "\r\n";
Application.DoEvents();
}
if (esito)
{
t.Start();
t.TickReceived += NewTick_Event;
System.Diagnostics.Debug.Print("Reading started...");
}
}
private void NewTick_Event(object sender, TickReceivedEventArgs e)
{
textBox1.Text += e.tick.DT + " " + e.tick.Price + " " + e.tick.Volume + "\r\n";
}
我在班上的InvalidOperationException
- cross.thread操作。我究竟做错了什么?
你不应该是,我看你不需要,在那个按钮点击处理程序中调用DoEvents。不必要地打开各种错误/问题。 – Servy
谢谢DoEvents是最后一次代码重构的补充。 –
然后只是认识到它是值得去除的,因为它是有害的,而不是无用的。 – Servy