So I'm new to timers and threads and I can't find out how to make a timer that calls a function that edits/modify a control on my MainWindow.
Xaml Code:
<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>
C# Code:
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;
}
}
}
The Error i get: "The calling thread cannot access this object because a different thread owns it." P.S.: Also I'm not so good with delegates so if you think one might help me in this case, then please post a code sample.