| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1027 人关注过本帖
标题:多线程编程的疑问
只看楼主 加入收藏
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
结帖率:33.33%
收藏
 问题点数:0 回复次数:12 
多线程编程的疑问

我写了一个摇奖机的程序,可是在我运行时点击恢复按钮时代码有时没有被执行,但是有时候却又被执行了,这是为什么啊??代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace WindowsApplication8
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Threading.Thread thread1;
private System.Threading.Thread thread2;
private System.Threading.Thread thread3;
private System.Threading.Thread thread4;
private System.Threading.Thread thread5;
private System.Threading.Thread thread6;

/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 80);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(40, 40);
this.label1.TabIndex = 0;
//
// label2
//
this.label2.Location = new System.Drawing.Point(80, 80);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(40, 40);
this.label2.TabIndex = 1;
//
// label3
//
this.label3.Location = new System.Drawing.Point(144, 80);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(40, 40);
this.label3.TabIndex = 2;
//
// label4
//
this.label4.Location = new System.Drawing.Point(208, 80);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(40, 40);
this.label4.TabIndex = 3;
//
// label5
//
this.label5.Location = new System.Drawing.Point(272, 80);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(40, 40);
this.label5.TabIndex = 4;
//
// label6
//
this.label6.Location = new System.Drawing.Point(344, 80);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(40, 40);
this.label6.TabIndex = 5;
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 208);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 32);
this.button1.TabIndex = 6;
this.button1.Text = "开始";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(240, 208);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 32);
this.button2.TabIndex = 7;
this.button2.Text = "恢复";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(336, 208);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 32);
this.button3.TabIndex = 8;
this.button3.Text = "退出";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(136, 208);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(64, 32);
this.button4.TabIndex = 9;
this.button4.Text = "暂停";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(432, 294);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
thread1 = new Thread(new ThreadStart(run1));
thread2 = new Thread(new ThreadStart(run2));
thread3 = new Thread(new ThreadStart(run3));
thread4 = new Thread(new ThreadStart(run4));
thread5 = new Thread(new ThreadStart(run5));
thread6 = new Thread(new ThreadStart(run6));

thread1.Start();
thread2.Start();
thread3.Start();
thread4.Start();
thread5.Start();
thread6.Start();

}

public void run1()
{

Random one = new Random(1);
do
{
int oneThread = one.Next(10);
this.label1.Text = oneThread.ToString();
Thread.Sleep(10);

}
while(true);
}

public void run2()
{

Random two = new Random(2);
do
{
int twoThread = two.Next(10);
this.label2.Text = twoThread.ToString();
Thread.Sleep(10);
}
while(true);
}

public void run3()
{

Random three = new Random(3);
do
{
int threeThread = three.Next(10);
this.label3.Text = threeThread.ToString();
Thread.Sleep(10);
}
while(true);
}

public void run4()
{

Random four = new Random(4);
do
{
int foureThread = four.Next(10);
this.label4.Text = foureThread.ToString();
Thread.Sleep(10);
}
while(true);
}

public void run5()
{

Random five = new Random(5);
do
{
int fiveThread = five.Next(10);
this.label5.Text = fiveThread.ToString();
Thread.Sleep(10);
}
while(true);
}

public void run6()
{

Random six = new Random(6);
do
{
int sixThread = six.Next(10);
this.label6.Text = sixThread.ToString();
Thread.Sleep(10);
}
while(true);
}

private void button3_Click(object sender, System.EventArgs e)
{
try
{
thread1.Abort();
thread2.Abort();
thread3.Abort();
thread4.Abort();
thread5.Abort();
thread6.Abort();
Application.Exit();
}
catch (Exception te)
{
MessageBox.Show(te.Message);
}
}

