线程和事件(形式奇怪) [英] Threads and Events (form oddness)

查看:67
本文介绍了线程和事件(形式奇怪)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在这里发帖,所以请原谅我,如果我做了什么

不正确。


我一直在学习C#和工作与不同的东西,并决定我想要进入多线程的
。我的问题是我一定不能正确行事

,因为只有部分内容可以正常运作。我会发布

到底发生了什么,然后我会发布我正在使用的示例代码

给我的问题。我确定它是我忽略的东西,或者我只是

完全错误。


以下是我的代码样品申请。它有2个按钮和一个标签。

基本上,线程只是让标签可见并且每5秒循环

。顶部按钮启动线程,底部

按钮调用Cancel()方法,该方法关闭阻止并允许

线程正确退出。问题是标签有时看起来会变得可见,但是如果表格最小化,或者窗户移过它,那么你会看到它真的不。此外..表格似乎变得非常滞后和

表现得好像它的处理真的很难(尽管它不像

任务管理员说它使用非常少的CPU)。 br />

您可能会问为什么我正在使用事件,这是因为我打算做的是

制作一个工作线程,它会定期发送更新进入状态栏的主要

程序。为什么我使用线程和事件并不重要。重要的是我必须做错事,而且我想要学习如何正确地做到这一点。


无论如何......那是我的问题..这里是来源......

=============================


使用System;

使用System.Drawing;

使用System.Collections;

使用System.ComponentModel ;

使用System.Windows.Forms;

使用System.Data;

使用System.Threading;


namespace ThreadTest

{

#region TestEventHandler

public delegate void TestEventHandler(object sender,TestEventArgs e);

公共类TestEventArgs:EventArgs

{

public bool ShowLabel = false;

public TestEventArgs(bool bShowLabel){ShowLabel = bShowLabel ; }

}

#endregion

#region测试线程

公共类clsThreadTest

{

公共事件TestEventHandler ShowLabel;

private bool blocked = false;

private Thread TestThread = null;

public clsThreadTest(){}

public void SetLabel(bool bVisible)

{

if(ShowLabel!= null)

{

TestEventArgs tea = new TestEventArgs(bVisible);

ShowLabel(this,tea);

}

}

public void Start()

{

if(!blocked)

{

阻止=!阻止;

TestThread =新线程(新ThreadStart(RunTest));

TestThread.ApartmentState = ApartmentState.MTA;

TestThread.Priority = ThreadPriority.Lowest;

TestThread.Name =" Test Thread";

TestThread.Start();

}

}


public void取消()

{

blocked = F另外;

}

public void RunTest()

{

while(阻止)

{

SetLabel(true);

Thread.Sleep(5000);

}

}

}

#endregion

公共类Form1:System.Windows.Forms.Form

{

private clsThreadTest cThreadTest = new clsThreadTest();

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Button button2;

private System.ComponentModel.Container components = null;


public Form1 ()

{

InitializeComponent();

cThreadTest.ShowLabel + = new

TestEventHandler(cThreadTest_ShowLabel);


}


public void cThreadTest_ShowLabel(对象发送者,TestEventArgs茶)

{

label1.Visible = tea.ShowLabel;

label1.Refresh();

this.Refresh();

}


protected override void Dispose(bool disposing)

{

if(disposing)

{

if(components!= null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}


#region Windows窗体设计器生成的代码

///< summary>

/// Designer支持所需的方法 - 不要修改

///这个内容使用代码编辑器的方法。

///< / summary>

private void InitializeComponent()

{

this.label1 = new System.Windows.Forms.Label();

this.button1 = new System.Windows.Forms.Button();

this。 button2 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// label1

//

this.label1.Font = new System.Drawing.Font(" Microsoft Sans

Serif",12F,System.Drawing。 FontStyle.Bold,<无线电通信/>
System.Drawing.GraphicsUnit.Point,((System.Byte)(0)));

this.label1.ForeColor = System.Drawing.Color.Red;

this.label1.Location = new System.Drawing.Point(42,12);

this.label1.Name =" label1";

this.label1.Size = new System.Drawing.Size(108,24);

this.label1.TabIndex = 0;

this.label1.Text =" ;你好!" ;;

this.label1.TextAlign =

System.Drawing.ContentAlignment.MiddleCenter;

this.label1.Visible = false;

//

// button1

//

this.button1.Location = new System。 Drawing.Point(186,6);

this.button1.Name =" button1";

this.button1.Size = new System.Drawing.Size(96 ,18);

this.button1.TabIndex = 1;

this.button1.Text =" Start Thread"

this。 button1.Click + = new

System.EventHandler(this.button1_Click);

//

// button2

//

this.button2.Location = new System.Drawing.Point(186,24);

this.button2.Name =" button2";

this .button2.Size = new System.Drawing.Size(96,18);

this.button2.TabIndex = 2;

this.button2.Text =" Queue停止;

this.button2.Click + = new

System.EventHandler(this.button2_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5,13);

this .ClientSize = new System.Drawing.Size(292,45);

this.Controls.AddRange(new System.Windows.Forms.Control [] {


this.button2,

this.button1,


this.label1});

这个。 Name =" Form1";

this.Text =" Form1";

this.ResumeLayout(false);


}

#endregion


[STAThread]

static void Main()

{

Application.Run(new Form1());

}


private void button1_Click(object sender,System.EventArgs e)

{

cThreadTest.Start();

}


private void button2_Click(object sender,System.EventArgs e)

{

cThreadTest.Cancel( );

}

}

}

This is my first time posting here, so please forgive me if I do anything
incorrectly.

I''ve been learning C# and working with different things and decided I wanted
to get into Multi-Threading. My problem is that I must not be doing it right
because only some of the stuff works as would be expected. I''ll post what
exactly is happening, then I''ll post the sample code I''m using that is
giving me the problems. I''m sure its something I''ve overlooked, or I''m just
doing it completely wrong.

Below is the code for my sample application. It has 2 buttons and a label.
Basically, the thread is just supposed to make the label visible and loop
every 5 seconds. the top button starts the thread going, and the bottom
button calls the Cancel() method which turns off blocking and allows the
thread to exit properly. The problem is that the label sometimes appears to
become visible, but if the form is minimized, or a window is moved over it,
you''ll see its really not. Also.. the Form seems to become VERY laggy and
acts as though its processing REALLY hard (although its not as the
taskmanager says its using very little CPU).

You may ask why I''m using Events and that is because what I plan on doing is
making a worker thread that will send periodic updates back to the main
program to progress a status bar. Why I''m using threads and events isn''t
important. What is important is that I must be doing something wrong, and I
want to learn how I should do it correctly.

Anyway.. that is my problem.. here is the source...
=============================

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace ThreadTest
{
#region TestEventHandler
public delegate void TestEventHandler(object sender, TestEventArgs e);
public class TestEventArgs : EventArgs
{
public bool ShowLabel = false;
public TestEventArgs(bool bShowLabel) { ShowLabel = bShowLabel; }
}
#endregion
#region Test Thread
public class clsThreadTest
{
public event TestEventHandler ShowLabel;
private bool blocked = false;
private Thread TestThread = null;
public clsThreadTest() { }
public void SetLabel(bool bVisible)
{
if (ShowLabel != null)
{
TestEventArgs tea = new TestEventArgs(bVisible);
ShowLabel(this, tea);
}
}
public void Start()
{
if (!blocked)
{
blocked = !blocked;
TestThread = new Thread(new ThreadStart(RunTest));
TestThread.ApartmentState = ApartmentState.MTA;
TestThread.Priority = ThreadPriority.Lowest;
TestThread.Name = "Test Thread";
TestThread.Start();
}
}

public void Cancel()
{
blocked = false;
}
public void RunTest()
{
while (blocked)
{
SetLabel(true);
Thread.Sleep(5000);
}
}
}
#endregion
public class Form1 : System.Windows.Forms.Form
{
private clsThreadTest cThreadTest = new clsThreadTest();
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
cThreadTest.ShowLabel += new
TestEventHandler(cThreadTest_ShowLabel);

}

public void cThreadTest_ShowLabel(object sender, TestEventArgs tea)
{
label1.Visible = tea.ShowLabel;
label1.Refresh();
this.Refresh();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 12F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(42, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(108, 24);
this.label1.TabIndex = 0;
this.label1.Text = "Hello!";
this.label1.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Visible = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(186, 6);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 18);
this.button1.TabIndex = 1;
this.button1.Text = "Start Thread";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(186, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 18);
this.button2.TabIndex = 2;
this.button2.Text = "Queue Stop";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 45);
this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.button2,

this.button1,

this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

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

private void button1_Click(object sender, System.EventArgs e)
{
cThreadTest.Start();
}

private void button2_Click(object sender, System.EventArgs e)
{
cThreadTest.Cancel();
}
}
}

推荐答案

一些事情很突出。


1.除了创建UI元素的那个线程之外,你不能更新UI元素。由于您的表单是在

主线程上创建的,因此只有主线程可以更新表单和/或

它的子控件。从另一个线程执行此操作会导致

最多不一致的结果。


您必须使用Control.Invoke同步调用您的

代理另一个线程或Control.BeginInvoke异步

调用它。


另外,你取消辅助线程的方法实际上不是

适当的方式去。考虑在循环中使用ManualResetEvent和

WaitOne调用。单击取消按钮后,设置

事件。在循环处理中,你做了一个


//假设ev是一个ManualResetEvent


而(true)

{

if(ev.WaitOne(5000))

breakl //事件被发出信号


... //在这里做其他的东西

}


您可以在表单中创建ManualResetEvent并将其作为

参数传递给您的& ;线程"
Jonathan Schafer

周五,2003年8月1日19:08:53 -0700,Drakier Dominaeus

< dr ***** @ cox.net>写道:
A couple things stick out.

1. You cannot update UI elements from any thread other than the one
that created the UI element. Since your form was created on the
Primary thread, only the primary thread can update the form and/or
it''s child controls. Doing so from another thread leads to
inconsistent results at best.

You would have to use Control.Invoke to synchronously call your
delegate on the other thread or Control.BeginInvoke to asynchronously
call it.

Also, your method of cancelling the secondary thread is really not the
appropriate way to go. Consider using a ManualResetEvent and a
WaitOne call in your loop. When your Cancel button is clicked, set
the event. In your loop processing, you do a

// Assume ev is a ManualResetEvent

while (true)
{
if (ev.WaitOne(5000))
breakl // event was signalled

... // do other stuff here
}

You can create the ManualResetEvent in your Form and pass it as a
parameter to your "Thread" class.

Jonathan Schafer
On Fri, 1 Aug 2003 19:08:53 -0700, "Drakier Dominaeus"
<dr*****@cox.net> wrote:
这是我第一次在这里发帖,如果我做错了什么,请原谅我。

我一直在学习C#并使用不同的东西,并决定我想进入多线程。我的问题是我一定不能做得好
因为只有一些东西按预期工作。我会发布
到底发生了什么,然后我会发布我正在使用的示例代码
给我的问题。我确定它是我忽略的东西,或者我只是完全错了。

以下是我的示例应用程序的代码。它有2个按钮和一个标签。
基本上,线程只是让标签可见并且每5秒循环一次。顶部按钮启动线程,底部
按钮调用Cancel()方法,该方法关闭阻止并允许
线程正确退出。问题是标签有时会变得可见,但如果表格最小化,或者窗户移过它,你会发现它真的不是。此外..表格似乎变得非常迟钝,并且好像它的处理真的很难(虽然它不像
任务管理员说它使用非常少的CPU)。

你我可能会问为什么我正在使用事件,这是因为我打算做的是制作一个工作线程,它会定期将更新发送回主程序以进行状态栏。为什么我使用线程和事件并不重要。重要的是我必须做错事,并且我想学习如何正确地做到这一点。

无论如何......这是我的问题..这里是来源。 ..

=============================

使用System;
使用System.Drawing;
使用System.Collections;
使用System.ComponentModel;
使用System.Windows.Forms;
使用System.Data;
使用System.Threading;

命名空间ThreadTest
#region TestEventHandler
公共委托void TestEventHandler(对象发送者,TestEventArgs e);
公共类TestEventArgs:EventArgs
{
public bool ShowLabel = false;
public TestEventArgs(bool bShowLabel){ShowLabel = bShowLabel; }
}
#endregion
#region测试线程
公共类clsThreadTest
{
公共事件TestEventHandler ShowLabel;
私有bool被阻止= false ;
私有线程TestThread = null;
public clsThreadTest(){}
public void SetLabel(bool bVisible)
{
if(ShowLabel!= null)
{
TestEventArgs tea = new TestEventArgs(bVisible);
ShowLabel(this,tea);
}
}
public void Start()
{
if(!blocked)
{block =!blocked;
TestThread = new Thread(new ThreadStart(RunTest));
TestThread.ApartmentState = ApartmentState。 MTA;
TestThread.Priority = ThreadPriority.Lowest;
TestThread.Name =" Te st Thread" ;;
TestThread.Start();
}
}
公共无效取消()
{
blocked = false;
}
public void RunTest()
{
while(阻止)
{
SetLabel(true);
Thread.Sleep(5000 );
}
}
#endregion
公共类Form1:System.Windows.Forms.Form
私有clsThreadTest cThreadTest = new clsThreadTest();
私有System.Windows.Forms.Label label1;
私有System.Windows.Forms.Button button1;
私有System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;

public Form1()
{/> InitializeComponent();
cThreadTest.ShowLabel + = new
Ť estEventHandler(cThreadTest_ShowLabel);



public void cThreadTest_ShowLabel(object sender,TestEventArgs tea)
{
label1.Visible = tea.ShowLabel; < br。> label1.Refresh();
this.Refresh();
}

protected override void Dispose(bool disposing)
{
if (处理)
{
if(components!= null)
{
components.Dispose();
}
}
base。处理(处理);
}
#region Windows窗体设计器生成的代码
///< summary>
///设计器支持所需的方法 - 做不要使用代码编辑器修改此方法的内容。
///< / summary>
private void InitializeComponen t()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
这个。 label1.Font = new System.Drawing.Font(" Microsoft Sans
Serif",12F,System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point,((System.Byte) (0)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(42,12);
this.label1.Name =" label1";
this.label1.Size = new System.Drawing.Size(108,24);
this.label1.TabIndex = 0;
this .label1.Text =" Hello!";
this.label1.TextAlign =
System.Drawing.ContentAlignment.Mid dleCenter;
this.label1.Visible = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point( 186,6);
this.button1.Name =" button1";
this.button1.Size = new System.Drawing.Size(96,18);
this.button1。 TabIndex = 1;
this.button1.Text =" Start Thread" ;;
this.button1.Click + = new
System.EventHandler(this.button1_Click);
/ /
// button2
//
this.button2.Location = new System.Drawing.Point(186,24);
this.button2.Name =" button2" ;
this.button2.Size = new System.Drawing.Size(96,18);
this.button2.TabIndex = 2;
this.button2.Text =" Queue Stop" ;
this.button2.Click + = new
System.Ev entHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5,13);
this.ClientSize = new System.Drawing.Size(292,45);
this.Controls.AddRange(new System.Windows.Forms.Control [] {

this.button2,

this.button1,

this.label1});
this.Name =" Form1";
this.Text =" Form1" ;
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}
私有void button1_Click(对象发送者,System.EventArgs e)

cThreadTest.Start();
}
私有void button2_Click(对象发送者,System.EventArgs e)
{ cThreadTest.Cancel();
}
}
}
This is my first time posting here, so please forgive me if I do anything
incorrectly.

I''ve been learning C# and working with different things and decided I wanted
to get into Multi-Threading. My problem is that I must not be doing it right
because only some of the stuff works as would be expected. I''ll post what
exactly is happening, then I''ll post the sample code I''m using that is
giving me the problems. I''m sure its something I''ve overlooked, or I''m just
doing it completely wrong.

Below is the code for my sample application. It has 2 buttons and a label.
Basically, the thread is just supposed to make the label visible and loop
every 5 seconds. the top button starts the thread going, and the bottom
button calls the Cancel() method which turns off blocking and allows the
thread to exit properly. The problem is that the label sometimes appears to
become visible, but if the form is minimized, or a window is moved over it,
you''ll see its really not. Also.. the Form seems to become VERY laggy and
acts as though its processing REALLY hard (although its not as the
taskmanager says its using very little CPU).

You may ask why I''m using Events and that is because what I plan on doing is
making a worker thread that will send periodic updates back to the main
program to progress a status bar. Why I''m using threads and events isn''t
important. What is important is that I must be doing something wrong, and I
want to learn how I should do it correctly.

Anyway.. that is my problem.. here is the source...
=============================

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace ThreadTest
{
#region TestEventHandler
public delegate void TestEventHandler(object sender, TestEventArgs e);
public class TestEventArgs : EventArgs
{
public bool ShowLabel = false;
public TestEventArgs(bool bShowLabel) { ShowLabel = bShowLabel; }
}
#endregion
#region Test Thread
public class clsThreadTest
{
public event TestEventHandler ShowLabel;
private bool blocked = false;
private Thread TestThread = null;
public clsThreadTest() { }
public void SetLabel(bool bVisible)
{
if (ShowLabel != null)
{
TestEventArgs tea = new TestEventArgs(bVisible);
ShowLabel(this, tea);
}
}
public void Start()
{
if (!blocked)
{
blocked = !blocked;
TestThread = new Thread(new ThreadStart(RunTest));
TestThread.ApartmentState = ApartmentState.MTA;
TestThread.Priority = ThreadPriority.Lowest;
TestThread.Name = "Test Thread";
TestThread.Start();
}
}

public void Cancel()
{
blocked = false;
}
public void RunTest()
{
while (blocked)
{
SetLabel(true);
Thread.Sleep(5000);
}
}
}
#endregion
public class Form1 : System.Windows.Forms.Form
{
private clsThreadTest cThreadTest = new clsThreadTest();
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
cThreadTest.ShowLabel += new
TestEventHandler(cThreadTest_ShowLabel);

}

public void cThreadTest_ShowLabel(object sender, TestEventArgs tea)
{
label1.Visible = tea.ShowLabel;
label1.Refresh();
this.Refresh();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 12F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(42, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(108, 24);
this.label1.TabIndex = 0;
this.label1.Text = "Hello!";
this.label1.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Visible = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(186, 6);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 18);
this.button1.TabIndex = 1;
this.button1.Text = "Start Thread";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(186, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 18);
this.button2.TabIndex = 2;
this.button2.Text = "Queue Stop";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 45);
this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.button2,

this.button1,

this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

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

private void button1_Click(object sender, System.EventArgs e)
{
cThreadTest.Start();
}

private void button2_Click(object sender, System.EventArgs e)
{
cThreadTest.Cancel();
}
}
}






一些东西坚持下去。


1.您无法从创建UI元素的其他线程更新UI元素。由于您的表单是在

主线程上创建的,因此只有主线程可以更新表单和/或

它的子控件。从另一个线程执行此操作会导致

最多不一致的结果。


您必须使用Control.Invoke同步调用您的

代理另一个线程或Control.BeginInvoke异步

调用它。


另外,你取消辅助线程的方法实际上不是

适当的方式去。考虑在循环中使用ManualResetEvent和

WaitOne调用。单击取消按钮后,设置

事件。在循环处理中,你做了一个


//假设ev是一个ManualResetEvent


而(true)

{

if(ev.WaitOne(5000))

breakl //事件被发出信号


... //在这里做其他的东西

}


您可以在表单中创建ManualResetEvent并将其作为

参数传递给您的& ;线程"
Jonathan Schafer

周五,2003年8月1日19:08:53 -0700,Drakier Dominaeus

< dr ***** @ cox.net>写道:
A couple things stick out.

1. You cannot update UI elements from any thread other than the one
that created the UI element. Since your form was created on the
Primary thread, only the primary thread can update the form and/or
it''s child controls. Doing so from another thread leads to
inconsistent results at best.

You would have to use Control.Invoke to synchronously call your
delegate on the other thread or Control.BeginInvoke to asynchronously
call it.

Also, your method of cancelling the secondary thread is really not the
appropriate way to go. Consider using a ManualResetEvent and a
WaitOne call in your loop. When your Cancel button is clicked, set
the event. In your loop processing, you do a

// Assume ev is a ManualResetEvent

while (true)
{
if (ev.WaitOne(5000))
breakl // event was signalled

... // do other stuff here
}

You can create the ManualResetEvent in your Form and pass it as a
parameter to your "Thread" class.

Jonathan Schafer
On Fri, 1 Aug 2003 19:08:53 -0700, "Drakier Dominaeus"
<dr*****@cox.net> wrote:
这是我第一次在这里发帖,如果我做错了什么,请原谅我。

我一直在学习C#并使用不同的东西,并决定我想进入多线程。我的问题是我一定不能做得好
因为只有一些东西按预期工作。我会发布
到底发生了什么,然后我会发布我正在使用的示例代码
给我的问题。我确定它是我忽略的东西,或者我只是完全错了。

以下是我的示例应用程序的代码。它有2个按钮和一个标签。
基本上,线程只是让标签可见并且每5秒循环一次。顶部按钮启动线程,底部
按钮调用Cancel()方法,该方法关闭阻止并允许
线程正确退出。问题是标签有时会变得可见,但如果表格最小化,或者窗户移过它,你会发现它真的不是。此外..表格似乎变得非常迟钝,并且好像它的处理真的很难(虽然它不像
任务管理员说它使用非常少的CPU)。

你我可能会问为什么我正在使用事件,这是因为我打算做的是制作一个工作线程,它会定期将更新发送回主程序以进行状态栏。为什么我使用线程和事件并不重要。重要的是我必须做错事,并且我想学习如何正确地做到这一点。

无论如何......这是我的问题..这里是来源。 ..

=============================

使用System;
使用System.Drawing;
使用System.Collections;
使用System.ComponentModel;
使用System.Windows.Forms;
使用System.Data;
使用System.Threading;

命名空间ThreadTest
#region TestEventHandler
公共委托void TestEventHandler(对象发送者,TestEventArgs e);
公共类TestEventArgs:EventArgs
{
public bool ShowLabel = false;
public TestEventArgs(bool bShowLabel){ShowLabel = bShowLabel; }
}
#endregion
#region测试线程
公共类clsThreadTest
{
公共事件TestEventHandler ShowLabel;
私有bool被阻止= false ;
私有线程TestThread = null;
public clsThreadTest(){}
public void SetLabel(bool bVisible)
{
if(ShowLabel!= null)
{
TestEventArgs tea = new TestEventArgs(bVisible);
ShowLabel(this,tea);
}
}
public void Start()
{
if(!blocked)
{block =!blocked;
TestThread = new Thread(new ThreadStart(RunTest));
TestThread.ApartmentState = ApartmentState。 MTA;
TestThread.Priority = ThreadPriority.Lowest;
TestThread.Name =" Te st Thread" ;;
TestThread.Start();
}
}
公共无效取消()
{
blocked = false;
}
public void RunTest()
{
while(阻止)
{
SetLabel(true);
Thread.Sleep(5000 );
}
}
#endregion
公共类Form1:System.Windows.Forms.Form
私有clsThreadTest cThreadTest = new clsThreadTest();
私有System.Windows.Forms.Label label1;
私有System.Windows.Forms.Button button1;
私有System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;

public Form1()
{/> InitializeComponent();
cThreadTest.ShowLabel + = new
Ť estEventHandler(cThreadTest_ShowLabel);



public void cThreadTest_ShowLabel(object sender,TestEventArgs tea)
{
label1.Visible = tea.ShowLabel; < br。> label1.Refresh();
this.Refresh();
}

protected override void Dispose(bool disposing)
{
if (处理)
{
if(components!= null)
{
components.Dispose();
}
}
base。处理(处理);
}
#region Windows窗体设计器生成的代码
///< summary>
///设计器支持所需的方法 - 做不要使用代码编辑器修改此方法的内容。
///< / summary>
private void InitializeComponen t()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
这个。 label1.Font = new System.Drawing.Font(" Microsoft Sans
Serif",12F,System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point,((System.Byte) (0)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(42,12);
this.label1.Name =" label1";
this.label1.Size = new System.Drawing.Size(108,24);
this.label1.TabIndex = 0;
this .label1.Text =" Hello!";
this.label1.TextAlign =
System.Drawing.ContentAlignment.Mid dleCenter;
this.label1.Visible = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point( 186,6);
this.button1.Name =" button1";
this.button1.Size = new System.Drawing.Size(96,18);
this.button1。 TabIndex = 1;
this.button1.Text =" Start Thread" ;;
this.button1.Click + = new
System.EventHandler(this.button1_Click);
/ /
// button2
//
this.button2.Location = new System.Drawing.Point(186,24);
this.button2.Name =" button2" ;
this.button2.Size = new System.Drawing.Size(96,18);
this.button2.TabIndex = 2;
this.button2.Text =" Queue Stop" ;
this.button2.Click + = new
System.Ev entHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5,13);
this.ClientSize = new System.Drawing.Size(292,45);
this.Controls.AddRange(new System.Windows.Forms.Control [] {

this.button2,

this.button1,

this.label1});
this.Name =" Form1";
this.Text =" Form1" ;
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}
私有void button1_Click(对象发送者,System.EventArgs e)

cThreadTest.Start();
}
私有void button2_Click(对象发送者,System.EventArgs e)
{ cThreadTest.Cancel();
}
}
}
This is my first time posting here, so please forgive me if I do anything
incorrectly.

