473,398 Members | 2,088 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,398 software developers and data experts.

Communicating Between Threads

Hello:

I want to have a class that sends messages to a textbox. However, I
want the message sender to be in a different thread than the message
displayer, so users can scroll through the messages.

How do I communicate between these threads?

Thanks,
Travis

Jul 5 '07 #1
5 1774
Travis,

You will have make an implementation of ISynchronizeInvoke available to
the thread so that you can call the Invoke method on it, passing a delegate
to be executed on the UI thread. The Control class implements
ISynchronizeInvoke, so you can use any control which was created on the UI
thread.

Basically, assume your textbox is visible to the method that is being
executed on the thread, you could do this:

public void ThreadEntryPoint()
{
// Do work here.

// Update the textbox.
statusTextbox.Invoke(new Action<T>(UpdateStatus), new object[]{ "Updated
status." });
}

private void UpdateStatus(string status)
{
statusTextbox.Text = status;
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<je**********@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
Hello:

I want to have a class that sends messages to a textbox. However, I
want the message sender to be in a different thread than the message
displayer, so users can scroll through the messages.

How do I communicate between these threads?

Thanks,
Travis

Jul 5 '07 #2
You need to hook into the textbox thread, as, by the sounds of it, you've
figured out already.
Take a look at the code below for how to do this using Invoke...

Cheers.
Dan.
private object listLock = new object();
public delegate void AddTextDelegate(string myText);

/// <summary>
/// Adds the text to a text box
/// </summary>
/// <param name="fi">The fi.</param>
public void AddText(string myText)
{
if (_myTextBox == null)
return;

if (_myTextBox.InvokeRequired)
{
_myTextBox.Invoke(new AddTextDelegate(AddText), new object[]
{myText});
}
else
{
lock (listLock)
{
_myTextBox.Text = text
}

}
}

<je**********@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
Hello:

I want to have a class that sends messages to a textbox. However, I
want the message sender to be in a different thread than the message
displayer, so users can scroll through the messages.

How do I communicate between these threads?

Thanks,
Travis

Jul 5 '07 #3
On Thu, 05 Jul 2007 07:43:01 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
[...]
public void ThreadEntryPoint()
{
// Do work here.

// Update the textbox.
statusTextbox.Invoke(new Action<T>(UpdateStatus),
new object[]{ "Updated status." });
}
I think instead of "Action<T>" you meant to write "Action<string>". Right?

Personally, I prefer the anonymous delegate method unless there's a
specific need for the method being invoked to be reused elsewhere (e.g.
it's more complicated than just a single assignment and you do the same
thing from other places in the code):

statusTextbox.Invoke(new MethodInvoker(delegate() { statusTextbox.Text
= "Updated status"; }));

Of course, the string "Updated status" can be replaced by whatever is
suitable in the context, like a variable referring to a string, or the
result of a call to some variable's ToString() method, or whatever.

And of course, either way is fine. There's nothing wrong with using a
non-anonymous delegate method.

Pete
Jul 5 '07 #4
On Thu, 05 Jul 2007 07:49:11 -0700, Daniel Bass
<da***********@blueCAPSbottle.comFIRSTwrote:
[...]
public void AddText(string myText)
{
if (_myTextBox == null)
return;

if (_myTextBox.InvokeRequired)
{
_myTextBox.Invoke(new AddTextDelegate(AddText), new
object[]
{myText});
}
else
{
lock (listLock)
{
_myTextBox.Text = text
}

}
Why the "lock()" statement? The whole point of using Invoke() is that you
are not allowed to operate on the Control from other threads. The check
for InvokeRequired guarantees that only one thread, the Control instance's
owning thread, will ever execute the code within the "lock()", so there
should never be any other thread that might take the lock. For sure, no
other thread can take the lock using the AddText() method, and it should
not be taking the lock from any other code either.

Pete
Jul 5 '07 #5
Yes, Action<stringis what was meant.

It doesn't matter either way, like you said, but I felt that it wasn't a
bad idea to encapsulate the logic for updating the status. However, if
that's all that needs to be done, then it really doesn't matter.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 05 Jul 2007 07:43:01 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
[...]
public void ThreadEntryPoint()
{
// Do work here.

// Update the textbox.
statusTextbox.Invoke(new Action<T>(UpdateStatus),
new object[]{ "Updated status." });
}
I think instead of "Action<T>" you meant to write "Action<string>". Right?

Personally, I prefer the anonymous delegate method unless there's a
specific need for the method being invoked to be reused elsewhere (e.g.
it's more complicated than just a single assignment and you do the same
thing from other places in the code):

statusTextbox.Invoke(new MethodInvoker(delegate() { statusTextbox.Text
= "Updated status"; }));

Of course, the string "Updated status" can be replaced by whatever is
suitable in the context, like a variable referring to a string, or the
result of a call to some variable's ToString() method, or whatever.

And of course, either way is fine. There's nothing wrong with using a
non-anonymous delegate method.

Pete
Jul 5 '07 #6

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

Similar topics

1
by: Martin | last post by:
This is probably a very easy one but I just can't put my finger on the communicating events name:- I have a MDIform with a couple of child forms. The child forms scroll vertically at run time...
2
by: Eric Brunel | last post by:
Hi all, We're trying to communicate with Microsoft Visual C++ 6.0 via COM to make it perform a few operations. Our main need is to set a breakpoint on a given line in a given file. We ended up...
3
by: Michael Sparks | last post by:
Hi, I'm posting a link to this since I hope it's of interest to people here :) I've written up the talk I gave at ACCU Python UK on the Kamaelia Framework, and it's been published as a BBC...
0
by: David Dolheguy | last post by:
I have written an application using VB.Net 2003 which contains a single timer control (Interval set to 5 seconds) and a function which uses the BeginReceiveFrom Socket function. The problem I am...
1
by: valere | last post by:
I look for information for communicating between JavaScript and a native plugin. I noticed that LiveConnect is oriented to communicate as a "bridge" between Java and JS. But how to process with a...
3
by: Stan | last post by:
Hallo, I have developed an application in MS Access 2000 (Polish version) under MS Windows XP prof (also Polish). Now I would like to run this code on MS Windows XP EN and MS Access XP EN. I have...
6
by: Joe Jax | last post by:
I have an object that spawns a worker thread to process one of its methods. That method processes methods on a collection of other objects. During this processing, a user may request to cancel the...
6
by: dolulob | last post by:
Hi, I'm trying to communicate with a console application through a c# program. the console application is micq a console based ICQ client. I want to be able to send an receive messages through...
2
by: aaronj1335 | last post by:
I am trying to write an application that controls a robot via a serial port. I made a robot class that includes the serial port over which I send commands, and I would like to be able to have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.