我使用GTK#,我需要在主窗口关闭时中止一个线程。该线程附加到主要内部窗口小部件。我试图使用删除和销毁事件。我失败了。
public class MyClass {
GLWidget widget= new AnyWidget();
public GLWidget widget{
get { return widget; }
}
private float red = 0.0f;
public MyClass ()
{
Thread producer = new Thread (new ThreadStart (ThreadRun));
producer.Start ();
widget.DeleteEvent += delegate { // I don't know what event I should use
producer.Abort ();
};
}
public void ThreadRun()
{
while (true) {
//TODO
}
}
}
问题是,当我按下关闭按钮时,我的GTK#应用程序没有被关闭。
答案 0 :(得分:0)
将您的生产者线程声明为该类的公共成员。
public class MyClass {
public Thread producer = new Thread (new ThreadStart (ThreadRun));
//...
}
然后你应该添加一个事件处理程序,如下所示:
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
producer.Abort ();
}
最后,只需将其分配给您的活动
widget.DeleteEvent +=OnDeleteEvent;