繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> 评论及其它 >> 给枯燥的 .Net 命令行控制台程序来点儿心跳 (关于退格键[\b]使用的奇技淫巧,见笑)

给枯燥的 .Net 命令行控制台程序来点儿心跳 (关于退格键[\b]使用的奇技淫巧,见笑)

2005-01-09 20:59:50  作者:playyuer  来源:互联网  浏览次数:20  文字大小:【】【】【
简介:/*给枯燥的 .Net 命令行控制台程序来点儿心跳(关于退格[\b]的使用)雕虫小技 见笑Java 同理*/ public class Class1{ public static bool b; static void Main(string[] args) {  Syste...

/*

给枯燥的 .Net 命令行控制台程序来点儿心跳(关于退格[\b]的使用)

雕虫小技 见笑

Java 同理

*/

public class Class1

{

public static bool b;

static void Main(string[] args)

{

System.Console.WriteLine("测试1: 直接等待 n = 199 次循环");

Wait(199);

System.Console.WriteLine("\n\n测试2: 等待结束条件: b == true");

// 主程序 开始

b = false;

new System.Threading.Thread(new System.Threading.ThreadStart(DoWait)).Start(); //监视线程: 显示滚动计数器

//以下是耗时的主程序

System.Threading.Thread.Sleep(5 * 1000); //主程序耗时 5 秒

b = true; //主程序 结束

System.Console.WriteLine("\n主程序耗时 5 秒");

}

private static void Wait(int Count)

{

System.Console.Write("在进行第 ");

string bs = ""; //用于记录上次的位数

for (int i = 0; i < Count + 1; i++)

{

System.Threading.Thread.Sleep(10); // 10/1000 second

System.Console.Write(bs + "\b\b\b" + i + " 次," + System.DateTime.Now);

bs = new string('\b',Digits(i) + 19 + 1); //19 为日期时间字符串长度, 1 是 ","

}

}

private static void DoWait()

{

Wait(ref b); //由委托调用的真正函数

}

private static void Wait(ref bool Flag) //Flag 可在其他线程改

{

System.Console.Write("在进行第 ");

int i = 0;

string bs = "";

while (!Flag)

{

//System.Threading.Thread.Sleep(1000); // 1 second

System.Console.Write(bs + "\b\b\b" + i + " 次," + System.DateTime.Now);

bs = new string('\b',Digits(i) + 19 + 1); //19 为日期时间字符串长度, 1 是 ","

i ++;

}

}

static int Digits(int n) //数字所占位数

{

n = System.Math.Abs(n) ;

n = n/10;

int i = 1;

while (n > 0)

{

n = n / 10;

i++;

}

return i;

}

}

责任编辑:admin
相关文章