Thread and UI Demo : Thread and UI « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » Thread and UIScreenshots 
Thread and UI Demo
 
using System;
using System.Threading;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public struct MyData {
    public double pi;
    public int iters;
}
public class Calc {
    private double _pi;
    private int _iters;

    private readonly int TotalIters;
    public Calc(int it) {
        _iters = 1;
        _pi = 0;
        TotalIters = it;
    }
    public MyData PI {
        get {
            MyData pi = new MyData();
            lock (this) {
                pi.pi = _pi;
                pi.iters = _iters;
            }
            return pi;
        }
    }
    public Thread MakeThread() {
        return new Thread(new ThreadStart(this.ThreadStarter));
    }
    private void calculate() {
        double series = 0;
        do {
            series ++;
            lock (this) {
                _iters += 4;
                _pi = series * 4;
            }
        while (_iters < TotalIters);
    }
    private void ThreadStarter() {
        try {
            calculate();
        catch (ThreadAbortException e) {
            Console.WriteLine("ThreadAbortException");
        }
    }
}
public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox PiValue;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox Iteratons;

    private Calc pi = new Calc(100000000);
    private Thread calcThread = null;
    private System.Windows.Forms.Timer timer1;
    private System.Windows.Forms.Button StopButton;
    private System.Windows.Forms.Button Pause;
    private System.ComponentModel.IContainer components;

    public Form1() {
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.Pause = new System.Windows.Forms.Button();
        this.PiValue = new System.Windows.Forms.TextBox();
        this.StopButton = new System.Windows.Forms.Button();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.Iteratons = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.Location = new System.Drawing.Point(824);
        this.label1.Name = "label1";
        this.label1.TabIndex = 0;
        this.label1.Text = "Value  of PI:";
        // 
        // label2
        // 
        this.label2.Location = new System.Drawing.Point(872);
        this.label2.Name = "label2";
        this.label2.TabIndex = 2;
        this.label2.Text = "Iterations:";
        // 
        // Pause
        // 
        this.Pause.Location = new System.Drawing.Point(24112);
        this.Pause.Name = "Pause";
        this.Pause.TabIndex = 5;
        this.Pause.Text = "Pause";
        this.Pause.Click += new System.EventHandler(this.Pause_Click);
        // 
        // PiValue
        // 
        this.PiValue.Location = new System.Drawing.Point(12824);
        this.PiValue.Name = "PiValue";
        this.PiValue.ReadOnly = true;
        this.PiValue.Size = new System.Drawing.Size(13620);
        this.PiValue.TabIndex = 1;
        this.PiValue.Text = "";
        // 
        // StopButton
        // 
        this.StopButton.Location = new System.Drawing.Point(200112);
        this.StopButton.Name = "StopButton";
        this.StopButton.TabIndex = 4;
        this.StopButton.Text = "Stop";
        this.StopButton.Click += new System.EventHandler(this.StopButton_Click);
        // 
        this.timer1.Enabled = true;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        // 
        this.Iteratons.Location = new System.Drawing.Point(12872);
        this.Iteratons.Name = "Iteratons";
        this.Iteratons.ReadOnly = true;
        this.Iteratons.TabIndex = 3;
        this.Iteratons.Text = "";
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(292149);
        this.Controls.AddRange(new System.Windows.Forms.Control[]  {
                                                                  this.Pause,
                                                                  this.StopButton,
                                                                  this.Iteratons,
                                                                  this.label2,
                                                                  this.PiValue,
                                                                  this.label1});
        this.Load += new System.EventHandler(this.Form1_Load);
        this.Closed += new System.EventHandler(this.Form1_Closed);
        this.ResumeLayout(false);

    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }

    private void Form1_Load(object sender, System.EventArgs e) {
        calcThread = pi.MakeThread();
        calcThread.Priority = ThreadPriority.Lowest;
        calcThread.Start();
    }
    private void timer1_Tick(object sender, System.EventArgs e) {
        if (this.Pause.Text == "Pause") {
            MyData p = pi.PI;
            this.PiValue.Text = p.pi.ToString();
            this.Iteratons.Text = p.iters.ToString();
        }
        if (calcThread.IsAlive == false) {
            StopButton.Enabled = false;
            Pause.Enabled = false;
            timer1.Enabled = false;
            calcThread = null;
        }
    }
    private void StopButton_Click(object sender, System.EventArgs e) {
        StopButton.Enabled = false;
        Pause.Enabled = false;
        timer1.Enabled = false;
        calcThread.Abort();
        calcThread.Join();
        calcThread = null;
    }
    private void Pause_Click(object sender, System.EventArgs e) {
        if (this.Pause.Text == "Pause") {
            calcThread.Suspend();
            this.Pause.Text = "Resume";
            this.StopButton.Enabled = false;
        else {
            calcThread.Resume();
            this.Pause.Text = "Pause";
            this.StopButton.Enabled = true;
        }
    }
    private void Form1_Closed(object sender, System.EventArgs e) {
        if (calcThread != null) {
            calcThread.Abort();
            calcThread.Join();
        }
    }
}

 
Related examples in the same category
1.Talking to a visual element in a background thread.
2.Background processing in a thread.
3.Starting and stopping a thread.
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.