Help! WPF is giving me a hard time !

8 views
Skip to first unread message

Kevin

unread,
Feb 12, 2009, 11:01:03 AM2/12/09
to
I'm running into all kinds of problems, and the error messages thrown at me
are not any more descriptive than: Invalid Operation Exception.

It drives me crazy!

It got something to do with interacting with the GUI, from another thread.

I know, the Dispatcher handles this kind of stuff, so in this routine, i
check if i have access by calling Dispatcher.CheckAccess().
If this is true, then i just update the UIElement, like TextBox.Width=50;
If this is false, i do this in a delegate method, by calling
Dispatcher.BeginInvoke(...)

I can see in the debugger, that the delegate method is invoked in the Main
Thread (green one in VS2008) but STILL it throws me an invalid operation
exception...

What is this all about !! ??

And can someone explain to me, why there is an CurrentDispatcher property in
the Dispatcher Class ? Becuse this just creates a new Dispatcher object for
the current thread, and the result from CheckAccess() on that Dispatcher
object is always true, even if it is not the GUI thread... so why on
earth....?

I probably miss the big picture here, but i just want my code to run now !

Kevin

unread,
Feb 12, 2009, 11:00:08 AM2/12/09
to

Jan Kučera

unread,
Feb 12, 2009, 11:25:37 AM2/12/09
to
Hello Kevin,
it would help if you provided us your ... in BeginInvoke parameters. Because
if you are invoking a parameterless delegate, than there is a bug in the
framework: you have to pass "new object[0]" to the args instead of null.

Well I guess just for the reasons you described, to get a dispatcher for
current thread. If you only want to check whether your thread has already a
dispatcher assigned without creating a new one, just call
Dispatcher.FromThread(Thread.CurrentThread) and check for null.


Jan

"Kevin" <Ke...@discussions.microsoft.com> wrote in message
news:502E3825-96B4-4A20...@microsoft.com...

Jan Kučera

unread,
Feb 12, 2009, 11:33:46 AM2/12/09
to
Yeah and the trick is you have to check access to the text box's dispatcher,
not yours.
So it should be rather TextBox.CheckAccess()

Jan

"Kevin" <Ke...@discussions.microsoft.com> wrote in message
news:502E3825-96B4-4A20...@microsoft.com...

Kevin

unread,
Feb 12, 2009, 11:57:01 AM2/12/09
to
Jan thanks !

How could i have missed that!

I found out that i guess i had different UI elements created in different
Dispatchers (so it seemed).

The UI element in question i now created using a
Application.Current.Dispatcher.BeginInvoke (
(ThreadStart)delegate() { ... }); and it works !

I probably have to do this everywhere i create an uielement.... ?

James

unread,
Feb 12, 2009, 4:32:06 PM2/12/09
to
You have two options; check each element that you are trying to update or
update all elements using a dispatchtimer (see TemperatureSensor sample).

There is also an article the team blog that goes into this in more details:
http://blogs.msdn.com/netmfteam/archive/2008/03/04/using-the-dispatcher.aspx

James

Jan Kučera

unread,
Feb 12, 2009, 4:50:13 PM2/12/09
to
How does it happen to you that you have elements on different dispatchers?
This does not sound very good...

Jan

"Kevin" <Ke...@discussions.microsoft.com> wrote in message

news:B7F921FD-D6CD-465A...@microsoft.com...

Kevin

unread,
Feb 13, 2009, 2:39:02 AM2/13/09
to
Most of the UIElements are build when the user-interface is instationated,
these run in the Application Dispatcher i guess.

However, there are some UI elements i want to dynamically generate and
display, during the runtime of the application, which can be in any other
process or worker thread.
I never payed attention to these single UI-thread restrictions, so i just
create a new UI element there (for instance a Canvas with text on it), and
then try to set that as the child of a UIElement already displayed... this
was not working, because the UI element that i created last had a different
dipatcher i guess?

So now i need to go through my code, to see what is the best approach to
dynamically create these UI elements...

Steve Maillet [MSFT]

unread,
Feb 13, 2009, 12:37:51 PM2/13/09
to
> However, there are some UI elements i want to dynamically generate and
> display, during the runtime of the application, which can be in any other
> process or worker thread.
> I never payed attention to these single UI-thread restrictions, so i just
> create a new UI element there (for instance a Canvas with text on it), and
> then try to set that as the child of a UIElement already displayed... this
> was not working, because the UI element that i created last had a
> different
> dipatcher i guess?
Yep, that won't work. The UIElement is bound to the current dispatcher of
the thread it was created on at the time of creation. So All the UIElements
must be created on the UI thread. This is normally done by using Invoke() on
the dispatcher for the UI thread to schedule an arbitrary delegate to be
called from the UIThread.


