Q: about BeginInvoke

N

Norbert

Hi

I have an App with two threads, one for the GUI and one for network
communications.

No I have code like that:
private void Server_ServerStoppedMeasurement(object sender, int
timestamp, double drivabilityResult)
{
// workaround because InvokeRequired isn't supportet by the
..net CF
if (this != sender)
{
this.BeginInvoke(new
MesStoppedByServerNotify(Server_ServerStoppedMeasurementHandler),
this,timestamp, drivabilityResult);
}
}

private void Server_ServerStoppedMeasurementHandler(object
sender, int timestamp, double Result)
{
Form_Result form_result = new Form_Result();
form_result.DriveScore = Result;
form_result.Duration = timestamp;
form_result.ShowDialog();

form_result.Close();

this.button_startstop.MainText =
Properties.Resources.StartStop_Start;
}

This handler gets called, whenever the UDP Server gets a
ServerStoppedMeasurement message.

So far so good, my problem is, that sometimes i get 2 ServerStopp
messages
-> so it happens that the result screen is shown 2 times!!!!

How does BeginInvoke work? I thougt this is more like a PostMessage in
the old MFC Times? why can 2 methodes be called in only one thread?

Norbert
 
R

Rayogen

Try using Control.Invoke.

Your main UI should attach to the thread event (or have a method that
the tread calls) then call this.Invoke (assuming your UI is a typeof
Control).

Your code should look something like this

private void ThreadA()
{
while(true)
{
FunctionA();
}
}

private void FunctionA()
{
this.Invoke(new EventHandler(UpdateUIFromThread);
}

private void UpdateUIFromThread(object sender, EventArgs e)
{
//Do you UI updates here.
}


If you are still getting two messages, then I would suspect that your
thread is calling the Invoke more then once.

I hope this helps

Norman Rericha
 
N

Norbert

I dont want to use INVOKE because I want the background Thread to
continue, because I'm waiting for a userinteraction.
And the Invoke blocks till i geht out of the Begininvoke method!

On the other side: I know that my background thread Invokes the GUI
method several times! But through the Invoke method the method should
be run in the context of the GUIThread, how can it happen that this
method is running twice at one time????


regards
Nobs
 
D

Daniel Moth

Only one thing can happen at the UI thread at any one time (or any thread
for that matter). You seeing two messages doesn't mean they are happening
concurrently (rather in quick succession).

Even if you use BeginInvoke I don't think you'll get what you think you
should get. In any case, you can simulate BeginInvoke by using the
ThreadPool and Invoke:
http://www.danielmoth.com/Blog/2004/10/invoke-cf-and-full-fx.html

Cheers
Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top