need little help with using multiple threads

  • Thread starter Thread starter Milsnips
  • Start date Start date
M

Milsnips

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.Threading.Thread t = new System.Threading.Thread(new
System.Threading.ThreadStart(doMailSend));
t.Start();

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

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

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
 
Milsnips said:
txtLog.text += "Sending - " + DateTime.Now.ToString();

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.ToString();
}

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

txtLog.Invoke(new MethodInvoker(SetTextLog));
 
Milsnips said:
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.Threading.Thread t = new System.Threading.Thread(new
System.Threading.ThreadStart(doMailSend));
t.Start();

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

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

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
 
Milsnips said:
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.Threading.Thread t = new System.Threading.Thread(new
System.Threading.ThreadStart(doMailSend));
t.Start();

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

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

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.
 
[...]
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
 
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/issues/04/05/BasicInstincts/

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

//a thread safe method
private SetText(string text)
{
if (!InvokeRequired == false)
{
txtLog.text=text;
}
else
{
this.BeginInvoke(new SetTextDelegate(SetText),new object[]{text});
}
}
....

gshzheng
20070326
 
Sorry:)

that's ok.

private SetText(string text)
{
if (!InvokeRequired)
{
txtLog.text=text;
}
else
{
this.BeginInvoke(new SetTextDelegate(SetText),new object[]{text});
}
}
 
Back
Top