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 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
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
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
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
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 > >
"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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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?
|
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,...
|
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...
|
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,...
| |
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,...
|
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: 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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |