| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1390 人关注过本帖
标题:[求助]走过路过千万不要错过
只看楼主 加入收藏
tanghuawei
Rank: 4
来 自:美丽的湖南
等 级:业余侠客
威 望:3
帖 子:531
专家分:220
注 册:2006-3-16
结帖率:100%
收藏
 问题点数:0 回复次数:23 
[求助]走过路过千万不要错过

我下面的这段代码是一个十字屏的一种播放效果,但是,当我一运行程序后,点击Start按纽,它就会一直循环下去,
中间那Stop、Close按纽都点击不了,而且要强行关闭,请各位帮忙看看,小弟不甚感激!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

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;

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()
{
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.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);
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(520, 477);
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 timer1_Tick(object sender, System.EventArgs e)
{
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);

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.CurrentThread.Join(1000);

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.CurrentThread.Join(1000);
}

private void btnStart_Click(object sender, System.EventArgs e)
{
this.timer1.Start();
}

private void btnStop_Click(object sender, System.EventArgs e)
{
this.timer1.Stop();
}

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

搜索更多相关主题的帖子: 运行程序 public 而且 
2006-08-23 09:56
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 
    private void btnStop_Click(object sender, System.EventArgs e)
        {
            this.timer1 .Enabled =false;
            //this.timer1.Stop();
        }

这样就可以了

此号自封于2006年11月30日
2006-08-23 10:10
tanghuawei
Rank: 4
来 自:美丽的湖南
等 级:业余侠客
威 望:3
帖 子:531
专家分:220
注 册:2006-3-16
收藏
得分:0 
谢谢你,但是这个都无所谓,你不信你运行程序试试,你点了Start之后其他的按纽根本没办法点了。

汽车尾气检测网络系统QQ:357766186__MSN:MSNTHW19850316@
2006-08-23 10:13
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
System.Threading.Thread.CurrentThread.Join(1000);
這個的意思是讓上面的循環只執行1秒?

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-23 10:16
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 
不会吧
我点了start之后
点其他2个一个暂停
一个关闭都挺好用的啊
难道我的人品太差?

此号自封于2006年11月30日
2006-08-23 10:21
tanghuawei
Rank: 4
来 自:美丽的湖南
等 级:业余侠客
威 望:3
帖 子:531
专家分:220
注 册:2006-3-16
收藏
得分:0 

就是跟Sleep差不多,也就是停顿一秒然后执行下一句。
这样之后就变成了一个死的了,运行之后什么都不能干。
还请你多多指教!


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

timer1設置為幾秒

join msdn上說到:
封鎖呼叫執行緒直到執行緒終止或已超過指定的時間為止,但仍繼續執行標準的 COM 與 SendMessage 幫浦作業。

誰有簡體版的 看下怎麼說的


[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-23 10:28
tanghuawei
Rank: 4
来 自:美丽的湖南
等 级:业余侠客
威 望:3
帖 子:531
专家分:220
注 册:2006-3-16
收藏
得分:0 
我timer1设置的是1ms啊,这样点按纽的反应速度就要快。
你可不可以帮我把代码执行一下,你看看会更明白我是什么错误,好吗?
多谢,我去看看msdn!

汽车尾气检测网络系统QQ:357766186__MSN:MSNTHW19850316@
2006-08-23 10:33
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
理論上來講你上面是個死循環
timer 1ms
執行2000ms+ 這個2000ms你是動不了的

1ms
又執行
執行2000ms+

所以主thread一直在執行 其他的沒辦法響印了

解決的話就只有用多線程 或把timer設置得比2000ms大 但是執行的2000ms你還是動不了

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-23 10:46
tanghuawei
Rank: 4
来 自:美丽的湖南
等 级:业余侠客
威 望:3
帖 子:531
专家分:220
注 册:2006-3-16
收藏
得分:0 
使用多线程就可以解决问题吗?
如果不使用线程又可以达到预计的效果吗?

汽车尾气检测网络系统QQ:357766186__MSN:MSNTHW19850316@
2006-08-23 10:50
快速回复:[求助]走过路过千万不要错过
数据加载中...
 
   



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

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