解决方案 »
- 求教一个特殊的XML获取值问题
- 在oracle中把表数据存放到datetable中,并把combox的数据源指定为datetable
- 请大家帮忙!!!VS2008 总是空白页 为什么?
- 学习C#,从B/S入门还是从C/S入门?
- 问个问题 这样实现有没有BUG?
- 异步socket如何控制连接超时?
- 想用C#写一个程序监控软件?有些小问题不会?
- 关于对本网站系统将来需要开发的功能的设想简介
- Windows Form里面的DataGrid支持双击事件吗?
- 请教:vs.net中编写web应用程序,运行后,怎么许多控件如:button、checkbox等不在窗口显示?
- C# datagridview 多层表头 横向滚动 重绘时显示黑色
- 大家说我这个excel表结构该如何完成用户的需求?
阻塞主线程,窗体要等到休眠结束后才响应,这个时候就出现卡死的现象。新启一个线程,对新线程Thread.Sleep,不会阻塞主线程,也就是不会出现窗体卡死的现象。
循环,sleep设置短一些
发现了一个残酷的现实,在类库中,并没有 DoEvents 方法
using system.windows.forms;
//创建StopNtime类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace 程序运行暂停器
{
class StopNtime
{
public StopNtime()
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//,跨线程调用控件必加上去
}
private int stopTime = 0;//暂停的时间
ThreadStart myStart;
Thread TheStop;
public readonly object MyLockWord = new object();
//
public void stopWay(int stopTime)
{
this.stopTime = stopTime;
myStart = new ThreadStart(this .ToStop );
TheStop = new Thread(myStart );
TheStop.Start();
}
private void ToStop()
{
lock (MyLockWord)
{
Thread.Sleep(this .stopTime );
Thread.CurrentThread.Abort();
}
}
}
}//主窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading ;
using System.Collections;
namespace 程序运行暂停器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "";
Thread f = new Thread(f1 );
f.Start();
}
string MyWord = "问题:1+1=? ...\n答案是:2 ...";
private void f1()
{
StopNtime mmm = new StopNtime();
foreach (char m in MyWord)
{
lock (mmm.MyLockWord )
{
label1.Text += m.ToString();
}
mmm.stopWay(300);
}
}
}
}
把原来调用Thread.Sleep的地方调用这个就可以了
public static void Delay(int mm)
{
DateTime current = DateTime.Now;
while (current.AddMilliseconds(mm) > DateTime.Now)
{
Application.DoEvents();
}
return;
}