473,738 Members | 7,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need little help with using multiple threads

hi there,

i created a little test application to send out multiple emails to
addresses, but using threads.

So anyway, what i've got is 1 form with a START button, and a multiline
textbox for recording log output of sending results.
I use the code:

System.Threadin g.Thread t = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(d oMailSend));
t.Start();

and in the function "doMailSend " i have a line:

txtLog.text += "Sending - " + DateTime.Now.To String();

This throws an error because i'm trying to set the log textbox from within
the thread and the error is:
"Cross-thread operation not valid: Control 'txtLog' accessed from a thread
other than the thread it was created on."

any help appreciated in how to set the txtLog.text from within the function
called by the thread.
thanks,
Paul
Mar 24 '07 #1
6 3052
"Milsnips" <mi******@hotma il.comwrote in message
news:OT******** ******@TK2MSFTN GP03.phx.gbl...
>
txtLog.text += "Sending - " + DateTime.Now.To String();

This throws an error because i'm trying to set the log textbox from within
the thread and the error is:
"Cross-thread operation not valid: Control 'txtLog' accessed from a thread
other than the thread it was created on."
You have to use the Invoke method of the form (or another control) to
marshall the execution between threads. Write an auxiliary function:

void SetTextLog()
{
txtLog.text += "Sending - " + DateTime.Now.To String();
}

Then when you want to call it from the other thread do this:

txtLog.Invoke(n ew MethodInvoker(S etTextLog));
Mar 24 '07 #2
Milsnips <mi******@hotma il.comwrote:
i created a little test application to send out multiple emails to
addresses, but using threads.

So anyway, what i've got is 1 form with a START button, and a multiline
textbox for recording log output of sending results.
I use the code:

System.Threadin g.Thread t = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(d oMailSend));
t.Start();

and in the function "doMailSend " i have a line:

txtLog.text += "Sending - " + DateTime.Now.To String();

This throws an error because i'm trying to set the log textbox from within
the thread and the error is:
"Cross-thread operation not valid: Control 'txtLog' accessed from a thread
other than the thread it was created on."

any help appreciated in how to set the txtLog.text from within the function
called by the thread.
See http://pobox.com/~skeet/csharp/threads/winforms.shtml

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 24 '07 #3
Milsnips wrote:
hi there,

i created a little test application to send out multiple emails to
addresses, but using threads.

So anyway, what i've got is 1 form with a START button, and a multiline
textbox for recording log output of sending results.
I use the code:

System.Threadin g.Thread t = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(d oMailSend));
t.Start();

and in the function "doMailSend " i have a line:

txtLog.text += "Sending - " + DateTime.Now.To String();

This throws an error because i'm trying to set the log textbox from within
the thread and the error is:
"Cross-thread operation not valid: Control 'txtLog' accessed from a thread
other than the thread it was created on."

any help appreciated in how to set the txtLog.text from within the function
called by the thread.
thanks,
Paul
You can change a variable in the class from the thread, and let a timer
control in the main thread monitor the variable to see if there are any
changes.

Or you can use the Invoke method from the thread to call a delegate in
the main thread.

--
Göran Andersson
_____
http://www.guffa.com
Mar 24 '07 #4
On Sat, 24 Mar 2007 07:09:32 -0700, Milsnips <mi******@hotma il.comwrote:
[...]
any help appreciated in how to set the txtLog.text from within the
function called by the thread.
Just out of curiosity, did you search in this newsgroup for "cross-thread
operation not valid"?

I'm not trying to be snide...it's just that your question is probably the
single-most-asked question here. I continue to be surprised that given
how often it's already been answered, people still see a need to ask it.

Are you (and perhaps other people) simply unaware of the search tools that
are available for looking up previous answers?

Pete
Mar 24 '07 #5
Bear a principle in mind:
Never aceess UI component directly in work-thread,Let UI-thread do it.

it will help.
http://msdn.microsoft.com/msdnmag/is...asicInstincts/

//sample code:
...
public delegate void SetTextDelegate (string text);
...

