Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2007

Tip: Looking for answers? Try searching our database.

The calling thread must be STA...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rainer Queck - 05 Dec 2007 09:27 GMT
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
Jon Skeet [C# MVP] - 05 Dec 2007 10:05 GMT
> I am currently exploring VS2008 and WPF.
> In a very little WPF-Aplication I am using a System.Timers.Timer and
[quoted text clipped - 8 lines]
>
> How would I do this with a WPF application now?

See http://msdn.microsoft.com/msdnmag/issues/07/10/WPFThreading/default.aspx

Jon
Willy Denoyette [MVP] - 05 Dec 2007 10:13 GMT
> Hi NG,
>
[quoted text clipped - 15 lines]
> Regards
> Rainer Queck

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

Willy.
Roman Wagner - 05 Dec 2007 10:15 GMT
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();
        }

    }
Rainer Queck - 05 Dec 2007 11:00 GMT
Hi Jon, Willy, Roman,

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

Regards
Rainer

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.