-4

我正在开发一个包含两个主要处理的项目,我尝试使用 timer_tick 来处理使应用程序非常慢的处理。应用程序需要始终运行,我希望 timer_tick 的计时器方面每 X 秒触发一次方法,但使用多线程,因为这使它更快。

任何人都可以帮忙吗?

该应用程序的当前结构如下:

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        setting_info();
    }

    public void setting_info()
    {
        // takes data from config file for API connections
    }

    private void swis()
    {
        // connects to API and fetches data
        // need to be continuously running - Ideally Interval(1200)
    }

    private void st_processing()
    {
        // processes the data that was fetched in swis() slows down the program without multiple threading
        // need to be continuously running - Ideally Interval(800)
    }

    private void alert_in_timer_Tick(object sender, EventArgs e)
    {            
        while (alert_in == true)
        {
            swis();
            break;
        }
    }

     private void st_processing_timer_Tick(object sender, EventArgs e)
    {
        while (st_processing == true && alert_data_database_has_data == true)
        {
            st_processing();
            //check_error_count();
            alert_data_database_has_data = false;
            break;
        }
    }
}
4

1 回答 1

0

不清楚你想知道什么。有很多很好的 C# 线程教程,如这里所讨论的,例如这个

启动线程很容易

Thread worker = new Thread(new ThreadStart(swis));
//...
worker.join();

如果由于线程的作用而需要更新 GUI,则需要小心。以前在这里讨论过。

此刻你打破了alert_in_timer_Tick它被称为swis函数的那一刻。

while (alert_in == true)
{
    swis();
    break;// why is this here?
}
于 2013-07-29T09:52:01.543 回答