//a thread safe method
private SetText(string text)
{
if (!InvokeRequire d == false)
{
txtLog.text=tex t;
}
else
{
this.BeginInvok e(new SetTextDelegate (SetText),new object[]{text});
}
}
....

gshzheng
20070326

"Milsnips" <mi******@hotma il.comдÈëÏûϢРÂÎÅ:OT********* *****@TK2MSFTNG P03.phx.gbl...
hi there,

i created a little test application to send out multiple emails to
addresses, but using threads.

So anyway, what i've got is 1 form with a START button, and a multiline
textbox for recording log output of sending results.
I use the code:

System.Threadin g.Thread t = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(d oMailSend));
t.Start();

and in the function "doMailSend " i have a line:

txtLog.text += "Sending - " + DateTime.Now.To String();

This throws an error because i'm trying to set the log textbox from within
the thread and the error is:
"Cross-thread operation not valid: Control 'txtLog' accessed from a thread
other than the thread it was created on."

any help appreciated in how to set the txtLog.text from within the
function called by the thread.
thanks,
Paul


Mar 26 '07 #6
Sorry:)

that's ok.

private SetText(string text)
{
if (!InvokeRequire d)
{
txtLog.text=tex t;
}
else
{
this.BeginInvok e(new SetTextDelegate (SetText),new object[]{text});
}
}
Mar 26 '07 #7

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

Similar topics

2
1721
by: Serge A. Ribalchenko | last post by:
Hi all, I'm new in python coding... Can someone point me how can I do such a usual thing? I need to ping some hosts, and harvest results in one list. Just cause there is about 120 hosts in the LAN, and approx. 10% is down at working hours, pinging each host at a time from cycle is VERY slow solution. I did: retv = os.popen('ping -c 1 ' + ip + ' |grep \"1 packet\" |awk \'{print $4}\'')
0
1849
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to explain it in details, 1. Python initialization and finalization is done in the *main* thread. 2. For each new thread I create a separate sub-interpreter . 3. Using PyRun_String("import myModule"...) before execution of python
9
1425
by: Klaus Neuner | last post by:
Hello, I wrote a program that does essentially the following: for rule in rules: for line in line_list: line = my_apply(rule, line) line_list contains the lines of some input text.
3
1824
by: adsheehan | last post by:
Hi, Does anyone know the reasoning or pros/cons for either (in a multi-threaded C++ app): - creating many sub-interpreters (Py_NewInterpreter) with a thread state each Or
6
3204
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to 12 threads, which then process a single object off of the stack. I use a loop with a boolean...
6
1348
by: Benjamin Walling | last post by:
We have 109 remote offices all running Sybase ASA Server. We collect data from these offices and consolidate it into our main server. I have written a program that will read from each office and synchronize the data. It takes a while to run because each office only has a 128k Frame Relay. I have two database classes: one for accessing the office, one for accessing the main server. The database classes expose simple functions of...
10
1689
by: HK | last post by:
With VB.NET 2005, and a Windows Form, running on a dual CPU box, I need to take a recordset (e.g. 100,000 records) and spawn a thread to handle an internet XML transaction routine for each of the records. This is a nice use of threading because those internet requests are going against 3rd party servers that often have 1 second latency problems and so handling them with multiple threads is the fastest way to get through all the records in...
113
5299
by: John Nagle | last post by:
The major complaint I have about Python is that the packages which connect it to other software components all seem to have serious problems. As long as you don't need to talk to anything outside the Python world, you're fine. But once you do, things go downhill. MySQLdb has version and platform compatibility problems. So does M2Crypto. The built-in SSL support is weak. Even basic sockets don't quite work right; the socket module...
5
2029
by: bean330 | last post by:
Hey, I'm somewhat new to C# and I need a little help, please! I'm selecting a bunch of records, setting properties on a COM executable and then calling a method on that executable to run. I want to run the executable in separate threads because they can be long-running and it would be optimal for us to run a bunch simultaneously. I've got that part working - it's pretty easy in C#. What I'm having a hard time with is managing the...
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8210
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.