I have the following code which uses Events and Deligate, I need to have a
timer in a library and update UI everytime the event fires.
but I am getting the following error in Reading method and the following
statement this.label1.Tex t = val.ToString(); :
Cross-thread operation not valid: Control 'label1' accessed from a thread
other than the thread it was created on.
How can I fix this problem?
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using LabExampleLib;
namespace LabExample
{
public partial class Form1 : Form
{
TimerLib tl = new TimerLib();
int i = 0;
public Form1()
{
InitializeCompo nent();
tl.Reading += new TimerLib.Calcul ate(Reading);
}
void Reading(double val)
{
i++;
this.label1.Tex t = val.ToString();
}
private void button1_Click(o bject sender, EventArgs e)
{
tl.Start();
}
}
}
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Threadin g;
using System.Timers;
namespace LabExampleLib
{
public class TimerLib
{
public delegate void Calculate(doubl e val);
public event Calculate Reading;
private double i = 0;
System.Timers.T imer aTimer;
public TimerLib()
{
aTimer = new System.Timers.T imer();
aTimer.Elapsed += new ElapsedEventHan dler(OnTimedEve nt);
}
private void OnTimedEvent(ob ject source, ElapsedEventArg s e)
{
i++;
OnReading(i);
}
public void Start()
{
aTimer.Interval = 1000;
aTimer.Enabled = true;
}
protected void OnReading(doubl e val)
{
if (Reading != null)
Reading(val);
}
}
}
Thank You
Peter
timer in a library and update UI everytime the event fires.
but I am getting the following error in Reading method and the following
statement this.label1.Tex t = val.ToString(); :
Cross-thread operation not valid: Control 'label1' accessed from a thread
other than the thread it was created on.
How can I fix this problem?
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using LabExampleLib;
namespace LabExample
{
public partial class Form1 : Form
{
TimerLib tl = new TimerLib();
int i = 0;
public Form1()
{
InitializeCompo nent();
tl.Reading += new TimerLib.Calcul ate(Reading);
}
void Reading(double val)
{
i++;
this.label1.Tex t = val.ToString();
}
private void button1_Click(o bject sender, EventArgs e)
{
tl.Start();
}
}
}
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Threadin g;
using System.Timers;
namespace LabExampleLib
{
public class TimerLib
{
public delegate void Calculate(doubl e val);
public event Calculate Reading;
private double i = 0;
System.Timers.T imer aTimer;
public TimerLib()
{
aTimer = new System.Timers.T imer();
aTimer.Elapsed += new ElapsedEventHan dler(OnTimedEve nt);
}
private void OnTimedEvent(ob ject source, ElapsedEventArg s e)
{
i++;
OnReading(i);
}
public void Start()
{
aTimer.Interval = 1000;
aTimer.Enabled = true;
}
protected void OnReading(doubl e val)
{
if (Reading != null)
Reading(val);
}
}
}
Thank You
Peter
Comment