Click here to Skip to main content
15,923,389 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# MySQL Question Pin
Shameel14-Aug-11 22:06
professionalShameel14-Aug-11 22:06 
Generaldon't use Parse Pin
Luc Pattyn15-Aug-11 1:35
sitebuilderLuc Pattyn15-Aug-11 1:35 
GeneralRe: don't use Parse Pin
Shameel15-Aug-11 6:37
professionalShameel15-Aug-11 6:37 
AnswerRe: C# MySQL Question Pin
Luc Pattyn15-Aug-11 1:40
sitebuilderLuc Pattyn15-Aug-11 1:40 
AnswerRe: C# MySQL Question Pin
Gonzalo Cao15-Aug-11 21:24
Gonzalo Cao15-Aug-11 21:24 
QuestionIntersection between two rectangles in 3D Pin
Ali Habib13-Aug-11 22:42
Ali Habib13-Aug-11 22:42 
AnswerRe: Intersection between two rectangles in 3D Pin
Peter_in_278014-Aug-11 12:35
professionalPeter_in_278014-Aug-11 12:35 
QuestionCrossThreadMessagingException when using an anonymous delegate as part of an event. Pin
BobsLawnService13-Aug-11 2:40
BobsLawnService13-Aug-11 2:40 
Hi,

I am trying to bone up on my C# skills and have written a new project from scratch. What I am doing is writing a class to simulate a thermometer that returns a temperature measurement and the time of the measurement at fixed intervals. This class will be consumed by a for and the temperature and date displayed on that form as it is updated.

I know that the form cannot be updated by the thread in the Thermometer class because I get a CrossThreadMessagingException

I know that I am somehow supposed to invoke the delegate but in the context of the code I haev written i really don't know how to do that. Could someone show me thow to change the code to achieve this and explain why that code works?

Here is the complete code I have written :

The Form :

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)
    {
        lblTemp.Text = e.Temperature.ToString("00.00");
        lblTime.Text = e.TimeOfMeasurement.ToString();
    }

    private void btnStopTempReading_Click(object sender, EventArgs e)
    {
        _therm.StopReading();
    }


and here is the code for the Thermometer class :

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 System.Threading.Timer _timer;
    private Form _form;

    public void StartReading()
    {
        if ((OnTempReading != null) && (_timer == null))
        {
            _timer = new System.Threading.Timer(delegate(object o) { OnTempReading(this, new ThermometerEventArgs()); }, null, 5000, 5000);
        }
    }

    public void StopReading()
    {
        _timer.Dispose();
    }
}


If it isn't possible to do this with this approach any advice will also be appreciated.

Thank you very much in advance for any assistance.
AnswerRe: CrossThreadMessagingException when using an anonymous delegate as part of an event. Pin
Ravi Bhavnani13-Aug-11 4:28
professionalRavi Bhavnani13-Aug-11 4:28 
GeneralRe: CrossThreadMessagingException when using an anonymous delegate as part of an event. Pin
BobsLawnService13-Aug-11 4:48
BobsLawnService13-Aug-11 4:48 
GeneralRe: CrossThreadMessagingException when using an anonymous delegate as part of an event. Pin
Ravi Bhavnani13-Aug-11 6:26
professionalRavi Bhavnani13-Aug-11 6:26 
AnswerRe: CrossThreadMessagingException when using an anonymous delegate as part of an event. Pin
PIEBALDconsult14-Aug-11 18:16
mvePIEBALDconsult14-Aug-11 18:16 
QuestionUdpClient.Send() hangs Pin
mav.northwind13-Aug-11 0:52
mav.northwind13-Aug-11 0:52 
QuestionInserting IMAGE FILES OR JPG,BMP file into MS-ACCESS TABLE Pin
M Riaz Bashir13-Aug-11 0:13
M Riaz Bashir13-Aug-11 0:13 
AnswerRe: Inserting IMAGE FILES OR JPG,BMP file into MS-ACCESS TABLE Pin
Peter_in_278013-Aug-11 0:54
professionalPeter_in_278013-Aug-11 0:54 
GeneralRe: Inserting IMAGE FILES OR JPG,BMP file into MS-ACCESS TABLE Pin
M Riaz Bashir13-Aug-11 1:07
M Riaz Bashir13-Aug-11 1:07 
Questioncode calibration in the wiimote Pin
Trieu Hau12-Aug-11 15:36
Trieu Hau12-Aug-11 15:36 
AnswerRe: code calibration in the wiimote Pin
OriginalGriff12-Aug-11 21:27
mveOriginalGriff12-Aug-11 21:27 
AnswerRe: code calibration in the wiimote Pin
DaveyM6912-Aug-11 23:59
professionalDaveyM6912-Aug-11 23:59 
AnswerRe: code calibration in the wiimote Pin
Eddy Vluggen13-Aug-11 0:27
professionalEddy Vluggen13-Aug-11 0:27 
QuestionCould not retrieve schema information for table or view ! Pin
nassimnastaran12-Aug-11 10:30
nassimnastaran12-Aug-11 10:30 
AnswerRe: Could not retrieve schema information for table or view ! Pin
nassimnastaran13-Aug-11 2:26
nassimnastaran13-Aug-11 2:26 
QuestionAppend to file that is in use? Pin
sheldons12-Aug-11 6:15
sheldons12-Aug-11 6:15 
AnswerRe: Append to file that is in use? Pin
SledgeHammer0112-Aug-11 6:47
SledgeHammer0112-Aug-11 6:47 
AnswerRe: Append to file that is in use? Pin
jschell12-Aug-11 8:20
jschell12-Aug-11 8:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.