473,505 Members | 13,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread Doubt.

Ram
Hi,
I have two threads, one parent thread containing UI and a child thread
waiting for some events from the parent thread. Now, I have two issues: 1)
to keep the child thread active till the end of parent thread. 2) Fire event
from the parent thread to child thread.

To keep the child thread active, i tried different means including
while(1){ application.DoEvents()} but seems that it is not working. I am
unable to make it work.

For the event, I am able to fire events from the child to parent using
delegates, But how to fire it from the parent to child?

Thanx
Ram
Nov 16 '05 #1
7 1756
Hi Ram.

Usually what you do is send the event from the worker thread to the UI
thread, not the opposite.
Why are you keeping a worker thread if it;s not working? Can you create the
thread as a response to the event in the UI and then when the worker thread
finish it does inform the UI it's done? That would be a possible solution.

You have two possible solution if you do not want to change your current
solution
1- A busy waiting ( dont remember if this is the correct term ) you do
expect for a variable to obtain a value, the value is set in the UI thread:

while( true )
{
while( Sleep){}
//Do processing
}

then in the UI thread you do Sleep = false to trigger the worker thread.

This is not the best solution, IMO. also remember to include locking code
when changing the shared variable

2- Put the thread to sleep and then later wake it up
// thread code
//Do processing...
Thread.CurrentThread.Suspend();

//In the UI code to resume the thread.
threadInstance.Resume();
Again, I think that you should rework the code for being the worker thread
the one that send the event to the UI ( using Control.Invoke )
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ram" <ra*@unitedprotech.com> wrote in message
news:Oz**************@TK2MSFTNGP12.phx.gbl...
Hi,
I have two threads, one parent thread containing UI and a child thread
waiting for some events from the parent thread. Now, I have two issues: 1)
to keep the child thread active till the end of parent thread. 2) Fire
event
from the parent thread to child thread.

To keep the child thread active, i tried different means including
while(1){ application.DoEvents()} but seems that it is not working. I am
unable to make it work.

For the event, I am able to fire events from the child to parent using
delegates, But how to fire it from the parent to child?

Thanx
Ram

Nov 16 '05 #2
Perhaps I am missing something, but why would you have a thread spun up
that has nothing to do. If all the child thread is doing is waiting for
an event from the parent, why not have the parent use a thread from the
thread pool when a new event occurs that it cares about. Another option
would be to use an AsynchDelegate ... which may give you some other
benefits as far as error handling ..etc..

John

Ram wrote:
Hi,
I have two threads, one parent thread containing UI and a child thread
waiting for some events from the parent thread. Now, I have two issues: 1)
to keep the child thread active till the end of parent thread. 2) Fire event
from the parent thread to child thread.

To keep the child thread active, i tried different means including
while(1){ application.DoEvents()} but seems that it is not working. I am
unable to make it work.

For the event, I am able to fire events from the child to parent using
delegates, But how to fire it from the parent to child?

Thanx
Ram

Nov 16 '05 #3
Ram
Ok. The problem goes like this: I have the main window that listens for
events from devices and COM components. The main window spawns childs (since
there are many to listen to) and starts listening. Whenever the main thread
receives events from these thread, the UI has to be updated. Now I want the
UI updation to happen in separate thread rather than in the main thread. So
I thought of having another thread just to paint and update the UI as I do
not want to overload the main thread as it has to listen for events from
other child thread.
I used to work with CWinThread in vc and everything seems to be possible for
me using PostThreadMessage. Here the equivalent of CWinThread is missing.(or
am I missing something ?)
I am new to C# and till now my exp are with vc. I have to try it out with
ThreadPool.
"John Murray" <jt*@io.com> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
Perhaps I am missing something, but why would you have a thread spun up
that has nothing to do. If all the child thread is doing is waiting for
an event from the parent, why not have the parent use a thread from the
thread pool when a new event occurs that it cares about. Another option
would be to use an AsynchDelegate ... which may give you some other
benefits as far as error handling ..etc..

John

Ram wrote:
Hi,
I have two threads, one parent thread containing UI and a child thread waiting for some events from the parent thread. Now, I have two issues: 1) to keep the child thread active till the end of parent thread. 2) Fire event from the parent thread to child thread.

To keep the child thread active, i tried different means including
while(1){ application.DoEvents()} but seems that it is not working. I am
unable to make it work.

For the event, I am able to fire events from the child to parent using
delegates, But how to fire it from the parent to child?

Thanx
Ram

Nov 16 '05 #4
You may want to consider reversing your logic here. Primarily, this is
because you cannot (well, you can, but results are usually not good)
update your UI on any thread other than the one that it was created on.
As a result, your paints have to happen in your UI thread -- although if
the information that you are updating is from an intensive operation,
you might benefit from having using the threadpool to pre-process the
updates.

John
Ram wrote:
Ok. The problem goes like this: I have the main window that listens for
events from devices and COM components. The main window spawns childs (since
there are many to listen to) and starts listening. Whenever the main thread
receives events from these thread, the UI has to be updated. Now I want the
UI updation to happen in separate thread rather than in the main thread. So
I thought of having another thread just to paint and update the UI as I do
not want to overload the main thread as it has to listen for events from
other child thread.
I used to work with CWinThread in vc and everything seems to be possible for
me using PostThreadMessage. Here the equivalent of CWinThread is missing.(or
am I missing something ?)
I am new to C# and till now my exp are with vc. I have to try it out with
ThreadPool.
"John Murray" <jt*@io.com> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
Perhaps I am missing something, but why would you have a thread spun up
that has nothing to do. If all the child thread is doing is waiting for
an event from the parent, why not have the parent use a thread from the
thread pool when a new event occurs that it cares about. Another option
would be to use an AsynchDelegate ... which may give you some other
benefits as far as error handling ..etc..

John

Ram wrote:
Hi,
I have two threads, one parent thread containing UI and a child
thread
waiting for some events from the parent thread. Now, I have two issues:
1)
to keep the child thread active till the end of parent thread. 2) Fire
event
from the parent thread to child thread.

To keep the child thread active, i tried different means including
while(1){ application.DoEvents()} but seems that it is not working. I am
unable to make it work.

For the event, I am able to fire events from the child to parent using
delegates, But how to fire it from the parent to child?

Thanx
Ram


Nov 16 '05 #5
I agree with your last few comments. The windows message loop is abstracted
out to a large extent in the FCL. I have not found any really good
documentation of the implementation. I tell my students to never perform any
UI based operations in a worker thread. I even recommend performing an
Invalidate by the UI thread via an Invoke rather than doing it in the worker
thread. The latter seems to work OK but is technically not recommended. Any
computationally intensive operations can be performed by the worker thread,
but the actual painting by the UI thread.

Thomas P. Skinner [MVP]

"Ram" <ra*********@hotmail.com> wrote in message
news:e3*************@TK2MSFTNGP10.phx.gbl...
Ok. The problem goes like this: I have the main window that listens for
events from devices and COM components. The main window spawns childs
(since
there are many to listen to) and starts listening. Whenever the main
thread
receives events from these thread, the UI has to be updated. Now I want
the
UI updation to happen in separate thread rather than in the main thread.
So
I thought of having another thread just to paint and update the UI as I do
not want to overload the main thread as it has to listen for events from
other child thread.
I used to work with CWinThread in vc and everything seems to be possible
for
me using PostThreadMessage. Here the equivalent of CWinThread is
missing.(or
am I missing something ?)
I am new to C# and till now my exp are with vc. I have to try it out with
ThreadPool.
"John Murray" <jt*@io.com> wrote in message
news:et**************@TK2MSFTNGP15.phx.gbl...
Perhaps I am missing something, but why would you have a thread spun up
that has nothing to do. If all the child thread is doing is waiting for
an event from the parent, why not have the parent use a thread from the
thread pool when a new event occurs that it cares about. Another option
would be to use an AsynchDelegate ... which may give you some other
benefits as far as error handling ..etc..

John

Ram wrote:
> Hi,
> I have two threads, one parent thread containing UI and a child thread > waiting for some events from the parent thread. Now, I have two issues: 1) > to keep the child thread active till the end of parent thread. 2) Fire event > from the parent thread to child thread.
>
> To keep the child thread active, i tried different means including
> while(1){ application.DoEvents()} but seems that it is not working. I
> am
> unable to make it work.
>
> For the event, I am able to fire events from the child to parent using
> delegates, But how to fire it from the parent to child?
>
> Thanx
> Ram
>
>


Nov 16 '05 #6

"Ram" <ra*********@hotmail.com> wrote in message
news:e3*************@TK2MSFTNGP10.phx.gbl...

I have the main window that listens for
events from devices and COM components. The main window spawns childs
(since
there are many to listen to) and starts listening.


Sorry, but I'm not clear on what you mean, here are some of my questions:
Who is actualy listening for the events from COM and other devices, the UI
thread, the spawned threads or both?
What thread(s) did you create your COM object on?
What threadingmodel do these COM objects use?
Did you set the appartment of the spawned thread accordingly?

Willy.

Nov 16 '05 #7
Ram
Thanx for the responses. As Thomas & John suggested, I could see a point in
that and have modified the UI logic. Coming to Willy questions:
UI thread waits for events from COM components. These are third party
components. I have not set the appartment model for the spawned thread.

Ram

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:em**************@TK2MSFTNGP15.phx.gbl...

"Ram" <ra*********@hotmail.com> wrote in message
news:e3*************@TK2MSFTNGP10.phx.gbl...

I have the main window that listens for
events from devices and COM components. The main window spawns childs
(since
there are many to listen to) and starts listening.


Sorry, but I'm not clear on what you mean, here are some of my questions:
Who is actualy listening for the events from COM and other devices, the UI
thread, the spawned threads or both?
What thread(s) did you create your COM object on?
What threadingmodel do these COM objects use?
Did you set the appartment of the spawned thread accordingly?

Willy.

Nov 16 '05 #8

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

Similar topics

3
2181
by: David Sworder | last post by:
This message was already cross-posted to C# and ADO.NET, but I forgot to post to this "general" group... sorry about that. It just occured to me after my first post that the "general" group readers...
31
2434
by: AlexeiOst | last post by:
Everywhere in documentation there are recommendations to use threads from thread pooling for relatively short tasks. As I understand, fetching a page or multiple pages (sometimes up to 50 but not...
2
2334
by: cottonviking | last post by:
Greetings, all! I've been pondering the pitfalls of aborting a secondary thread in a service app I'm writing (VB, fx v1.1). Everything I've read so far pretty much dissuades one from aborting one...
23
6480
by: Jeff Rodriguez | last post by:
Here's what I want do: Have a main daemon which starts up several threads in a Boss-Queue structure. From those threads, I want them all to sit and watch a queue. Once an entry goes into the...
4
1093
by: Geoff Jones | last post by:
Hi Have a form (form1) that starts a thread which has another form within it (form2). How can I send a message to this thread? For example, how can I send some text from Form1 to an TextBox in...
5
3010
by: temp2 | last post by:
Hello, I have an app that reads data params from a stream and updates controls accordingly. The stream reader is on a different thread than the main thread that created the controls. I fully...
6
5101
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be...
13
3565
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
0
7213
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
7098
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
7298
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
5610
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,...
1
5026
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
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1526
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
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
406
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.