--
Steve Maillet
Program Manager - .NET Micro Framework
http://blogs.msdn.com/smaillet

TimL

unread,
May 20, 2010, 7:34:29 PM5/20/10
to
I am also having a hard time using Invoke to return information to the main ui thread. Can one help please?

I have serial data (Telemetry) being received in a thread that I want to return to the main ui class that initiated the serial thread.

I have created a delegate in my main thread and also a function with the same signature. I can't work out how to use beginInvoke or Invoke.

In the example below I would like the class created by the new thread in TelemetryProviderThreadProc to return Telemetry info (a class) to the OnTelemetryCallback method.

Thanks if you can help


private Thread TelemetryProviderThread;
TelemetryProvider telemetryProvider;

public delegate void TelemetryCallBackDelegate(Telemetry tel);

public void OnTelemetryCallback(Telemetry tel)
{
telemetry = tel;
}

public void StartTelemetry()
{
TelemetryProviderThread = new Thread(new ThreadStart(TelemetryProviderThreadProc));
TelemetryProviderThread.Priority = ThreadPriority.Lowest;
TelemetryProviderThread.Start();
}

void TelemetryProviderThreadProc()
{

telemetryProvider = new TelemetryProviders.TelemetrySerialProvider("COM2", (int)57600);
}

Steve Maillet [MSFT] wrote:

Yep, that won't work.

13-Feb-09

Yep, that won't work. The UIElement is bound to the current dispatcher of
the thread it was created on at the time of creation. So All the UIElements
must be created on the UI thread. This is normally done by using Invoke() on
the dispatcher for the UI thread to schedule an arbitrary delegate to be
called from the UIThread.


--
Steve Maillet
Program Manager - .NET Micro Framework
http://blogs.msdn.com/smaillet

Previous Posts In This Thread:

On Thursday, February 12, 2009 11:01 AM
Kevi wrote:

Help! WPF is giving me a hard time !


I'm running into all kinds of problems, and the error messages thrown at me
are not any more descriptive than: Invalid Operation Exception.

It drives me crazy!

It got something to do with interacting with the GUI, from another thread.

I know, the Dispatcher handles this kind of stuff, so in this routine, i
check if i have access by calling Dispatcher.CheckAccess().
If this is true, then i just update the UIElement, like TextBox.Width=50;
If this is false, i do this in a delegate method, by calling
Dispatcher.BeginInvoke(...)

I can see in the debugger, that the delegate method is invoked in the Main
Thread (green one in VS2008) but STILL it throws me an invalid operation
exception...

What is this all about !! ??

And can someone explain to me, why there is an CurrentDispatcher property in
the Dispatcher Class ? Becuse this just creates a new Dispatcher object for
the current thread, and the result from CheckAccess() on that Dispatcher
object is always true, even if it is not the GUI thread... so why on
earth....?

I probably miss the big picture here, but i just want my code to run now !

On Thursday, February 12, 2009 11:25 AM
kucer wrote:

Hello Kevin,it would help if you provided us your ...


Hello Kevin,
it would help if you provided us your ... in BeginInvoke parameters. Because
if you are invoking a parameterless delegate, than there is a bug in the
framework: you have to pass "new object[0]" to the args instead of null.

Well I guess just for the reasons you described, to get a dispatcher for
current thread. If you only want to check whether your thread has already a
dispatcher assigned without creating a new one, just call
Dispatcher.FromThread(Thread.CurrentThread) and check for null.


Jan

"Kevin" <Ke...@discussions.microsoft.com> wrote in message
news:502E3825-96B4-4A20...@microsoft.com...

On Thursday, February 12, 2009 11:33 AM
kucer wrote:

Yeah and the trick is you have to check access to the text box's dispatcher,
Yeah and the trick is you have to check access to the text box's dispatcher,
not yours.
So it should be rather TextBox.CheckAccess()

Jan

On Thursday, February 12, 2009 11:57 AM
Kevi wrote:

Jan thanks !How could i have missed that!
Jan thanks !

How could i have missed that!

I found out that i guess i had different UI elements created in different
Dispatchers (so it seemed).

The UI element in question i now created using a
Application.Current.Dispatcher.BeginInvoke (
(ThreadStart)delegate() { ... }); and it works !

I probably have to do this everywhere i create an uielement.... ?

"Jan Ku??era" wrote:

On Thursday, February 12, 2009 4:32 PM
Jame wrote:

