简体   繁体   English

C#自动检查功能

[英]C# AutoCheck Function

I have a function called Checking() to update my UI. 我有一个称为Checking()的函数来更新我的UI。 I'd like to make this function AutoRun and every 1 Second it runs it and updates my UI. 我想让此功能自动运行,并每1秒运行一次并更新我的UI。

How can I do that? 我怎样才能做到这一点?

Here is my function: 这是我的功能:

public MainWindow()
{
    InitializeComponent();
    Checking()
}

public void Checking()
{
    if (status= Good)
        UI.color.fill= Green
    else
        UI.color.Fill = Red
}

This code can help you 此代码可以帮助您

//need to add  System.Timers in usings
using System.Timers;

//inside you code
//create timer with interval 2 sec
Timer timer=new Timer(2000);
//add eventhandler 
timer.Elapsed+=new ElapsedEventHandler(timer_Elapsed);
//start timer
timer.Start();


private void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        MessageBox.Show("324");
        //or other actions
    }

You'll want to make sure the changes made in Checking() are bound and a IPropertyNotifyChange sent for it. 您将需要确保Checking()中所做的更改已绑定,并为此发送了IPropertyNotifyChange。

using System.Reactive;
public MainWindow()
{
    InitializeComponent();
    Observable.Interval(TimeSpan.FromSeconds(1))
        .Subscribe( _ => Checking() );
}

DispathTimer is timer, that working in one thread with UI. DispathTimer是计时器,使用UI在一个线程中工作。 This code can help you 此代码可以帮助您

 public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();

        DispatcherTimer timer = new DispatcherTimer(){Interval = new TimeSpan(0,0,0,1)};
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
    }

    void timer_Tick(object sender, EventArgs e) {
        Checking();
    }

    public void Checking()
    {   
       .....
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM