Even And Deligate

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter

    Even And Deligate

    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


  • DrewCE

    #2
    Re: Even And Deligate

    This, it seems, is a right of passage for beginning .Net users.

    You are only allowed to update the UI from the UI thread. See the following
    link and code excerpt from said link....


    // This method demonstrates a pattern for making thread-safe
    // calls on a Windows Forms control.
    //
    // If the calling thread is different from the thread that
    // created the TextBox control, this method creates a
    // SetTextCallback and calls itself asynchronously using the
    // Invoke method.
    //
    // If the calling thread is the same as the thread that created
    // the TextBox control, the Text property is set directly.

    private void SetText(string text)
    {
    // InvokeRequired required compares the thread ID of the
    // calling thread to the thread ID of the creating thread.
    // If these threads are different, it returns true.
    if (this.textBox1. InvokeRequired)
    {
    SetTextCallback d = new SetTextCallback (SetText);
    this.Invoke(d, new object[] { text });
    }
    else
    {
    this.textBox1.T ext = text;
    }
    }
    Good luck,-Drew"Peter" <czupet@nospam. nospamwrote in message
    news:eiCKmkTHJH A.4884@TK2MSFTN GP03.phx.gbl...
    >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
    >
    >

    Comment

    • Peter

      #3
      Re: Even And Deligate


      "DrewCE" <moc.sgodniahc@ werd - backwardswrote in message
      news:24D8C930-10D7-47DF-99CE-AA89AE70471C@mi crosoft.com...
      This, it seems, is a right of passage for beginning .Net users.
      >
      You are only allowed to update the UI from the UI thread. See the
      following link and code excerpt from said link....
      >

      // This method demonstrates a pattern for making thread-safe
      // calls on a Windows Forms control.
      //
      // If the calling thread is different from the thread that
      // created the TextBox control, this method creates a
      // SetTextCallback and calls itself asynchronously using the
      // Invoke method.
      //
      // If the calling thread is the same as the thread that created
      // the TextBox control, the Text property is set directly.
      >
      private void SetText(string text)
      {
      // InvokeRequired required compares the thread ID of the
      // calling thread to the thread ID of the creating thread.
      // If these threads are different, it returns true.
      if (this.textBox1. InvokeRequired)
      {
      SetTextCallback d = new SetTextCallback (SetText);
      this.Invoke(d, new object[] { text });
      }
      else
      {
      this.textBox1.T ext = text;
      }
      }
      Good luck,-Drew"Peter" <czupet@nospam. nospamwrote in message
      news:eiCKmkTHJH A.4884@TK2MSFTN GP03.phx.gbl...
      >>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
      >>
      >>
      >
      Thanks the problem is solved.


      Comment

      Working...