How C # is multithreaded programming

Source: Internet
Author: User
Tags count thread thread stop tostring
Programming | multithreading

First Contact C # When a small example, first posted to this bar. Because multithreaded programming is very complex, this small example can only be a knowledge point of the entry line

First build an application project, named Threadexample, put a text box (TextBox1) on the form, a label (Lblresult), and then put two buttons, respectively, named Btnstart, Btnstop.

Form code:

Namespace Threadexample
... {
Partial class Threadexample
... {
/**////<summary>
Required designer variable.
</summary>
Private System.ComponentModel.IContainer components = null;

/**////<summary>
Clean up any being used.
</summary>
<param name= "disposing" >true if managed resources should is disposed; Otherwise, false.</param>
protected override void Dispose (bool disposing)
... {
if (disposing && (components!= null))
... {
Components. Dispose ();
}
Base. Dispose (disposing);
}

Windows Form Designer generated code#region Windows Form Designer generated code

/**////<summary>
Required to Designer support-do not modify
The contents is with the Code Editor.
</summary>
private void InitializeComponent ()
... {
This.btnstart = new System.Windows.Forms.Button ();
This.btnstop = new System.Windows.Forms.Button ();
This.button1 = new System.Windows.Forms.Button ();
This.textbox1 = new System.Windows.Forms.TextBox ();
This.lblresult = new System.Windows.Forms.Label ();
This. SuspendLayout ();
//
Btnstart
//
This.btnStart.Location = new System.Drawing.Point (14, 38);
This.btnStart.Name = "Btnstart";
This.btnStart.Size = new System.Drawing.Size (75, 23);
This.btnStart.TabIndex = 0;
This.btnStart.Text = "Start";
This.btnStart.Click + = new System.EventHandler (This.btnstart_click);
//
Btnstop
//
This.btnStop.Location = new System.Drawing.Point (14, 68);
This.btnStop.Name = "Btnstop";
This.btnStop.Size = new System.Drawing.Size (75, 23);
This.btnStop.TabIndex = 1;
This.btnStop.Text = "Stop";
This.btnStop.Click + = new System.EventHandler (This.btnstop_click);
//
Button1
//
This.button1.Location = new System.Drawing.Point (14, 97);
This.button1.Name = "Button1";
This.button1.Size = new System.Drawing.Size (75, 23);
This.button1.TabIndex = 3;
This.button1.Text = "Close";
This.button1.Click + = new System.EventHandler (This.button1_click);
//
TextBox1
//
This.textBox1.Location = new System.Drawing.Point (14, 11);
This.textBox1.Name = "TextBox1";
This.textBox1.Size = new System.Drawing.Size (75, 21);
This.textBox1.TabIndex = 4;
This.textBox1.Text = "200";
//
Lblresult
//
This.lblResult.AutoSize = true;
This.lblResult.Location = new System.Drawing.Point (12, 139);
This.lblResult.Name = "Lblresult";
This.lblResult.Size = new System.Drawing.Size (23, 12);
This.lblResult.TabIndex = 5;
This.lblResult.Text = "0/0";
//
Threadexample
//
This. Autoscaledimensions = new System.Drawing.SizeF (6F, 12F);
This. AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
This. ClientSize = new System.Drawing.Size (104, 164);
This. Controls.Add (This.lblresult);
This. Controls.Add (This.textbox1);
This. Controls.Add (This.button1);
This. Controls.Add (This.btnstop);
This. Controls.Add (This.btnstart);
This. Name = "Threadexample";
This. Text = "Form1";
This. ResumeLayout (FALSE);
This. PerformLayout ();

}

#endregion

Private System.Windows.Forms.Button btnstart;
Private System.Windows.Forms.Button btnstop;
Private System.Windows.Forms.Button button1;
Private System.Windows.Forms.TextBox TextBox1;
Private System.Windows.Forms.Label Lblresult;
}
}
Program code:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.Threading;

Namespace Threadexample
... {
public partial class Threadexample:form
... {
Declaring a thread
Private Thread TimerThread;
Declare a variable to store the label value
int count, i = 0;

Public Threadexample ()
... {
InitializeComponent ();
}

Add the value of the label to 1;
public void AddData ()
... {
Displays the value of the lable
if (i = = count)
i = 0;
This.lblResult.Text = i.tostring () + "/" + count. ToString ();
i++;
}

Update threads
public void Updatathread ()
... {
Try
... {
You can use this delegate when invoking a control's calling method, or when you need a simple delegate and do not want to define it yourself.
MethodInvoker mi = new MethodInvoker (this. AddData);
while (true)
... {
Executes the specified delegate asynchronously on the thread that created the control's underlying handle
This. BeginInvoke (MI);
Thread.Sleep (50);
}
}
catch (ThreadInterruptedException)
... {
Customizing exception throwing display for specific problems
}
Finally
... {
Do some processing
}
}

       //boot thread
         public void Startthread ()
        ... {
            stopthread ();
            timerthread = new Thread (New ThreadStart ( Updatathread));
           //Gets or sets a value that indicates whether a thread is a background thread.
            Timerthread.isbackground = true;
            Timerthread.start ();
       }

       /Stop thread
         public void Stopthread ()
        ... {
            if (timerthread!= null)
             ... {
               /thread Threads
                Timerthread.interrupt ();
                TimerThread = null ;
           }
       }

       //boot thread, display results
         private void btnStart_Click (object sender, EventArgs e)
         ... {
           //Calling thread startup function
             count = Int. Parse (TextBox1.Text);
            this. Startthread ();
       }

Stop thread
private void Btnstop_click (object sender, EventArgs e)
... {
Call Thread Stop function
This. Stopthread ();
}
}
}
After compiling, run, enter 200 in the text box, click the Start button, the label for dynamic growth, click Stop can pause program execution.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.