Timer OnTimedEvent isn't updating my form
Hi.
I'm recently getting back into programming.
As far as I can tell, this should work.
The timer does work, and my Console does write the proper value.
It could be that I'm just trying to call object the wrong way.
I'm trying to update the label called txt_Monitor
Code:
public Monitor()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
System.Timers.Timer aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 2000;
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Worked");
string text = System.IO.File.ReadAllText(@"G:\Monitor.txt");
Monitor Test = new Monitor();
Test.txt_Monitor.Text = text + "/125";
Console.WriteLine(text);
}
Re: Timer OnTimedEvent isn't updating my form
Can you give the rest of the code, including the Form1 class? What you have here doesn't make sense out of context. You have a constructor called Monitor, which means that this code is inside a class called Monitor, yet you reference a 'Form1' object.
Without knowing what exactly is going on, try the following code to see if it fixes your issue.
Code:
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Worked");
string text = System.IO.File.ReadAllText(@"G:\Monitor.txt");
txt_Monitor.Text = text + "/125";
Console.WriteLine(text);
}
Re: Timer OnTimedEvent isn't updating my form
Here is the full code.
Code:
namespace WindowsFormsApplication1
{
public partial class CLIMonitor : Form
{
public CLIMonitor()
{
InitializeComponent();
}
public void CLIMonitor_Load(object sender, EventArgs e)
{
System.Timers.Timer aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 200;
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
public static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Worked");
string textz = System.IO.File.ReadAllText(@"G:\CLIMonitor.txt");
CLIMonitor Test = new CLIMonitor();
Test.txt_CLI.Text = textz;
Console.WriteLine(textz);
}
}
}
I get no errors. Just doesn't update the Text field.
If I remove the constructor reference (ie: just having txt_CLI.Text = textz;), I get the following message:
Error 1 An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.CLIMonitor.txt_CLI'
Re: Timer OnTimedEvent isn't updating my form
I have no idea what it is that I'm doing wrong.
All I'm trying to do is update a textbox field every X seconds.
The value being in a text file (No problem here).
Getting the console to recognize the value (No problem here).
Getting an a form object to update with anything at all... (can't figure out why it isn't updating).