Kilgore_Trout_Redux
Executive Member
- Joined
- Sep 20, 2006
- Messages
- 7,498
- Reaction score
- 347
Hi,
I'm busy boning up on my C# skills and wanted to write some code from scratch to help me get my head around delegates and events. The code looks ok but I'm having issues accessing the eventargs from my form in the form of a CrossThreadMessagingException. I know that I need to invoke the call somehow so that it is threadsafe but I'm not sure how to do that the way the code is structured.
The idea is that I am simulating a thermometer that sends readings of temperature and the date of the reading at five second intervals. The readings will be displayed on a form. I want this to be reusable so I don't want the Thermometer class to know anything about what is accessing the delegate.
Here is the code :
For the form :
------------
and the code for the Thermometer class :
Also, any other code review, comments, arsekickery for idiotic coding would be much appreciated.
I'm busy boning up on my C# skills and wanted to write some code from scratch to help me get my head around delegates and events. The code looks ok but I'm having issues accessing the eventargs from my form in the form of a CrossThreadMessagingException. I know that I need to invoke the call somehow so that it is threadsafe but I'm not sure how to do that the way the code is structured.
The idea is that I am simulating a thermometer that sends readings of temperature and the date of the reading at five second intervals. The readings will be displayed on a form. I want this to be reusable so I don't want the Thermometer class to know anything about what is accessing the delegate.
Here is the code :
For the form :
------------
Code:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private Thermometer _therm = new Thermometer();
private void btnStartTempReading_Click(object sender, EventArgs e)
{
_therm.OnTempReading += new Thermometer.TemperatureHandler(ShowTemperatureMeasureMent);
_therm.StartReading();
}
private void ShowTemperatureMeasureMent(Thermometer therm, ThermometerEventArgs e)
{
//string s = e.Temperature.ToString("00.00");
lblTemp.Text = e.Temperature.ToString("00.00");
lblTime.Text = e.TimeOfMeasurement.ToString();
}
private void btnStopTempReading_Click(object sender, EventArgs e)
{
_therm.StopReading();
}
}
and the code for the Thermometer class :
Code:
class ThermometerEventArgs : EventArgs
{
public double Temperature
{
get {
Random rand = new Random();
return (rand.NextDouble() * rand.Next(-50, 50));
}
}
public DateTime TimeOfMeasurement
{
get { return DateTime.Now; }
}
}
class Thermometer
{
#region events and delegates
public event TemperatureHandler OnTempReading;
public delegate void TemperatureHandler(Thermometer therm, ThermometerEventArgs e);
#endregion
private Timer _timer;
public void StartReading()
{
//_state = true;
if ((OnTempReading != null) && (_timer == null))
{
_timer = new Timer(delegate(object o) { OnTempReading(this, new ThermometerEventArgs()); }, null, 5000, 5000);
}
}
public void StopReading()
{
_timer.Dispose();
}
}
Also, any other code review, comments, arsekickery for idiotic coding would be much appreciated.