| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 872 人关注过本帖
标题:[求助]快来帮帮忙啊(搞定)
只看楼主 加入收藏
tanghuawei
Rank: 4
来 自:美丽的湖南
等 级:业余侠客
威 望:3
帖 子:531
专家分:220
注 册:2006-3-16
结帖率:100%
收藏
 问题点数:0 回复次数:16 
[求助]快来帮帮忙啊(搞定)
kJBx7IaR.rar (24.89 KB) [求助]怎样灵活控制线程?


兄弟们,还在这里纠缠你们真不好意思,只是我实在是无助了,请你们帮我看看。
上面的程序最大的问题就是线程的控制问题,我想实现的效果是当我选择Action 1时,点击开始它就显示Action 1所设置的效果,如果我马上又选择Action 2点开始,它马上又显示Action 2的效果(或者是点暂停之后再选择Action 2,之后再点开始),我做了好久还是搞不定,请各位帮帮忙,感谢!

[此贴子已经被作者于2006-8-31 8:34:36编辑过]

搜索更多相关主题的帖子: 最大的 
2006-08-29 17:18
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
[CODE]

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

namespace waTest
{
/// <summary>
/// Form1 腔晡猁佽隴﹝
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ComboBox comboBox1;

private Thread m_thread;

public Form1()
{
//
// Windows 敦极扢數盓厥垀斛剒腔
//
InitializeComponent();

//
// TODO: 婓 InitializeComponent 覃蚚綴氝樓睡凳婖滲杅測鎢
//
}

/// <summary>
/// 燴垀衄淏婓妏蚚腔訧埭﹝
/// </summary>
protected override void Dispose( bool disposing )
{
if(m_thread!=null)
{
if((int)m_thread.ThreadState == 96||(int)m_thread.ThreadState==97||(int)m_thread.ThreadState==6||(int)m_thread.ThreadState==7)
m_thread.Resume();
m_thread.Abort();
}
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 敦极扢數汜傖腔測鎢
/// <summary>
/// 扢數盓厥垀剒腔源楊 - 祥猁妏蚚測鎢晤憮党蜊
/// 森源楊腔囀﹝
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.btnClose = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
this.btnStart = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(8, 104);
this.btnClose.Name = "btnClose";
this.btnClose.TabIndex = 6;
this.btnClose.Text = "Close";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(8, 56);
this.btnStop.Name = "btnStop";
this.btnStop.TabIndex = 7;
this.btnStop.Text = "Stop";
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(8, 8);
this.btnStart.Name = "btnStart";
this.btnStart.TabIndex = 5;
this.btnStart.Text = "Start";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"Action 1",
"Action 2"});
this.comboBox1.Location = new System.Drawing.Point(8, 136);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(80, 20);
this.comboBox1.TabIndex = 8;
this.comboBox1.Text = "Action 1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(519, 476);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.btnStart);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 茼蚚最唗腔翋諳萸﹝
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void StartDrawing()
{
while(true)
{
this.comboBox1.Text = this.comboBox1.SelectedItem.ToString();
int width = 10;
int high = 10;

Graphics g = this.CreateGraphics();
System.Drawing.Drawing2D.LinearGradientBrush myBrush = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle,Color.Gray, Color.Gray, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
System.Drawing.Drawing2D.LinearGradientBrush myBrush2 = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle,Color.Red, Color.Red, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
if(this.comboBox1.Text.Equals("Action 1"))
{
for(int y = 10;y<470;y=y+10)
{
for(int x = 170; x<310;x = x+10)
{
g.FillEllipse(myBrush,x,y,width,high);
g.FillEllipse(myBrush,y,x,width,high);
System.Threading.Thread.Sleep(1);
}
}
//System.Threading.Thread.CurrentThread.Join(1000);
Thread.Sleep(1000);
}
else
{
for(int y = 460;y>=10;y=y-10)
{
for(int x =300; x>160;x = x-10)
{
g.FillEllipse(myBrush2,x,y,width,high);
g.FillEllipse(myBrush2,y,x,width,high);
System.Threading.Thread.Sleep(1);
}
}
//System.Threading.Thread.CurrentThread.Join(1000);
Thread.Sleep(1000);
}
}
}