private void button2_Click(object sender, System.EventArgs e)
{
try
{
if(thread1.ThreadState == ThreadState.Suspended || thread2.ThreadState == ThreadState.Suspended ||
thread3.ThreadState == ThreadState.Suspended || thread4.ThreadState == ThreadState.Suspended ||
thread5.ThreadState == ThreadState.Suspended || thread6.ThreadState == ThreadState.Suspended ||
thread1.ThreadState == ThreadState.WaitSleepJoin || thread2.ThreadState == ThreadState.WaitSleepJoin ||
thread3.ThreadState == ThreadState.WaitSleepJoin || thread4.ThreadState == ThreadState.WaitSleepJoin ||
thread5.ThreadState == ThreadState.WaitSleepJoin || thread6.ThreadState == ThreadState.WaitSleepJoin ||
thread1.ThreadState == ThreadState.SuspendRequested || thread2.ThreadState == ThreadState.SuspendRequested ||
thread3.ThreadState == ThreadState.SuspendRequested || thread4.ThreadState == ThreadState.SuspendRequested ||
thread5.ThreadState == ThreadState.SuspendRequested || thread6.ThreadState == ThreadState.SuspendRequested)
{
thread1.Resume();
thread2.Resume();
thread3.Resume();
thread4.Resume();
thread5.Resume();
thread6.Resume();
}
}
catch (Exception te)
{
MessageBox.Show(te.Message);
}
}

private void button4_Click(object sender, System.EventArgs e)
{
try
{
if(thread1.ThreadState == ThreadState.Running || thread2.ThreadState == ThreadState.Running ||
thread3.ThreadState == ThreadState.Running || thread4.ThreadState == ThreadState.Running ||
thread5.ThreadState == ThreadState.Running || thread6.ThreadState == ThreadState.Running ||
thread1.ThreadState == ThreadState.WaitSleepJoin || thread2.ThreadState == ThreadState.WaitSleepJoin ||
thread3.ThreadState == ThreadState.WaitSleepJoin || thread4.ThreadState == ThreadState.WaitSleepJoin ||
thread5.ThreadState == ThreadState.WaitSleepJoin || thread6.ThreadState == ThreadState.WaitSleepJoin)
{
thread1.Suspend();
thread2.Suspend();
thread3.Suspend();
thread4.Suspend();
thread5.Suspend();
thread6.Suspend();
}
}
catch (Exception te)
{
MessageBox.Show(te.Message);
}
}
}
}

搜索更多相关主题的帖子: using 线程 System 疑问 summary 
2006-11-11 15:43
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 

怎么又没有人啊??


2006-11-11 21:29
这个真叫烦那
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2006-7-9
收藏
得分:0 
呵呵,把有问题的代码发上来.
别这样发代码,会吓跑人呢!!!

if(不耻下问) { MessageBox.Show(\"好好学习,天天向上\"); }
2006-11-12 15:51
skyland84
Rank: 2
等 级:新手上路
威 望:4
帖 子:544
专家分:0
注 册:2006-10-9
收藏
得分:0 
哈哈~!
就是啊~!
这样的代码 看了 眼睛多难受啊~!
又没有注释~!
想吓死人啊~!
哈哈

决定人生~
2006-11-13 13:28
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 
也是不过编译时没有错误 运行时也可以运行
就是那个叫“恢复”的按键不听话啊!!!
try
{
if(thread1.ThreadState == ThreadState.Running || thread2.ThreadState == ThreadState.Running ||
thread3.ThreadState == ThreadState.Running || thread4.ThreadState == ThreadState.Running ||
thread5.ThreadState == ThreadState.Running || thread6.ThreadState == ThreadState.Running ||
thread1.ThreadState == ThreadState.WaitSleepJoin || thread2.ThreadState ==ThreadState.WaitSleepJoin || thread3.ThreadState == ThreadState.WaitSleepJoin || thread4.ThreadState == ThreadState.WaitSleepJoin ||
thread5.ThreadState == ThreadState.WaitSleepJoin || thread6.ThreadState == ThreadState.WaitSleepJoin)
{
thread1.Suspend();
thread2.Suspend();
thread3.Suspend();
thread4.Suspend();
thread5.Suspend();
thread6.Suspend();
}
}
catch (Exception te)
{
MessageBox.Show(te.Message);
}
这就是那段有问题的代码!

2006-11-13 20:22
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
你在try设置断点看什么回事嘛
2006-11-14 11:00
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
哦,这个问题是UI线程与分支线程的问题,你这样写想法上是可行,2003也不提示什么,而且是异常的

就是说分支线程不允许直接操作UI线程
2006-11-14 11:17
ruihua
Rank: 1
等 级:新手上路
威 望:1
帖 子:235
专家分:0
注 册:2006-2-21
收藏
得分:0 
我觉得程序应该没有你写的那个复杂吧

2006-11-14 15:03
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 
怎么才能解决这个问题啊??

2006-11-14 17:46
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
2006-11-14 17:58
快速回复:多线程编程的疑问
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013839 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved