concept UI thread in category .net

appears as: UI thread
IronPython in Action

This is an excerpt from Manning's book IronPython in Action.

  • Threading and asynchronous callbacks and dispatching onto the UI thread
  • Fortunately this is straightforward. WPF uses dispatchers to invoke delegates onto the UI thread, and Silverlight includes a cut-down version of this system. Silverlight provides a single dispatcher, which is accessible via the Dispatcher property on user interface components. From IronPython, functions that we pass into the Dispatcher.BeginInvoke method are invoked onto the main thread.

    Listing 13.15. Using a DispatcherTimer for timed events on the UI thread
    from System.Windows.Threading import DispatcherTimer from System import TimeSpan text = TextBlock() text.Text = "Nothing yet" text.FontSize = 24 root.Children.Add(text) counter = 0 def callback(sender, event): global counter counter += 1 text.Text = 'Tick %s' % counter timer = DispatcherTimer() timer.Tick += callback timer.Interval = TimeSpan.FromSeconds(2) timer.Start()

    When this code is executed, the Tick event fires every two seconds and increments the counter. Because Tick is executed on the UI thread, there is no need for us to explicitly dispatch the callback. If we didn’t need to interact with the user interface, then we could use the System.Threading.Timer class instead.