private void btnStart_Click(object sender, System.EventArgs e)
{
if(m_thread == null)
{
m_thread = new Thread(new ThreadStart(StartDrawing));
m_thread.Start();
}
else
{
if((int)m_thread.ThreadState == 96||(int)m_thread.ThreadState==97||(int)m_thread.ThreadState==6||(int)m_thread.ThreadState==7)
m_thread.Resume();
}

}

private void btnStop_Click(object sender, System.EventArgs e)
{
if(m_thread != null)
{
//if((int)m_thread.ThreadState == 96||(int)m_thread.ThreadState==97||(int)m_thread.ThreadState==6||(int)m_thread.ThreadState==7)
m_thread.Suspend();
}

}

private void btnClose_Click(object sender, System.EventArgs e)
{
Close();
}

}
}

[/CODE]

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-29 17:37
mylover624
Rank: 1
来 自:乖乖的心中
等 级:新手上路
帖 子:868
专家分:0
注 册:2006-7-6
收藏
得分:0 
好像是一样的效果吧,我这里没什么变化。

一个天才顶不上十个笨蛋!
书山有路勤为径,学海无涯友相伴。
我的E-mail:mylover624@.cn
2006-08-29 17:45
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
在出錯上面
ThreadState能保存兩個枚舉值
真TNND

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-29 17:46
tanghuawei
Rank: 4
来 自:美丽的湖南
等 级:业余侠客
威 望:3
帖 子:531
专家分:220
注 册:2006-3-16
收藏
得分:0 

现在就是一个很难处理的问题出现在这里:当我点开始时(此线程被调用Start()启动),线程的状态转为:Running,当我点暂停时(调用Sleep()或Suspend方法),线程的状态将转为 Sleep()或者SuspendRequested(线程挂起),但是有可能线程不会被立即挂起,要等到线程达到安全点时它才挂起,即会出现你点开始他不会立即执行另一个线程,它会继续当前线程,直到它到达安全点,这样它就执行另一线程了。Thread.Resume方法正好可以继续当前线程,点暂停时,用这个方法可以继续,这些我们都明白了。
最需要解决的就是,当我点暂停时,使线程状态挂起,且能够立即挂起,如我调用Thread.Resume方法,线程可以继续,如果我不想Thread.Resume,我想进行另外一个线程,又能使他马上跳为线程的最初状态,即当一个Thread实例刚创建时,它的ThreadState是Unstarted,这样我点开始它就可以开始一个新的线程了。
不知道说明白没?我解决不了了,希望大家给点意见,谢谢大家!


汽车尾气检测网络系统QQ:357766186__MSN:MSNTHW19850316@
2006-08-30 08:46
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
if((int)m_thread.ThreadState == 96||(int)m_thread.ThreadState==97||(int)m_thread.ThreadState==6||(int)m_thread.ThreadState==7)

這樣判斷還不行?

我這調適都沒出問題

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-30 10:11
tanghuawei
Rank: 4
来 自:美丽的湖南
等 级:业余侠客
威 望:3
帖 子:531
专家分:220
注 册:2006-3-16
收藏
得分:0 

不行啊,因为你选择另外一个Action时,它不能立即变化,我最终的效果是实现点暂停后,我选择另外一个Action再点开始它就显示另一个Action的效果了,而不要继续显示完前面的效果在显示另外一个Action,呵呵,好罗嗦吧?


汽车尾气检测网络系统QQ:357766186__MSN:MSNTHW19850316@
2006-08-30 10:16
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 

那你就只有abort線程 從新開始

不然代碼塊還是按上次停止的地方開始執行


[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-30 10:18
tanghuawei
Rank: 4
来 自:美丽的湖南
等 级:业余侠客
威 望:3
帖 子:531
专家分:220
注 册:2006-3-16
收藏
得分:0 
把abort写在stop事件里面?我试过,不行哦,我都郁闷死了!

汽车尾气检测网络系统QQ:357766186__MSN:MSNTHW19850316@
2006-08-30 10:30
猫不吃鱼
Rank: 1
等 级:新手上路
威 望:1
帖 子:95
专家分:0
注 册:2006-8-20
收藏
得分:0 
8楼,,,小金金也在啊,,,,不错不错

2006-08-30 11:42
快速回复:[求助]快来帮帮忙啊(搞定)
数据加载中...
 
   



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

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