PC Review


Reply
Thread Tools Rate Thread

Threading trouble

 
 
Chris
Guest
Posts: n/a
 
      17th Mar 2005
Hello there,

Can I invoke a method on a foreign thread from within a continually running
worker thread? The method called should be executed by the foreign thread
and in the foreign thread's context!
The method is NOT associated with a certain control or Form object, it is
simply a different thread running.
In principle, it would be sufficient to send a notification to a different
thread!!!!
Please don't tell me how to invoke methods with Control.Invoke, I already
know that.;-)
Anyway, I'm very thankful if someone knew-
regards
Chris


 
Reply With Quote
 
 
 
 
Chris Tacke, eMVP
Guest
Posts: n/a
 
      17th Mar 2005
So you want this?

You have 2 threads, A and B.

You want Thread B to be able to run a mthod in Thread A's context?

The only way to do that is to have Thread A listening for an event (not a
managed event, that's in the caller's context) or monitoring some global
flag that when set by Thread B tells it to call the desired method.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


"Chris" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello there,
>
> Can I invoke a method on a foreign thread from within a continually
> running worker thread? The method called should be executed by the foreign
> thread and in the foreign thread's context!
> The method is NOT associated with a certain control or Form object, it is
> simply a different thread running.
> In principle, it would be sufficient to send a notification to a different
> thread!!!!
> Please don't tell me how to invoke methods with Control.Invoke, I already
> know that.;-)
> Anyway, I'm very thankful if someone knew-
> regards
> Chris
>
>



 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      17th Mar 2005
Well Chris,
you got it exactly. But it sounds not very good, to use active wait (for
polling a flag)!!! (especially not in a resource constrained device). The
"not managed" NamedEvent-Objects in turn block my receiving thread, so it
could do no other work!!!! (although the thread would enter an efficient
wait state!)
Maybe I should resort to a little dirty trick -
what would happen if I used the Control.Invoke to call a delegate from the
gui thread to execute my method for me?!?!?
The gui thread in turn doesn't really need to update the form but can do
other work i intend for it. Am i right or is this wrong!?
As I think, thr only pity would be I had to keep a reference on a gui object
in my object when needing to Control.Invoke!?
regards
Chris


"Chris Tacke, eMVP" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> So you want this?
>
> You have 2 threads, A and B.
>
> You want Thread B to be able to run a mthod in Thread A's context?
>
> The only way to do that is to have Thread A listening for an event (not a
> managed event, that's in the caller's context) or monitoring some global
> flag that when set by Thread B tells it to call the desired method.
>
> --
> Chris Tacke
> Co-founder
> OpenNETCF.org
> Has OpenNETCF helped you? Consider donating to support us!
> http://www.opennetcf.org/donate
>
>
> "Chris" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hello there,
>>
>> Can I invoke a method on a foreign thread from within a continually
>> running worker thread? The method called should be executed by the
>> foreign thread and in the foreign thread's context!
>> The method is NOT associated with a certain control or Form object, it is
>> simply a different thread running.
>> In principle, it would be sufficient to send a notification to a
>> different thread!!!!
>> Please don't tell me how to invoke methods with Control.Invoke, I already
>> know that.;-)
>> Anyway, I'm very thankful if someone knew-
>> regards
>> Chris
>>
>>

>
>



 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      17th Mar 2005
Hello,

now I am back to state I got a solution to my problem that Invoking a method
from a running thread in a different thread (the GUI thread) context is not
possible.

All what is needed is a dummy Control-Object that is placed in whatever
class you want - as long as it is executed in the main thread and ist is
visible from the point where it is Control.Invoked. It is not an
academically cute solution; however,
software isn't good because of its beauty but because of its functionality!
:-)
---------------------------------------------------------------

class Form1
{
//blabla, GUI issues, widgets...
public void Main()
{
Application.Run(new Form1());
}
public Form1() //constructor
{

CAnyThing thing = new CAnyThing();
thing.ExecuteWorkThread();
}
}