You have two options; check each element that you are trying to update or
You have two options; check each element that you are trying to update or
update all elements using a dispatchtimer (see TemperatureSensor sample).

There is also an article the team blog that goes into this in more details:
http://blogs.msdn.com/netmfteam/archive/2008/03/04/using-the-dispatcher.aspx

James

"Kevin" wrote:

On Thursday, February 12, 2009 4:50 PM
kucer wrote:

How does it happen to you that you have elements on different dispatchers?
How does it happen to you that you have elements on different dispatchers?
This does not sound very good...

Jan

On Friday, February 13, 2009 2:39 AM
Kevi wrote:

Most of the UIElements are build when the user-interface is
Most of the UIElements are build when the user-interface is instationated,
these run in the Application Dispatcher i guess.

However, there are some UI elements i want to dynamically generate and

display, during the runtime of the application, which can be in any other
process or worker thread.
I never payed attention to these single UI-thread restrictions, so i just
create a new UI element there (for instance a Canvas with text on it), and
then try to set that as the child of a UIElement already displayed... this
was not working, because the UI element that i created last had a different
dipatcher i guess?

So now i need to go through my code, to see what is the best approach to

dynamically create these UI elements...

"Jan Ku??era" wrote:

On Friday, February 13, 2009 12:37 PM
Steve Maillet [MSFT] wrote:

Yep, that won't work.
Yep, that won't work. The UIElement is bound to the current dispatcher of
the thread it was created on at the time of creation. So All the UIElements
must be created on the UI thread. This is normally done by using Invoke() on
the dispatcher for the UI thread to schedule an arbitrary delegate to be
called from the UIThread.


--
Steve Maillet
Program Manager - .NET Micro Framework
http://blogs.msdn.com/smaillet


Submitted via EggHeadCafe - Software Developer Portal of Choice
Crypto Obfuscator for .NET - Product Review
http://www.eggheadcafe.com/tutorials/aspnet/bf15c41b-6510-403e-9af8-f5fd987fafb1/crypto-obfuscator-for-ne.aspx

TimL

unread,
May 20, 2010, 7:36:02 PM5/20/10
to
I am also having a hard time using Invoke to return information to the main ui thread. Can one help please?

I have serial data (Telemetry) being received in a thread that I want to return to the main ui class that initiated the serial thread.

I have created a delegate in my main thread and also a function with the same signature. I can't work out how to use beginInvoke or Invoke.

In the example below I would like the class created by the new thread in TelemetryProviderThreadProc to return Telemetry info (a class) to the OnTelemetryCallback method.

Thanks if you can help


private Thread TelemetryProviderThread;
TelemetryProvider telemetryProvider;

public delegate void TelemetryCallBackDelegate(Telemetry tel);

public void OnTelemetryCallback(Telemetry tel)
{
telemetry = tel;
}

public void StartTelemetry()
{
TelemetryProviderThread = new Thread(new ThreadStart(TelemetryProviderThreadProc));
TelemetryProviderThread.Priority = ThreadPriority.Lowest;
TelemetryProviderThread.Start();
}

void TelemetryProviderThreadProc()
{

telemetryProvider = new TelemetryProviders.TelemetrySerialProvider("COM2", (int)57600);
}

Steve Maillet [MSFT] wrote:

Yep, that won't work.

13-Feb-09

Yep, that won't work. The UIElement is bound to the current dispatcher of
the thread it was created on at the time of creation. So All the UIElements
must be created on the UI thread. This is normally done by using Invoke() on
the dispatcher for the UI thread to schedule an arbitrary delegate to be
called from the UIThread.


--
Steve Maillet
Program Manager - .NET Micro Framework
http://blogs.msdn.com/smaillet

Previous Posts In This Thread:

It drives me crazy!


Jan

Jan

"Jan Ku??era" wrote:

James

"Kevin" wrote:

Jan

However, there are some UI elements i want to dynamically generate and

display, during the runtime of the application, which can be in any other
process or worker thread.
I never payed attention to these single UI-thread restrictions, so i just
create a new UI element there (for instance a Canvas with text on it), and
then try to set that as the child of a UIElement already displayed... this
was not working, because the UI element that i created last had a different
dipatcher i guess?

So now i need to go through my code, to see what is the best approach to

dynamically create these UI elements...

"Jan Ku??era" wrote:

On Friday, February 13, 2009 12:37 PM
Steve Maillet [MSFT] wrote:

Yep, that won't work.
Yep, that won't work. The UIElement is bound to the current dispatcher of
the thread it was created on at the time of creation. So All the UIElements
must be created on the UI thread. This is normally done by using Invoke() on
the dispatcher for the UI thread to schedule an arbitrary delegate to be
called from the UIThread.


--
Steve Maillet
Program Manager - .NET Micro Framework
http://blogs.msdn.com/smaillet

On Thursday, May 20, 2010 7:34 PM
TimL wrote:

NET MF Threading


I am also having a hard time using Invoke to return information to the main ui thread. Can one help please?

I have serial data (Telemetry) being received in a thread that I want to return to the main ui class that initiated the serial thread.

I have created a delegate in my main thread and also a function with the same signature. I can't work out how to use beginInvoke or Invoke.

In the example below I would like the class created by the new thread in TelemetryProviderThreadProc to return Telemetry info (a class) to the OnTelemetryCallback method.

Thanks if you can help


private Thread TelemetryProviderThread;
TelemetryProvider telemetryProvider;

public delegate void TelemetryCallBackDelegate(Telemetry tel);

public void OnTelemetryCallback(Telemetry tel)
{
telemetry = tel;
}

public void StartTelemetry()
{
TelemetryProviderThread = new Thread(new ThreadStart(TelemetryProviderThreadProc));
TelemetryProviderThread.Priority = ThreadPriority.Lowest;
TelemetryProviderThread.Start();
}

void TelemetryProviderThreadProc()
{

telemetryProvider = new TelemetryProviders.TelemetrySerialProvider("COM2", (int)57600);
}

Submitted via EggHeadCafe - Software Developer Portal of Choice

LINQ With Strings
http://www.eggheadcafe.com/tutorials/aspnet/03afdcf3-7b60-47db-adb9-db7fe2c6ab8c/linq-with-strings.aspx

Jan Kučera

unread,
May 21, 2010, 8:54:29 AM5/21/10
to
Hi TimL,

could you please give also some of your UI code so that we know what are you
trying to get and where?

Jan


"TimL" wrote in message news:201052019...@hotmail.co.uk...

TimL

unread,
May 21, 2010, 6:33:01 PM5/21/10
to
Thanks Jan,

Well from your response I am guessing I have got things wrong :)

I might have a few windows that need to share some telemetry information. So
I have defined a static variable at application level to provide telemetry
information to whichever UI window is active.

public static Telemetry telemetry;

As a test I was hoping that my serial reader class/thread could populate a
new Telemetry class and send it to the application.

So I could leave the code I posted below in the app or move it to the main
window.

In the window I expect to setup a timer to refresh the UI from the Telemetry
variable.

At this stage I am trying to ensure that I find the most efficient way to
send back the Telemetry stats to the main app before I code the rest of the
solution.

Sorry for the alck of info. What other info can I give you?

Tim


"Jan Kučera" wrote:

> .
>

Jan Kučera

unread,
May 21, 2010, 7:48:08 PM5/21/10
to
So if I got it right, you are continuously acquiring data from a serial port
and storing it in a static variable. You also have multiple windows, and
want to display the data in the currently active window.

If you want to update the data as they arrive from serial port, you would
probably need a dispatcher.invoke to push it to the window (kind of event
notification). Correct dispatcher is needed only if you want to touch the UI
controls.

If you want to update the window as it becomes active, you don't need the
dispatcher.invoke mechanism, as you are already in an UI thread when the
window becomes active, and you can read the data from the static variable
without problems.

Not sure if this is what needed to know, feel free to ask further. :)
Jan

"TimL" <Ti...@discussions.microsoft.com> wrote in message
news:2149D58B-D758-4530...@microsoft.com...

TimL

unread,
May 22, 2010, 10:29:01 AM5/22/10
to
Thanks for the good explaination. I am trying to be better at explaining
myself :)

The serial is in a serperate non blocking thread allowing debug of the UI.

Can I simply update the static application variable from the serial thread?

If a timer in a UI thread is reading the variable will this cause a problem
if the serial thread is updating the static member at the same time?

I thought I would have to push a new copy of the variable to the
applications queue from the serial thread using a dispatcher? If the UI is
busy I want the "push" to timeout and fail?

Am I making this too complicated?

Thanks

"Jan Kučera" wrote:

> .
>

TimL

unread,
May 22, 2010, 10:09:01 PM5/22/10
to
Updating a static program variable from a thread at the same time as reading
the variable from the UI seems to work reliably.

I was obviously worrying too much.

Thanks for your help.

By the way your "Rich Media Extensions" also work very well :)

Tim


"Jan Kučera" wrote:

> .
>

Reply all
Reply to author
Forward
0 new messages