473,486 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Threading: UI update from another namespace.

I'm really new to threading.

Imagine you create a thread like this:

public MainForm()
{

InitializeComponent();

Thread trd = new Thread(new ThreadStart(this.threadControl));
trd.IsBackground = true;
trd.Start();

}

public void threadControl()
{
classInAnotherNamespace run = new classInAnotherNamespace ();

run.longMethod();
}

How do I send back messages back to the UI from run.longMethod()? For
instance, I might want to let the user know that the routine is
"Beginning procedure 1" or "Ending procedure 1," etc.

I'd sure appreciate any tips.

Thanks!

--Brent
Nov 17 '05 #1
6 1321
Well, first of all, threading has nothing to do with namespaces. A thread
could care less about what namespace another thread is in. It simply matters
that they're separate threads.

Namespaces are largely just a convenience of organizing for humans to better
understand the code.

As for how to communicate back, there are a number of ways. One simple (and
not terribly elegant way), assuming you only have one instance of MainForm
is to store that instance in a public static field. For example:

public class MainForm
{
public static _mainFormInstance;

public MainForm()
{
_mainFormInstance = this;
...
}

public void ThreadNotification(string message)
{
Console.WriteLine("Thread sent following message: " + message);
}
}
Then from the thread, simply do:

MainForm._mainFormInstance.ThreadNotification("Thi s is a message");

Pete
"Brent" <""b b i g l e r \"@ y a h o o . c o m"> wrote in message
news:11*************@corp.supernews.com...
I'm really new to threading.

Imagine you create a thread like this:

public MainForm()
{

InitializeComponent();

Thread trd = new Thread(new ThreadStart(this.threadControl));
trd.IsBackground = true;
trd.Start();

}

public void threadControl()
{
classInAnotherNamespace run = new classInAnotherNamespace ();

run.longMethod();
}

How do I send back messages back to the UI from run.longMethod()? For
instance, I might want to let the user know that the routine is "Beginning
procedure 1" or "Ending procedure 1," etc.

I'd sure appreciate any tips.

Thanks!

--Brent

Nov 17 '05 #2
<Brent <""b b i g l e r \"@ y a h o o . c o m">> wrote:
I'm really new to threading.

Imagine you create a thread like this:

public MainForm()
{

InitializeComponent();

Thread trd = new Thread(new ThreadStart(this.threadControl));
trd.IsBackground = true;
trd.Start();

}

public void threadControl()
{
classInAnotherNamespace run = new classInAnotherNamespace ();

run.longMethod();
}

How do I send back messages back to the UI from run.longMethod()? For
instance, I might want to let the user know that the routine is
"Beginning procedure 1" or "Ending procedure 1," etc.


See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

Namespaces are irrelevant here - the key is not to try to update the UI
from a different thread. It doesn't matter which namespace you're in.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
Thanks for your replies!

This example is instructive, but I'm still having a hard time wrapping
my head around getting the message back to the main form. The example
uses a MethodInvoker to call an update function within the same
namespace, which then updates a control or what-have-you on the form,
also in the same namespace. But if I do something similar, say, invoking
(in Namespace2) ...

public void output(string value)
{
richTextBox1.AppendText(value + "\n");

}

.... the compiler complains that richTextBox1 doesn't exist. Which it
doesn't, not in Namespace2.

Alternately, if (after referencing in Namespace2 the namespace where
MainForm is located) I try ...

public void output(string value)
{

MainForm.richTextBox1.AppendText(value + "\n");

}

....the compiler tells me I can't access richTextBox1 because of its
protection level.

I must be having some kind of structural problem here.

I do appreciate your help.

Thanks!

--Brent
Jon Skeet [C# MVP] wrote:

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

Namespaces are irrelevant here - the key is not to try to update the UI
from a different thread. It doesn't matter which namespace you're in.

Nov 18 '05 #4
<Brent <""b b i g l e r \"@ y a h o o . c o m">> wrote:
Thanks for your replies!

This example is instructive, but I'm still having a hard time wrapping
my head around getting the message back to the main form. The example
uses a MethodInvoker to call an update function within the same
namespace, which then updates a control or what-have-you on the form,
also in the same namespace. But if I do something similar, say, invoking
(in Namespace2) ...
Please, ignore the whole namespace issue. It's a red herring.
public void output(string value)
{
richTextBox1.AppendText(value + "\n");

}

... the compiler complains that richTextBox1 doesn't exist. Which it
doesn't, not in Namespace2.
The namespace isn't important. What's important is that it doesn't
exist in your *class*. You need to give it a reference to that form
somehow - eg by passing it into the constructor of your other class,
which would obviously have to be modified appropriately.
Alternately, if (after referencing in Namespace2 the namespace where
MainForm is located) I try ...

public void output(string value)
{

MainForm.richTextBox1.AppendText(value + "\n");

}

...the compiler tells me I can't access richTextBox1 because of its
protection level.


So I should hope. I'd also hope that richTextBox1 isn't a static member
- what would you do if you had two MainForms visible?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 18 '05 #5
That's the push I needed! I've got the initial code working now by
passing a RichTextBox reference from namespace1 to namespace2.

Thanks!

--Brent

<snip>

You need to give it a reference to that form
somehow - eg by passing it into the constructor of your other class,
which would obviously have to be modified appropriately.

Nov 18 '05 #6
<Brent <""b b i g l e r \"@ y a h o o . c o m">> wrote:
That's the push I needed! I've got the initial code working now by
passing a RichTextBox reference from namespace1 to namespace2.


No, it's passing it from one *class* to another. As I keep saying,
namespaces are entirely irrelevant here.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 18 '05 #7

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

Similar topics

4
4076
by: Bardo | last post by:
Hi, I have a situation where I am capturing both a WMI event utilising the "ManagementEventWatcher" in the "System.Management" namespace, and a corresponding event ("EntryWritten") raised from...
8
8168
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
17
1477
by: Arun Kumar | last post by:
What is wrong with this code. All i am trying to test is 3 progressbar and one button. On buttonclick i create 3 threads and each thread calls a method which in turn updates the progressbar and it...
3
2362
by: Elliot Rodriguez | last post by:
Hi: I am writing a WinForm app that contains a DataGrid control and a StatusBar control. My goal is to update the status bar using events from a separate class, as well as some other simple...
3
5954
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
8
1491
by: MattB | last post by:
Hello I am starting a new thread in a button click event. This thread calls an method which sends emails, I don't want the page to wait for the emails to finish going out as it slows the user...
1
288
by: James | last post by:
Simple concept. I have a stored procedure call that takes a very long time to run. This is a necessary evil. While this procedure is running, it does a simple update to a table with a %...
7
2347
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
5
6915
by: CCLeasing | last post by:
For an application I'm creating I want to create a 'fake' progress bar. By fake I mean a progress bar that looks like it's doing something but actually isn't. I know philosophically this isn't...
0
6964
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
7123
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
7173
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
7305
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...
1
4863
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...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1378
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 ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
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...

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.