Threading trouble

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

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
 
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
 
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
 
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);
}

---------------------------------------------------------------
 
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.
 
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.
 
Back
Top