如何检测控件是否失效?

发布于 2024-11-07 08:55:42 字数 995 浏览 4 评论 0原文

我正在实施生产者/消费者问题。代码如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命比纸薄 2024-11-14 08:55:42

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

北斗星光 2024-11-14 08:55:42

您可以尝试使用 new 关键字来使自己的 Invalidate

    public new void Invalidate()
    {
        // place your logic here
        base.Invalidate();
    }

Aslo Form 实现具有 Invalidated 事件在 Ivalidate 结束后触发

编辑:

public void InvalidateEventHandler(object sender, InvalidateEventArgs e)
{
    anotherForm.Invoke(new Action(() =>
    {
        // Do Stuff to form -- Where exception raises
    }));
}

You can try to use new keyword to make your own implementation of Invalidate

    public new void Invalidate()
    {
        // place your logic here
        base.Invalidate();
    }

Aslo Form has Invalidated event wich is triggered after Ivalidate ends

EDIT:

public void InvalidateEventHandler(object sender, InvalidateEventArgs e)
{
    anotherForm.Invoke(new Action(() =>
    {
        // Do Stuff to form -- Where exception raises
    }));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文