如何检测控件是否失效?
我正在实施生产者/消费者问题。代码如下所示:
void producer()
{
// produce item
// update some control in form
}
void consumer()
{
// consume item
// update some control in form
}
生产者和消费者方法在与创建表单的线程不同的线程中执行,因此我无法更新表单中的控件。我尝试了以下代码:
void producer()
{
// produce item
// put the work to be done in a queue
this.Invalidate();
}
void consumer()
{
// consume item
// put the work to be done in a queue
this.Invalidate();
}
所以现在我必须检测表单是否已失效。我查看了 Form 的事件列表,我能找到的最好的事情就是绘制事件。我输入了完成工作的代码,并且运行良好。问题是我有点怀疑我是否以正确的方式做到了这一点,尽管它有效。我认为绘画不是做这项工作的正确地方,因为我所做的不仅仅是绘画。我想知道是否有更好的方法来做到这一点。
编辑 - 无效事件处理程序的片段不起作用
public Form1()
{
InitializeComponent();
this.Invalidated += InvalidateEventHandler;
}
void producer(object o)
{
// produce
// put work in queue
this.Invalidate();
}
public void InvalidateEventHandler(object sender, InvalidateEventArgs e)
{
// Do Stuff to form -- Where exception raises
}
I'm implementing producer/consumer problem. the code looks like this:
void producer()
{
// produce item
// update some control in form
}
void consumer()
{
// consume item
// update some control in form
}
producer and consumer methods are executed in different threads from the one that created my form, so I can't update controls in form. I tried following code:
void producer()
{
// produce item
// put the work to be done in a queue
this.Invalidate();
}
void consumer()
{
// consume item
// put the work to be done in a queue
this.Invalidate();
}
So now I have to detect if the form has been invalidated. I looked in Form's event list, and the best thing I could find was paint event. I put the code that got the job done, and it works fine. The problem is I somehow doubt I've done this the right way although it works. I think paint is not the right place to do the job, as what I'm doing it not just painting. I was wondering if there's a better way to do this.
Edit -- Snippet for Invalidated event handler not working
public Form1()
{
InitializeComponent();
this.Invalidated += InvalidateEventHandler;
}
void producer(object o)
{
// produce
// put work in queue
this.Invalidate();
}
public void InvalidateEventHandler(object sender, InvalidateEventArgs e)
{
// Do Stuff to form -- Where exception raises
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Invalidate 的目的是触发 Paint。
您需要的是在表单上
Control.Invoke()
您自己的刷新方法。编辑:
您的非 GUI 线程甚至不应该调用 Invalidate(),它们无法触及 GUI。
您可以编写自己的 ProcessData() 表单方法,并从 Prod/Cons 调用
mainForm.Invoke(ProcessData)
然后
ProcessData()
负责线程安全访问数据和使 GUI 无效Invalidate is intended to trigger a Paint.
What you need is to
Control.Invoke()
your own refresh method on he form.Edit:
Your non-GUI threads should not even call Invalidate(), they can't touch the GUI.
You can write your own ProcessData() form-method and from the Prod/Cons call
mainForm.Invoke(ProcessData)
Then
ProcessData()
is responsible for thread-safe access to the data and for Invalidating the GUI您可以尝试使用
new
关键字来使自己的 InvalidateAslo Form 实现具有 Invalidated 事件在 Ivalidate 结束后触发
编辑:
You can try to use
new
keyword to make your own implementation of InvalidateAslo Form has Invalidated event wich is triggered after Ivalidate ends
EDIT: