Keep the UI from becoming non-responsive in single threaded application which performs a long operation. : Thread « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » Thread 
24.153.8.Keep the UI from becoming non-responsive in single threaded application which performs a long operation.
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Prime Numbers" Width="260" Height="75">
  <StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
    <Button Content="Start" Click="StartOrStop" Name="startStopButton" Margin="5,0,5,0"/>
    <TextBlock Margin="10,5,0,0">Number:</TextBlock>
    <TextBlock Name="numberTextBlock" Margin="4,5,0,0">3</TextBlock>
  </StackPanel>
</Window>
//File:Window.xaml.cs

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

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public delegate void NextPrimeDelegate();
        private long num = 1;   

        private bool continueCalculating = false;

        public Window1() : base()
        {
            InitializeComponent();
        }
        public void StartOrStop(object sender, EventArgs e)
        {
            if (continueCalculating)
            {
                continueCalculating = false;
                startStopButton.Content = "Resume";
            }
            else
            {
                continueCalculating = true;
                startStopButton.Content = "Stop";
                startStopButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,new NextPrimeDelegate(CheckNextNumber));
            }
        }
        public void CheckNextNumber()
        {
            numberTextBlock.Text = num.ToString();
            num += 2;
            if (continueCalculating)
            {
                startStopButton.Dispatcher.BeginInvoke(
                    System.Windows.Threading.DispatcherPriority.SystemIdle, 
                    new NextPrimeDelegate(this.CheckNextNumber));
            }
        }
    }
}
WPF Keep The U I From Becoming Nonresponsive In Single Threaded Application Which Performs A Long Operation
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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.