The calling thread must be STA...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rainer Queck

    #1

    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.T imer 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.InvokeRequ red 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]

    #2
    Re: The calling thread must be STA...

    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.T imer 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.InvokeRequ red 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

    Comment

    • Roman Wagner

      #3
      Re: The calling thread must be STA...

      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()
      {
      InitializeCompo nent();
      _timer.Elapsed += delegate
      {
      this.Dispatcher .Invoke(
      System.Windows. Threading.Dispa tcherPriority.N ormal,
      (DummyDelegate)
      delegate { _lbl.Content = DateTime.Now.To LongTimeString( ); });
      };
      _timer.Start();
      }

      }

      Comment

      • Willy Denoyette [MVP]

        #4
        Re: The calling thread must be STA...

        "Rainer Queck" <Rainer@noemail .noemailwrote in message
        news:edjHLEyNIH A.4712@TK2MSFTN GP04.phx.gbl...
        Hi NG,
        >
        I am currently exploring VS2008 and WPF.
        In a very little WPF-Aplication I am using a System.Timers.T imer 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.InvokeRequ red 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.Invo ke method in System.Windows. Threading.

        Willy.

        Comment

        • Rainer Queck

          #5
          Re: The calling thread must be STA...

          Hi Jon, Willy, Roman,

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

          Regards
          Rainer


          Comment

          Working...