I''ve been learning C# and working with different things and decided I wanted
to get into Multi-Threading. My problem is that I must not be doing it right
because only some of the stuff works as would be expected. I''ll post what
exactly is happening, then I''ll post the sample code I''m using that is
giving me the problems. I''m sure its something I''ve overlooked, or I''m just
doing it completely wrong.

Below is the code for my sample application. It has 2 buttons and a label.
Basically, the thread is just supposed to make the label visible and loop
every 5 seconds. the top button starts the thread going, and the bottom
button calls the Cancel() method which turns off blocking and allows the
thread to exit properly. The problem is that the label sometimes appears to
become visible, but if the form is minimized, or a window is moved over it,
you''ll see its really not. Also.. the Form seems to become VERY laggy and
acts as though its processing REALLY hard (although its not as the
taskmanager says its using very little CPU).

You may ask why I''m using Events and that is because what I plan on doing is
making a worker thread that will send periodic updates back to the main
program to progress a status bar. Why I''m using threads and events isn''t
important. What is important is that I must be doing something wrong, and I
want to learn how I should do it correctly.

Anyway.. that is my problem.. here is the source...
=============================

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace ThreadTest
{
#region TestEventHandler
public delegate void TestEventHandler(object sender, TestEventArgs e);
public class TestEventArgs : EventArgs
{
public bool ShowLabel = false;
public TestEventArgs(bool bShowLabel) { ShowLabel = bShowLabel; }
}
#endregion
#region Test Thread
public class clsThreadTest
{
public event TestEventHandler ShowLabel;
private bool blocked = false;
private Thread TestThread = null;
public clsThreadTest() { }
public void SetLabel(bool bVisible)
{
if (ShowLabel != null)
{
TestEventArgs tea = new TestEventArgs(bVisible);
ShowLabel(this, tea);
}
}
public void Start()
{
if (!blocked)
{
blocked = !blocked;
TestThread = new Thread(new ThreadStart(RunTest));
TestThread.ApartmentState = ApartmentState.MTA;
TestThread.Priority = ThreadPriority.Lowest;
TestThread.Name = "Test Thread";
TestThread.Start();
}
}

public void Cancel()
{
blocked = false;
}
public void RunTest()
{
while (blocked)
{
SetLabel(true);
Thread.Sleep(5000);
}
}
}
#endregion
public class Form1 : System.Windows.Forms.Form
{
private clsThreadTest cThreadTest = new clsThreadTest();
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
cThreadTest.ShowLabel += new
TestEventHandler(cThreadTest_ShowLabel);

}

public void cThreadTest_ShowLabel(object sender, TestEventArgs tea)
{
label1.Visible = tea.ShowLabel;
label1.Refresh();
this.Refresh();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 12F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(42, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(108, 24);
this.label1.TabIndex = 0;
this.label1.Text = "Hello!";
this.label1.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Visible = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(186, 6);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 18);
this.button1.TabIndex = 1;
this.button1.Text = "Start Thread";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(186, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 18);
this.button2.TabIndex = 2;
this.button2.Text = "Queue Stop";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 45);
this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.button2,

this.button1,

this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

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

private void button1_Click(object sender, System.EventArgs e)
{
cThreadTest.Start();
}

private void button2_Click(object sender, System.EventArgs e)
{
cThreadTest.Cancel();
}
}
}






我''我不确定ManualResetEvents如何工作。我之前从未使用过它们,所以

我不确定它们是什么,或者它们是如何被使用的。如果你能解释一下

a一点点,并且可能包含我的一些源代码修改后它应该是怎么回事,我想我会理解它好一点上下文。从另一个线程,

我了解到我无法控制来自其他线程的UI元素,而且有人建议我可以使用Invoke。我主要想知道正确的方法

这样我第一次就能正确学习而不会养成坏习惯。


-Drakier


" Jonathan Schafer" < jschafer@*NOSPAM*brierley.a.b.c.com>在消息中写道

news:45 ******************************** @ 4ax.com ...
I''m not sure how the ManualResetEvents work. I''ve never used them before, so
I''m not sure what they are, or how they are used. If you could explain that
a little, and maybe include some of my source code modified to how it should
be, I think I''ll understand it a bit better in context. From another thread,
I learned that I cannot control UI elements from other threads, and someone
else suggested I could use Invoke. I mainly want to know the proper method
so that I can learn right the first time and not get into bad habits.

-Drakier

"Jonathan Schafer" <jschafer@*NOSPAM*brierley.a.b.c.com> wrote in message
news:45********************************@4ax.com...
有几件事情发生了。

1.您无法从创建UI元素的线程以外的任何线程更新UI元素。由于您的表单是在主线程上创建的,因此只有主线程可以更新表单和/或它的子控件。从另一个线程执行此操作最多会导致结果不一致。

您必须使用Control.Invoke在另一个线程或Control.BeginInvoke上同步调用您的
代理异步
称之为。

此外,您取消辅助线程的方法实际上不是适当的方法。考虑在循环中使用ManualResetEvent和
WaitOne调用。单击取消按钮后,设置
事件。在循环处理中,你做了一个

//假设ev是一个ManualResetEvent

而(true)
{
if(ev.WaitOne( 5000))
breakl //事件被发出信号

... //在这里做其他的事情


你可以在你的事件中创建ManualResetEvent形成并将其作为
参数传递给您的线程。 Jonathan Schafer

周五,2003年8月1日19:08:53 -0700,Drakier Dominaeus
< dr **** *@cox.net>写道:
A couple things stick out.

1. You cannot update UI elements from any thread other than the one
that created the UI element. Since your form was created on the
Primary thread, only the primary thread can update the form and/or
it''s child controls. Doing so from another thread leads to
inconsistent results at best.

You would have to use Control.Invoke to synchronously call your
delegate on the other thread or Control.BeginInvoke to asynchronously
call it.

Also, your method of cancelling the secondary thread is really not the
appropriate way to go. Consider using a ManualResetEvent and a
WaitOne call in your loop. When your Cancel button is clicked, set
the event. In your loop processing, you do a

// Assume ev is a ManualResetEvent

while (true)
{
if (ev.WaitOne(5000))
breakl // event was signalled

... // do other stuff here
}

You can create the ManualResetEvent in your Form and pass it as a
parameter to your "Thread" class.

Jonathan Schafer
On Fri, 1 Aug 2003 19:08:53 -0700, "Drakier Dominaeus"
<dr*****@cox.net> wrote:
这是我第一次在这里发帖,如果我做错了什么,请原谅我。

我一直在学习C#和处理不同的事情并决定我想要进入多线程。我的问题是我一定不能这样做,因为只有部分内容可以正常运作。我会发布
到底发生了什么,然后我会发布我正在使用的示例代码
给我的问题。我确定它是我忽略的东西,或者我只是把它完全弄错了。

以下是我的示例应用程序的代码。它有2个按钮和
标签。基本上,线程只是让标签可见并且每5秒循环一次。顶部按钮启动线程,底部
按钮调用Cancel()方法,该方法关闭阻止并允许
线程正确退出。问题是标签有时看起来是
可见,但如果窗体最小化,或窗口移动超过
它,你会看到它真的没有。此外..表格似乎变得非常迟钝,并且好像它的处理真的很难(虽然它不像
任务管理员说它使用非常少的CPU)。

你may ask why I''m using Events and that is because what I plan on doing
ismaking a worker thread that will send periodic updates back to the main
program to progress a status bar. Why I’’m using threads and events isn’’t
important. What is important is that I must be doing something wrong, and
Iwant to learn how I should do it correctly.

Anyway.. that is my problem.. here is the source...

=============================

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace ThreadTest
{
#region TestEventHandler
public delegate void TestEventHandler(object sender, TestEventArgs
e); public class TestEventArgs : EventArgs
{
public bool ShowLabel = false;
public TestEventArgs(bool bShowLabel) { ShowLabel =
bShowLabel; } }
#endregion
#region Test Thread
public class clsThreadTest
{
public event TestEventHandler ShowLabel;
private bool blocked = false;
private Thread TestThread = null;
public clsThreadTest() { }
public void SetLabel(bool bVisible)
{
if (ShowLabel != null)
{
TestEventArgs tea = new TestEventArgs(bVisible);
ShowLabel(this, tea);
}
}
public void Start()
{
if (!blocked)
{
blocked = !blocked;
TestThread = new Thread(new ThreadStart(RunTest));
TestThread.ApartmentState = ApartmentState.MTA;
TestThread.Priority = ThreadPriority.Lowest;
TestThread.Name = "Test Thr ead";
TestThread.Start();
}
}

public void Cancel()
{
blocked = false;
}
public void RunTest()
{
while (blocked)
{
SetLabel(true);
Thread.Sleep(5000);
}
}
}
#endregion
public class Form1 : System.Windows.Forms.Form
{
private clsThreadTest cThreadTest = new clsThreadTest();
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
cThreadTest.ShowLabel += new
TestEve ntHandler(cThreadTest_ShowLabel);

}

public void cThreadTest_ShowLabel(object sender, TestEventArgs
tea) {
label1.Visible = tea.ShowLabel;
label1.Refresh();
this.Refresh();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 12F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(42, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(108, 24);
this.label1.TabIndex = 0;
this.label1.Text = "Hello!";
this.label1.TextAlign =
System.Drawing.ContentAlignment.MiddleCent er;
this.label1.Visible = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(186, 6);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 18);
this.button1.TabIndex = 1;
this.button1.Text = "Start Thread";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(186, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 18);
this.button2.TabIndex = 2;
this.button2.Text = "Queue Stop";
this.button2.Click += new
System.EventHand ler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 45);
this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.button2,

this.button1,

this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

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

private void button1_Click(object sender, System.EventArgs e)
{
cThreadTest.Start();
}

private void button2_Click(object sender, System.EventArgs e)
{
cThreadTest.Cancel();
}
}
}
This is my first time posting here, so please forgive me if I do anything
incorrectly.

I''ve been learning C# and working with different things and decided I wantedto get into Multi-Threading. My problem is that I must not be doing it rightbecause only some of the stuff works as would be expected. I''ll post what
exactly is happening, then I''ll post the sample code I''m using that is
giving me the problems. I''m sure its something I''ve overlooked, or I''m justdoing it completely wrong.

Below is the code for my sample application. It has 2 buttons and a label.Basically, the thread is just supposed to make the label visible and loop
every 5 seconds. the top button starts the thread going, and the bottom
button calls the Cancel() method which turns off blocking and allows the
thread to exit properly. The problem is that the label sometimes appears tobecome visible, but if the form is minimized, or a window is moved over it,you''ll see its really not. Also.. the Form seems to become VERY laggy and
acts as though its processing REALLY hard (although its not as the
taskmanager says its using very little CPU).

You may ask why I''m using Events and that is because what I plan on doing ismaking a worker thread that will send periodic updates back to the main
program to progress a status bar. Why I''m using threads and events isn''t
important. What is important is that I must be doing something wrong, and Iwant to learn how I should do it correctly.

Anyway.. that is my problem.. here is the source...
=============================

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace ThreadTest
{
#region TestEventHandler
public delegate void TestEventHandler(object sender, TestEventArgs e); public class TestEventArgs : EventArgs
{
public bool ShowLabel = false;
public TestEventArgs(bool bShowLabel) { ShowLabel = bShowLabel; } }
#endregion
#region Test Thread
public class clsThreadTest
{
public event TestEventHandler ShowLabel;
private bool blocked = false;
private Thread TestThread = null;
public clsThreadTest() { }
public void SetLabel(bool bVisible)
{
if (ShowLabel != null)
{
TestEventArgs tea = new TestEventArgs(bVisible);
ShowLabel(this, tea);
}
}
public void Start()
{
if (!blocked)
{
blocked = !blocked;
TestThread = new Thread(new ThreadStart(RunTest));
TestThread.ApartmentState = ApartmentState.MTA;
TestThread.Priority = ThreadPriority.Lowest;
TestThread.Name = "Test Thread";
TestThread.Start();
}
}

public void Cancel()
{
blocked = false;
}
public void RunTest()
{
while (blocked)
{
SetLabel(true);
Thread.Sleep(5000);
}
}
}
#endregion
public class Form1 : System.Windows.Forms.Form
{
private clsThreadTest cThreadTest = new clsThreadTest();
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
cThreadTest.ShowLabel += new
TestEventHandler(cThreadTest_ShowLabel);

}

public void cThreadTest_ShowLabel(object sender, TestEventArgs tea) {
label1.Visible = tea.ShowLabel;
label1.Refresh();
this.Refresh();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 12F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(42, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(108, 24);
this.label1.TabIndex = 0;
this.label1.Text = "Hello!";
this.label1.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Visible = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(186, 6);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 18);
this.button1.TabIndex = 1;
this.button1.Text = "Start Thread";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(186, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 18);
this.button2.TabIndex = 2;
this.button2.Text = "Queue Stop";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 45);
this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.button2,

this.button1,

this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

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

private void button1_Click(object sender, System.EventArgs e)
{
cThreadTest.Start();
}

private void button2_Click(object sender, System.EventArgs e)
{
cThreadTest.Cancel();
}
}
}



这篇关于线程和事件(形式奇怪)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