473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.Text = 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.Collections.Generic;
using System.ComponentModel;
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()
{
InitializeComponent();
tl.Reading += new TimerLib.Calculate(Reading);
}

void Reading(double val)
{
i++;
this.label1.Text = val.ToString();

}

private void button1_Click(object sender, EventArgs e)
{
tl.Start();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Timers;

namespace LabExampleLib
{
public class TimerLib
{
public delegate void Calculate(double val);
public event Calculate Reading;

private double i = 0;
System.Timers.Timer aTimer;

public TimerLib()
{
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
}

private void OnTimedEvent(object source, ElapsedEventArgs e)
{
i++;
OnReading(i);
}

public void Start()
{
aTimer.Interval = 1000;
aTimer.Enabled = true;

}

protected void OnReading(double val)
{
if (Reading != null)
Reading(val);
}
}
}
Thank You

Peter
Sep 23 '08 #1
2 2079
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....

http://msdn.microsoft.com/en-us/libr...28(VS.80).aspx
// 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.Text = text;
}
}
Good luck,-Drew"Peter" <cz****@nospam.nospamwrote in message
news:ei**************@TK2MSFTNGP03.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.Text = 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.Collections.Generic;
using System.ComponentModel;
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()
{
InitializeComponent();
tl.Reading += new TimerLib.Calculate(Reading);
}

void Reading(double val)
{
i++;
this.label1.Text = val.ToString();

}

private void button1_Click(object sender, EventArgs e)
{
tl.Start();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Timers;

namespace LabExampleLib
{
public class TimerLib
{
public delegate void Calculate(double val);
public event Calculate Reading;

private double i = 0;
System.Timers.Timer aTimer;

public TimerLib()
{
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
}

private void OnTimedEvent(object source, ElapsedEventArgs e)
{
i++;
OnReading(i);
}

public void Start()
{
aTimer.Interval = 1000;
aTimer.Enabled = true;

}

protected void OnReading(double val)
{
if (Reading != null)
Reading(val);
}
}
}
Thank You

Peter

Sep 23 '08 #2

"DrewCE" <moc.sgodniahc@werd - backwardswrote in message
news:24**********************************@microsof t.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....

http://msdn.microsoft.com/en-us/libr...28(VS.80).aspx
// 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.Text = text;
}
}
Good luck,-Drew"Peter" <cz****@nospam.nospamwrote in message
news:ei**************@TK2MSFTNGP03.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.Text = 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.Collections.Generic;
using System.ComponentModel;
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()
{
InitializeComponent();
tl.Reading += new TimerLib.Calculate(Reading);
}

void Reading(double val)
{
i++;
this.label1.Text = val.ToString();

}

private void button1_Click(object sender, EventArgs e)
{
tl.Start();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Timers;

namespace LabExampleLib
{
public class TimerLib
{
public delegate void Calculate(double val);
public event Calculate Reading;

private double i = 0;
System.Timers.Timer aTimer;

public TimerLib()
{
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
}

private void OnTimedEvent(object source, ElapsedEventArgs e)
{
i++;
OnReading(i);
}

public void Start()
{
aTimer.Interval = 1000;
aTimer.Enabled = true;

}

protected void OnReading(double val)
{
if (Reading != null)
Reading(val);
}
}
}
Thank You

Peter

Thanks the problem is solved.
Sep 23 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: bill drescher | last post by:
I have a long table and I want to have the alternating rows slightly different colors for clarity. Is there any way to avoid using a class designation in each tr ? (yes, it is for tabular data...
0
by: Nathan | last post by:
Hi, I seem to having a peculiar problem with the display of odd and even pages in XSL-FO. Here is a small background of the problem. My xsl stylesheet mentions my fo:layout-master-set as ...
34
by: Frederick Gotham | last post by:
I'm trying to write extremely efficient, fully portable algorithms to determine if an integer is odd or even. The most basic algorithms would be: #define IS_ODD(x) ((x) % 2) #define IS_EVEN(x)...
17
by: Ron | last post by:
I want to write a program that will accept a number in a textbox for example 23578 and then in a label will display the sum of the odd and even number like this... the textbox containsthe number...
6
by: Klaas Vantournhout | last post by:
Hi, I have a question, which is just out of interest. What is the fastest way to do an odd/even check with c++ and if needed assembler. Assume n is an unsigned integer like type (unsigned...
1
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge...
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
18
by: =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= | last post by:
Hello. It seems a year is all it takes for one's proficiency in C++ to become too rusty. Professionally, I've been away from the language (and from programming in general), but I still preserve...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.