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

我用一个线程来获取一个随机数然后把随机数的值添加到label上,可是当我运行这个程序时,这个程序不用多少时间就会没有响应了,这是为什么啊?难道是我的程序上的问题?

搜索更多相关主题的帖子: 随机数 
2006-11-07 20:34
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
不会吧,可能是你代码上的错误哦.

飘过~~
2006-11-07 21:19
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 

我把代码贴出来 大家看看哪里有错误!
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();
}
while(true);
}

public void run2()
{

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

public void run3()
{

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

public void run4()
{

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

public void run5()
{

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

public void run6()
{

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

private void button3_Click(object sender, System.EventArgs e)
{
thread1.Abort();
thread2.Abort();
thread3.Abort();
thread4.Abort();
thread5.Abort();
thread6.Abort();
Application.Exit();
}

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);
}
}
}
}


2006-11-07 21:25
从小到大
Rank: 1
等 级:新手上路
威 望:2
帖 子:200
专家分:0
注 册:2006-6-24
收藏
得分:0 
自己调试一下不就清楚了

2006-11-08 16:16
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
因為你在用其他線程操作ui

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-11-08 16:32
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
我把跨线程使用控件的属性值该了,可以按照03的使用。
但是

我试过了,在2005上面启动一geThread,可以看到随即变幻的数据,但是启动两个以后就死了。。

从program上面看,似乎每个随机数都是一个单独的thread控制,为什么会死掉呢?

2006-11-08 16:44
CrazyWeed0907
Rank: 2
等 级:新手上路
威 望:5
帖 子:1385
专家分:0
注 册:2006-5-30
收藏
得分:0 
用委托 就OK了

“十步杀一人,千里不留行。事了拂衣去,深藏身与名。”
2006-11-08 16:46
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
可以解释一下为什么界面会死吗?

照理说每个随机数新开一个thread 不会影响主线程的运转吧。。。。。。。。。。。

我一直是这么理解的。。。。。。

不对吗?

2006-11-08 16:57
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
看下cpu是不是100%?

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-11-08 16:58
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 
以下是引用chenjin145在2006-11-8 16:32:27的发言:
因為你在用其他線程操作ui

能说得明白一些吗?该怎么改啊??


2006-11-08 22:11
快速回复:有关随机数死机的问题
数据加载中...
 
   



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

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