469,964 Members | 1,570 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

The calling thread must be STA...

Hi NG,

I am currently exploring VS2008 and WPF.
In a very little WPF-Aplication I am using a System.Timers.Timer and
assigned a Eventhandler to its "Elapsed" event.

When the eventhandler is called, I receive the following error message:
The calling thread must be STA, because many UI components require this.

I assum I need to synchronize the call with the GUI some how.
In Winform applications I used this.InvokeRequred to detect the neccessary
to do so and the used this.Invoke.

How would I do this with a WPF application now?

Thanks for hints and help.

Regards
Rainer Queck
Dec 5 '07 #1
4 11327
On Dec 5, 9:27 am, "Rainer Queck" <Rai...@noemail.noemailwrote:
I am currently exploring VS2008 and WPF.
In a very little WPF-Aplication I am using a System.Timers.Timer and
assigned a Eventhandler to its "Elapsed" event.

When the eventhandler is called, I receive the following error message:
The calling thread must be STA, because many UI components require this.

I assum I need to synchronize the call with the GUI some how.
In Winform applications I used this.InvokeRequred to detect the neccessary
to do so and the used this.Invoke.

How would I do this with a WPF application now?
See http://msdn.microsoft.com/msdnmag/is...g/default.aspx

Jon
Dec 5 '07 #2
In WPF you should use the Dispatcher Property to start any async
operations.
See the following example the variable _lbl is a label of my form.

public partial class Window1 : System.Windows.Window
{
private Timer _timer = new Timer(1000);

private delegate void DummyDelegate();

public Window1()
{
InitializeComponent();
_timer.Elapsed += delegate
{
this.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal ,
(DummyDelegate)
delegate { _lbl.Content = DateTime.Now.ToLongTimeString(); });
};
_timer.Start();
}

}

Dec 5 '07 #3
"Rainer Queck" <Ra****@noemail.noemailwrote in message
news:ed**************@TK2MSFTNGP04.phx.gbl...
Hi NG,

I am currently exploring VS2008 and WPF.
In a very little WPF-Aplication I am using a System.Timers.Timer and
assigned a Eventhandler to its "Elapsed" event.

When the eventhandler is called, I receive the following error message:
The calling thread must be STA, because many UI components require this.

I assum I need to synchronize the call with the GUI some how.
In Winform applications I used this.InvokeRequred to detect the neccessary
to do so and the used this.Invoke.

How would I do this with a WPF application now?

Thanks for hints and help.

Regards
Rainer Queck
Take a look at the Dispatcher.Invoke method in System.Windows.Threading.

Willy.

Dec 5 '07 #4
Hi Jon, Willy, Roman,

Thanks for your hints. I have my little app working now.

Regards
Rainer
Dec 5 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Daylor | last post: by
3 posts views Thread by Mike | last post: by
4 posts views Thread by Edwin Gomez | last post: by
1 post views Thread by | last post: by
7 posts views Thread by =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post: by
9 posts views Thread by Pubs | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.