이번 예제는 C#으로 Multi-Thread를 구현한 MS 예제 입니다 .참고 하세요
아래는 실행 화면 입니다.
------------------------------
frmMain.cs
------------------------------
//Copyright (C) 2002 Microsoft Corporation
//All rights reserved.
//THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
//EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
//MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//Requires the Trial or Release version of Visual Studio .NET Professional (or greater).
using System;
using System.Windows.Forms;
using System.Threading; // Namespace for Thread class
public class frmMain : System.Windows.Forms.Form
{
#region " Windows Form Designer generated code "
public frmMain()
{
//This call is required by the Windows Form Designer.
InitializeComponent();
//Add any initialization after the InitializeComponent() call
// So that we only need to set the title of the application once,
// we use the AssemblyInfo class (defined in the AssemblyInfo.vb file)
// to read the AssemblyTitle attribute.
AssemblyInfo ainfo = new AssemblyInfo();
this.Text = ainfo.Title;
this.mnuAbout.Text = string.Format("&About {0} ...", ainfo.Title);
}
//Form overrides dispose to clean up the component list.
protected override void Dispose(bool disposing)
{
if(disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
//Required by the Windows Form Designer
private System.ComponentModel.IContainer components = null;
//NOTE: The following procedure is required by the Windows Form Designer
//It can be modified using the Windows Form Designer.
//Do not modify it using the code editor.
private System.Windows.Forms.MainMenu mnuMain;
private System.Windows.Forms.MenuItem mnuFile;
private System.Windows.Forms.MenuItem mnuExit;
private System.Windows.Forms.MenuItem mnuHelp;
private System.Windows.Forms.MenuItem mnuAbout;
private System.Windows.Forms.GroupBox grpLongRunningTask;
private System.Windows.Forms.Button cmdSameThread;
private System.Windows.Forms.Button cmdWorkerPoolThread;
private System.Windows.Forms.Button cmdRunOnnewWin32Thread;
private System.Windows.Forms.Label lblThreadID;
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmMain));
this.mnuMain = new System.Windows.Forms.MainMenu();
this.mnuFile = new System.Windows.Forms.MenuItem();
this.mnuExit = new System.Windows.Forms.MenuItem();
this.mnuHelp = new System.Windows.Forms.MenuItem();
this.mnuAbout = new System.Windows.Forms.MenuItem();
this.grpLongRunningTask = new System.Windows.Forms.GroupBox();
this.cmdRunOnnewWin32Thread = new System.Windows.Forms.Button();
this.cmdWorkerPoolThread = new System.Windows.Forms.Button();
this.cmdSameThread = new System.Windows.Forms.Button();
this.lblThreadID = new System.Windows.Forms.Label();
this.grpLongRunningTask.SuspendLayout();
this.SuspendLayout();
//
//mnuMain
//
this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFile, this.mnuHelp});
//
//mnuFile
//
this.mnuFile.Index = 0;
this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.mnuExit});
this.mnuFile.Text = "&File";
//
//mnuExit
//
this.mnuExit.Index = 0;
this.mnuExit.Text = "E&xit";
this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click);
//
//mnuHelp
//
this.mnuHelp.Index = 1;
this.mnuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.mnuAbout});
this.mnuHelp.Text = "&Help";
//
//mnuAbout
//
this.mnuAbout.Index = 0;
this.mnuAbout.Text = "Text Comes from AssemblyInfo";
this.mnuAbout.Click += new System.EventHandler(this.mnuAbout_Click);
//
//grpLongRunningTask
//
this.grpLongRunningTask.Controls.AddRange(new System.Windows.Forms.Control[] {this.cmdRunOnnewWin32Thread, this.cmdWorkerPoolThread, this.cmdSameThread});
this.grpLongRunningTask.Location = new System.Drawing.Point(16, 56);
this.grpLongRunningTask.Name = "grpLongRunningTask";
this.grpLongRunningTask.Size = new System.Drawing.Size(232, 144);
this.grpLongRunningTask.TabIndex = 3;
this.grpLongRunningTask.TabStop = false;
this.grpLongRunningTask.Text = "Execute a long-running task";
//
//cmdRunOnnewWin32Thread
//
this.cmdRunOnnewWin32Thread.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.cmdRunOnnewWin32Thread.Location = new System.Drawing.Point(16, 96);
this.cmdRunOnnewWin32Thread.Name = "cmdRunOnnewWin32Thread";
this.cmdRunOnnewWin32Thread.Size = new System.Drawing.Size(200, 23);
this.cmdRunOnnewWin32Thread.TabIndex = 5;
this.cmdRunOnnewWin32Thread.Text = "Run on &new Win32 thread";
this.cmdRunOnnewWin32Thread.Click += new System.EventHandler(this.cmdRunOnNewWin32Thread_Click);
//
//cmdWorkerPoolThread
//
this.cmdWorkerPoolThread.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.cmdWorkerPoolThread.Location = new System.Drawing.Point(16, 64);
this.cmdWorkerPoolThread.Name = "cmdWorkerPoolThread";
this.cmdWorkerPoolThread.Size = new System.Drawing.Size(200, 23);
this.cmdWorkerPoolThread.TabIndex = 4;
this.cmdWorkerPoolThread.Text = "Run on &worker pool thread";
this.cmdWorkerPoolThread.Click += new System.EventHandler(this.cmdWorkerPoolThread_Click);
//
//cmdSameThread
//
this.cmdSameThread.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.cmdSameThread.Location = new System.Drawing.Point(16, 32);
this.cmdSameThread.Name = "cmdSameThread";
this.cmdSameThread.Size = new System.Drawing.Size(200, 23);
this.cmdSameThread.TabIndex = 3;
this.cmdSameThread.Text = "Run on & same thread";
this.cmdSameThread.Click += new System.EventHandler(this.cmdSameThread_Click);
//
//lblThreadID
//
this.lblThreadID.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.lblThreadID.Location = new System.Drawing.Point(16, 16);
this.lblThreadID.Name = "lblThreadID";
this.lblThreadID.Size = new System.Drawing.Size(240, 24);
this.lblThreadID.TabIndex = 4;
this.lblThreadID.Text = "This window is being serviced by thread: ";
//
//frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(266, 235);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.lblThreadID, this.grpLongRunningTask});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = (System.Drawing.Icon) resources.GetObject("$this.Icon");
this.MaximizeBox = false;
this.Menu = this.mnuMain;
this.Name = "frmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Title Comes from Assembly Info";
this.grpLongRunningTask.ResumeLayout(false);
this.ResumeLayout(false);
this.Load += new EventHandler(this.frmMain_Load);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
#region " Standard Menu Code "
// <system.diagnostics.debuggerstepthrough>has been added to some procedures since they are
// not the focus of the demo. Remove them if you wish to debug the procedures.
// This code simply shows the About form.</system.diagnostics.debuggerstepthrough>
private void mnuAbout_Click(object sender, System.EventArgs e)
{
// Open the About form in Dialog Mode
frmAbout frm = new frmAbout();
frm.ShowDialog(this);
frm.Dispose();
}
// This code will close the form.
private void mnuExit_Click(object sender, System.EventArgs e)
{
// Close the current form
this.Close();
}
#endregion
private void frmMain_Load(object sender, System.EventArgs e)
{
//Display the main thread for the application
lblThreadID.Text += AppDomain.GetCurrentThreadId().ToString();
}
private void cmdSameThread_Click(object sender, System.EventArgs e)
{
//Run the task on the same thread that is managing the frmMain window.
TheLongRunningTask();
}
//To run the task on a worker pool thread, you can use an asynchronous
//invocation on a delegate. For this example, we'll declare a delegate
//named TaskDelegate, and call it asynchronously. The signature of the
//delegate declaration must match the method (TheLongRunningTask) exactly.
delegate void TaskDelegate();
private void cmdWorkerPoolThread_Click(object sender, System.EventArgs e)
{
//To run the task an a thread from the worker pool, create an instance
//of a delegate whose signature matches the method that will be called,
//then call BeginInvoke on that delegate. For this example, the arguments
//and return value of BeginInvoke are unneeded. This technique is sometimes
//referred to as "Fire and Forget".
TaskDelegate td = new TaskDelegate(new ThreadStart(TheLongRunningTask));
td.BeginInvoke(null, null); // Runs on a worker thread from the pool
}
private void cmdRunOnNewWin32Thread_Click(object sender, System.EventArgs e)
{
//To run the task on a newly created OS thread (rather than a tread from the
//thread pool), create a new instance of a managed thread. The constructor
//takes the address of a subroutine with no arguments.
Thread t = new Thread(new ThreadStart(TheLongRunningTask)); // Creates the new thread
t.Start(); // Begins the execution of the new thread
}
private void TheLongRunningTask()
{
//This method simulates some long-running task. To represent the work in
//progress, a form with a progress bar will display during its execution.
//The method displays the form, then updates the progress bar every half
//second for 5 seconds. After simulating the task, the form is taken down.
frmTaskProgress f = new frmTaskProgress();
f.Show();
f.Refresh(); // Refresh causes an instant (non-posted) display of the label.
//Slowly increment the progress bar
int i;
for(i = 1; i < 10; i++)>
{
f.prgTaskProgress.Value += 10;
Thread.Sleep(500); // half-second delay
}
//Remove the form after the "task" finishes
f.Hide();
f.Dispose();
}
}
---------------------------
frmTaskProgress.cs
---------------------------
//Copyright (C) 2002 Microsoft Corporation
//All rights reserved.
//THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
//EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
//MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//Requires the Trial or Release version of Visual Studio .NET Professional (or greater).
using System;
public class frmTaskProgress : System.Windows.Forms.Form
{
#region " Windows Form Designer generated code "
public frmTaskProgress()
{
//This call is required by the Windows Form Designer.
InitializeComponent();
//Add any initialization after the InitializeComponent() call
}
//Form overrides dispose to clean up the component list.
protected override void Dispose(bool disposing)
{
if(disposing)
{
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
//Required by the Windows Form Designer
private System.ComponentModel.IContainer components;
//NOTE: The following procedure is required by the Windows Form Designer
//It can be modified using the Windows Form Designer.
//Do not modify it using the code editor.
public System.Windows.Forms.ProgressBar prgTaskProgress;
private System.Windows.Forms.Label lblLengthyTask;
private System.Windows.Forms.Label lblThreadID;
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmTaskProgress));
this.prgTaskProgress = new System.Windows.Forms.ProgressBar();
this.lblLengthyTask = new System.Windows.Forms.Label();
this.lblThreadID = new System.Windows.Forms.Label();
this.SuspendLayout();
//
//prgTaskProgress
//
this.prgTaskProgress.Location = new System.Drawing.Point(32, 40);
this.prgTaskProgress.Name = "prgTaskProgress";
this.prgTaskProgress.Size = new System.Drawing.Size(216, 24);
this.prgTaskProgress.TabIndex = 0;
//
//lblLengthyTask
//
this.lblLengthyTask.Font = new System.Drawing.Font("Microsoft Sans Serif", (float) 8.25, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, (byte) 0);
this.lblLengthyTask.Location = new System.Drawing.Point(56, 72);
this.lblLengthyTask.Name = "lblLengthyTask";
this.lblLengthyTask.Size = new System.Drawing.Size(184, 16);
this.lblLengthyTask.TabIndex = 1;
this.lblLengthyTask.Text = "Executing some lengthy task...";
//
//lblThreadID
//
this.lblThreadID.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.lblThreadID.Location = new System.Drawing.Point(32, 8);
this.lblThreadID.Name = "lblThreadID";
this.lblThreadID.Size = new System.Drawing.Size(272, 24);
this.lblThreadID.TabIndex = 5;
this.lblThreadID.Text = "This window is being serviced by thread: ";
//
//frmTaskProgress
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 101);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.lblThreadID, this.lblLengthyTask, this.prgTaskProgress});
this.Icon = (System.Drawing.Icon) resources.GetObject("$this.Icon");
this.Name = "frmTaskProgress";
this.Text = "Task Progress";
this.ResumeLayout(false);
this.Load += new System.EventHandler(this.frmTaskProgress_Load);
}
#endregion
private void frmTaskProgress_Load(object sender, System.EventArgs e)
{
//Display the ID of the thread that is loading this form.
this.lblThreadID.Text += System.AppDomain.GetCurrentThreadId().ToString();
}
}
--------------------
frmAbout.cs
--------------------
//Copyright (C) 2002 Microsoft Corporation
//All rights reserved.
//THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
//EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
//MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//Requires the Trial or Release version of Visual Studio .NET Professional (or greater).
using System;
using System.Windows.Forms;
using System.Reflection;
public class frmAbout: System.Windows.Forms.Form
{
#region " Windows Form Designer generated code ";
public frmAbout() {
//This call is required by the Windows Form Designer.
InitializeComponent();
//Add any initialization after the InitializeComponent() call
}
//Form overrides dispose to clean up the component list.
protected override void Dispose(bool disposing) {
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
//Required by the Windows Form Designer
private System.ComponentModel.IContainer components = null;
//NOTE: The following procedure is required by the Windows Form Designer
//It can be modified using the Windows Form Designer.
//Do not modify it using the code editor.
protected System.Windows.Forms.PictureBox pbIcon;
protected System.Windows.Forms.Label lblTitle;
protected System.Windows.Forms.Label lblVersion;
protected System.Windows.Forms.Label lblDescription;
protected System.Windows.Forms.Label lblCopyright;
protected System.Windows.Forms.Label lblCodebase;
protected System.Windows.Forms.Button btnOK;
private void InitializeComponent() {
this.pbIcon = new System.Windows.Forms.PictureBox();
this.lblTitle = new System.Windows.Forms.Label();
this.lblVersion = new System.Windows.Forms.Label();
this.lblDescription = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.lblCopyright = new System.Windows.Forms.Label();
this.lblCodebase = new System.Windows.Forms.Label();
this.SuspendLayout();
//
//pbIcon
//
this.pbIcon.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pbIcon.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.pbIcon.Location = new System.Drawing.Point(16, 16);
this.pbIcon.Name = "pbIcon";
this.pbIcon.Size = new System.Drawing.Size(32, 32);
this.pbIcon.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pbIcon.TabIndex = 0;
this.pbIcon.TabStop = false;
//
//lblTitle
//
this.lblTitle.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.lblTitle.Location = new System.Drawing.Point(72, 16);
this.lblTitle.Name = "lblTitle";
this.lblTitle.Size = new System.Drawing.Size(360, 23);
this.lblTitle.TabIndex = 1;
this.lblTitle.Text = "Application Title";
//
//lblVersion
//
this.lblVersion.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.lblVersion.Location = new System.Drawing.Point(72, 40);
this.lblVersion.Name = "lblVersion";
this.lblVersion.Size = new System.Drawing.Size(360, 23);
this.lblVersion.TabIndex = 2;
this.lblVersion.Text = "Application Version";
//
//lblDescription
//
this.lblDescription.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.lblDescription.Location = new System.Drawing.Point(72, 80);
this.lblDescription.Name = "lblDescription";
this.lblDescription.Size = new System.Drawing.Size(360, 46);
this.lblDescription.TabIndex = 3;
this.lblDescription.Text = "Application Description";
//
//btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.btnOK.Location = new System.Drawing.Point(352, 200);
this.btnOK.Name = "btnOK";
this.btnOK.TabIndex = 4;
this.btnOK.Text = "OK";
//
//lblCopyright
//
this.lblCopyright.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.lblCopyright.Location = new System.Drawing.Point(72, 56);
this.lblCopyright.Name = "lblCopyright";
this.lblCopyright.Size = new System.Drawing.Size(360, 23);
this.lblCopyright.TabIndex = 5;
this.lblCopyright.Text = "Application Copyright";
//
//lblCodebase
//
this.lblCodebase.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.lblCodebase.Location = new System.Drawing.Point(72, 128);
this.lblCodebase.Name = "lblCodebase";
this.lblCodebase.Size = new System.Drawing.Size(360, 64);
this.lblCodebase.TabIndex = 6;
this.lblCodebase.Text = "Application Codebase";
//
//frmAbout
//
this.AcceptButton = this.btnOK;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.btnOK;
this.ClientSize = new System.Drawing.Size(440, 232);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.lblCodebase, this.lblCopyright, this.btnOK, this.lblDescription, this.lblVersion, this.lblTitle, this.pbIcon});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frmAbout";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "About ...";
this.ResumeLayout(false);
this.Load += new EventHandler(this.frmAbout_Load);
}
#endregion
// Note: Because this form is opened by frmMain using the ShowDialog command, we simply set the
// DialogResult property of cmdOK to OK which cause the form to close when clicked.
private void frmAbout_Load(object sender, System.EventArgs e) {
try
{
// set {this Form's Text & Icon properties by using values from the parent form
this.Text = "About " + this.Owner.Text + " ...";
this.Icon = this.Owner.Icon;
// set {this Form's Picture Box's image using the parent's icon
// However, we need to convert it to a Bitmap since the Picture Box Control
// will not accept a raw Icon.
this.pbIcon.Image = this.Owner.Icon.ToBitmap();
// set {the labels identitying the Title, Version, and Description by
// reading Assembly meta-data originally entered in the AssemblyInfo.vb file
// using the AssemblyInfo class defined in the same file
AssemblyInfo ainfo = new AssemblyInfo();
this.lblTitle.Text = ainfo.Title;
this.lblVersion.Text = string.Format("Version {0}", ainfo.Version);
this.lblCopyright.Text = ainfo.Copyright;
this.lblDescription.Text = ainfo.Description;
this.lblCodebase.Text = ainfo.CodeBase;
}
catch(System.Exception exp)
{
// This catch will trap any unexpected error.
MessageBox.Show(exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
}
댓글 없음:
댓글 쓰기