PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Compact Framework Threading trouble

Reply

Threading trouble

 
Thread Tools Rate Thread
Old 17-03-2005, 03:21 PM   #1
Chris
Guest
 
Posts: n/a
Default Threading trouble


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
Old 17-03-2005, 04:02 PM   #2
Chris Tacke, eMVP
Guest
 
Posts: n/a
Default Re: Threading trouble

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" <foo@bar.com> wrote in message
news:eeTC2XwKFHA.3380@TK2MSFTNGP10.phx.gbl...
> 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
Old 17-03-2005, 04:57 PM   #3
Chris
Guest
 
Posts: n/a
Default Re: Threading trouble

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" <ctacke@spamfree-opennetcf.org> schrieb im Newsbeitrag
news:uZVU6qwKFHA.436@TK2MSFTNGP09.phx.gbl...
> 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" <foo@bar.com> wrote in message
> news:eeTC2XwKFHA.3380@TK2MSFTNGP10.phx.gbl...
>> 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
Old 17-03-2005, 06:14 PM   #4
Chris
Guest
 
Posts: n/a
Default Re: Threading trouble

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
Old 17-03-2005, 06:43 PM   #5
=?Utf-8?B?UmV6YQ==?=
Guest
 
Posts: n/a
Default Re: Threading trouble

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
Old 17-03-2005, 06:57 PM   #6
Chris
Guest
 
Posts: n/a
Default Re: Threading trouble

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off