小编Vla*_* M.的帖子

如何使用System.Timers.Timer访问/修改另一个线程上的控件?

所以我是计时器和线程的新手,我无法找到如何制作一个调用编辑/修改我的MainWindow控件的函数的计时器.

Xaml代码:

<Window x:Class="TimerTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="479" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

C#代码:

namespace TimerTest
{
    public partial class MainWindow : Window
    {
        //Declaring my aTimer as global
        public static System.Timers.Timer aTimer;
        //Function that is called
        public void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            textBox1.Text += "SomeText ";
        }
        public MainWindow()
        {
            InitializeComponent();
            aTimer = new Timer();
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 1000; // every 5 seconds
            aTimer.Enabled = true;
        }
    } …
Run Code Online (Sandbox Code Playgroud)

c# user-interface controls multithreading timer

3
推荐指数
1
解决办法
9646
查看次数

标签 统计

c# ×1

controls ×1

multithreading ×1

timer ×1

user-interface ×1