class CAnyThing
{
public event EventHandler ExecuteInvokeDelegate; // not absolutely
needed

public void ExecuteWorkThread()
{
Thread t = new Thread(new Threadstart(WorkThreadMethod));
t.Start(); //asynchronous operation starts
}

public int MemberChanged;
public void WorkThreadMethod()
{
while (true)
{
WaitOnSomething(); //blocking wait
//do something "useful", i.e. modify MemberChanged
Thread.Sleep(500);
//discover the need to have the GUI thread update a member
SyncControl.Invoke(new EventHandler(GUI_Thread_Callback);
//go on with data mining
}
}
}

public void GUI_Thread_Callback //is being executed on the gui thread
{
observer.Notify(this.MemberChanged);
}

---------------------------------------------------------------



 
Reply With Quote
 
=?Utf-8?B?UmV6YQ==?=
Guest
Posts: n/a
 
      17th Mar 2005
Hi

Another option is posting a message to the thread. Your thread periodically
checks it's message queue to know what function it is supposed to call.

Regards.

"Chris" wrote:

> Hello,
>
> now I am back to state I got a solution to my problem that Invoking a method
> from a running thread in a different thread (the GUI thread) context is not
> possible.
>
> All what is needed is a dummy Control-Object that is placed in whatever
> class you want - as long as it is executed in the main thread and ist is
> visible from the point where it is Control.Invoked. It is not an
> academically cute solution; however,
> software isn't good because of its beauty but because of its functionality!
> :-)
> ---------------------------------------------------------------
>
> class Form1
> {
> //blabla, GUI issues, widgets...
> public void Main()
> {
> Application.Run(new Form1());
> }
> public Form1() //constructor
> {
>
> CAnyThing thing = new CAnyThing();
> thing.ExecuteWorkThread();
> }
> }
>
> class CAnyThing
> {
> public event EventHandler ExecuteInvokeDelegate; // not absolutely
> needed
>
> public void ExecuteWorkThread()
> {
> Thread t = new Thread(new Threadstart(WorkThreadMethod));
> t.Start(); //asynchronous operation starts
> }
>
> public int MemberChanged;
> public void WorkThreadMethod()
> {
> while (true)
> {
> WaitOnSomething(); //blocking wait
> //do something "useful", i.e. modify MemberChanged
> Thread.Sleep(500);
> //discover the need to have the GUI thread update a member
> SyncControl.Invoke(new EventHandler(GUI_Thread_Callback);
> //go on with data mining
> }
> }
> }
>
> public void GUI_Thread_Callback //is being executed on the gui thread
> {
> observer.Notify(this.MemberChanged);
> }
>
> ---------------------------------------------------------------
>
>
>
>

 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      17th Mar 2005
Thank you for your help!
Initially I wanted to get _arbitrary_ threads calling functions on the
others behalf, but that turned out to be impossible (with neither of the
threads blocking!). At least in CF.
When using the message loop, synchronization is also limited to the gui
thread which is the only one to use window messages. With my natural dislike
of Window messages, I resorted to the crippled-style-control-Solution.
But using Window Messages is also a feasible solution.
-btw does anyone know how the Control-Invoking is done internally in
operating system level ?
I think involvin controls could indeed have something to do with Windows
MEssages-
regards
Chris.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with wireless connection. Would appreciate suggestions on where to trouble shoot or where to look for problems. Ronald DIY PC 2 24th Sep 2007 12:54 PM
.NET Threading (& Windows threading) =?Utf-8?B?QW5kcmV3?= Microsoft Dot NET Framework 6 26th Aug 2005 09:01 PM
Threading trouble Chris Microsoft Dot NET Compact Framework 0 17th Mar 2005 04:28 PM
Trouble with Multi Threading jayderk Microsoft Dot NET Compact Framework 4 11th Nov 2004 05:18 PM
Trouble with Admin rights and thus trouble installing/updating programs =?Utf-8?B?aGtydWc2NA==?= Windows XP General 0 18th Jan 2004 07:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:26 AM.