从计时器线程GUI线程调用方法 [英] Call method on the GUI thread from a timers thread

查看:110
本文介绍了从计时器线程GUI线程调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序正在使用定时器一个RSS feed检查更新,如果有新的项目被发现,我弹出一个定制对话框,告知用户。当我运行检查手动一切都很正常,但是当自动检查中已用事件不显示自定义对话框定时器运行。

In my application I am using a timer to check for updates in an RSS feed, if new items are found I pop up a custom dialog to inform the user. When I run the check manually everything works great, but when the automatic check runs in the timers Elapsed event the custom dialog is not displayed.

首先是这样的一个线程问题? (我假设这是因为无论是手动和自动检查使用相同的代码)。

First of all is this a thread issue? (I am assuming it is because both the manual and automatic check use the same code).

当我运行自动检查,我必须调用运行方法从经过的事件处理程序定时器的支票?

When I run the automatic check, do I have to invoke the method that runs the check from the Timers Elapsed event handler?

有什么我需要在我的自定义对话框类呢?

Is there something I need to do in my custom dialog class?

编辑:
,这是一个WinForms应用程序

this is a winforms application.

下面是代码是什么样的一个例子。 (请不要在此代码示例指出语法错误,这只是一个简单的例子不是真正的代码)。

Here is an example of what the code is like. (Please don't point out syntax errors in this code example, this is just a simple example not real code).

public class MainForm : System.Windows.Forms.Form
{
    //This is the object that does most of the work.
    ObjectThatDoesWork MyObjectThatDoesWork = new ObjectThatDoesWork(); 
    MyObjectThatDoesWork.NewItemsFound += new NewItemsFoundEventHandler(Found_New_Items);

    private void Found_New_Items(object sender, System.EventArgs e)
    {
        //Display custom dialog to alert user.
    }

    //Method that doesn't really exist in my class, 
    // but shows that the main form can call Update for a manual check.
    private void Button_Click(object sender, System.EventArgs e)
    {
        MyObjectThatDoesWork.Update();
    }

    //The rest of MainForm with boring main form stuff
}


public class ObjectThatDoesWork
{
    System.Timers.Timer timer;

    public ObjectThatDoesWork()
    {
        timer = new System.Timers.Timer();
        timer.Interval = 600000;
        timer.AutoReset = true;
        timer.Elapsed += new new System.Timers.ElapsedEventHandler(TimeToWork);
        timer.Start();
    }

    private void TimeToWork(object sender, System.Timers.ElapsedEventArgs e)
    {
        Update();
    }

    public void Update()
    {
        //Check for updates and raise an event if new items are found.
        //The event is consumed by the main form.
        OnNewItemsFound(this);
    }

    public delgate void NewItemsFoundEventHandler(object sender, System.EventArgs e);
    public event NewItemsFoundEventHandler NewItemsFound;
    protected void OnNewItemsFound(object sender)
    {
        if(NewItemsFound != null)
        {
            NewItemsFound(sender, new System.EventArgs());
        }
    }
}



阅读一些评论后和答案,我想我的问题是,我使用的是 System.Timers.Timer 不是 System.Windows.Forms.Timer

编辑:

更改为Forms.Timer初步测试后看起来不错(但没有新项目的存在却又如此没有看到自定义对话框)。我加了一些代码输出线程ID到一个文件时,更新方法被调用。使用Timers.Timer线程ID不是GUI线程,但是使用Forms.Timer线程ID是相同的图形用户界面。

After changing to a Forms.Timer initial testing looks good (but no new items exist yet so have not seen the custom dialog). I added a bit of code to output the thread ID to a file when the update method is called. Using the Timers.Timer the thread ID was not the GUI thread, but using the Forms.Timer the thread ID is the same as the GUI.

推荐答案

您使用哪种计时器? System.Windows.Forms.Timer自动闪光UI线程上的事件。如果你使用的是其他一个你需要使用 Control.Invoke 调用UI上的方法主题。

Which timer are you using? System.Windows.Forms.Timer automatically fires the event on the UI thread. If you are using other one you will need to use Control.Invoke to call the method on UI thread.

这篇关于从计时器线程GUI线程调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