Create a multi threaded web browsing application. : Thread « Windows Presentation Foundation « C# / CSharp Tutorial






<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MultiBrowse" Height="600" Width="800" Loaded="OnLoaded">
  <StackPanel Name="Stack" Orientation="Vertical">
    <StackPanel Orientation="Horizontal">
      <Button Content="New Window" Click="NewWindowHandler" />
      <TextBox Name="newLocation" Width="500" />
      <Button Content="GO!" Click="Browse" />
    </StackPanel>
    <Frame Name="placeHolder" Width="800" Height="550"></Frame>
  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Threading;
using System.Threading;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1() : base()
        {
            InitializeComponent();
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
           placeHolder.Source = new Uri("http://www.java2s.com");
        }

        private void Browse(object sender, RoutedEventArgs e)
        {
            placeHolder.Source = new Uri(newLocation.Text);
        }
        private void NewWindowHandler(object sender, RoutedEventArgs e)
        {       
            Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));
            newWindowThread.SetApartmentState(ApartmentState.STA);
            newWindowThread.IsBackground = true;
            newWindowThread.Start();
        }
        private void ThreadStartingPoint()
        {
            Window1 tempWindow = new Window1();
            tempWindow.Show();       
            System.Windows.Threading.Dispatcher.Run();
        }
    }
}
WPF Create A Multi Threaded Web Browsing Application








24.153.Thread
24.153.1.WPF ThreadingWPF Threading
24.153.2.Thread Sleep in Button Click handlerThread Sleep in Button Click handler
24.153.3.Check Whether You Are Running on the UI ThreadCheck Whether You Are Running on the UI Thread
24.153.4.Ensure That You Are Running on the UI ThreadEnsure That You Are Running on the UI Thread
24.153.5.Execute a Method Asynchronously Using the Dispatcher QueueExecute a Method Asynchronously Using the Dispatcher Queue
24.153.6.ThreadPool.QueueUserWorkItemThreadPool.QueueUserWorkItem
24.153.7.BlockThread.xamlBlockThread.xaml
24.153.8.Keep the UI from becoming non-responsive in single threaded application which performs a long operation.Keep the UI from becoming non-responsive in single threaded application which performs a long operation.
24.153.9.Create a multi threaded web browsing application.Create a multi threaded web browsing application.