/**/
/*
我写的 Blog 随笔的样例代码绝大部分都是,控制台 Console 程序
里面有一些关于"字符界面"的小技巧,都挺简单的,再次提炼出来,废话不多说了!
都是雕虫小技,见笑:
《给枯燥的 .Net 控制台程序来点儿心跳 (关于退格 '\b' 的使用)》
Java 同理
*/
![None.gif](/Images/OutliningIndicators/None.gif)
public
class
Class1
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public static bool b; //结束条件
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Console.WriteLine("测试1: 直接等待 n = 199 次循环");
Wait(199);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
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 秒");
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private static void Wait(int Count)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Console.Write("在进行第 ");
string bs = ""; //用于记录上次的位数
string s = "";
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < Count + 1; i++)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
s = System.DateTime.Now.ToString();
System.Threading.Thread.Sleep(10); // 10/1000 second
System.Console.Write(bs + "\b\b\b" + i + " 次," + s);
bs = new string('\b', Digits(i) + s.Length + 1); //19 为日期时间字符串长度, 1 是 ","
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private static void DoWait()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Wait(ref b); //由委托调用的真正函数
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private static void Wait(ref bool Flag) //Flag 可在其他线程改
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Console.Write("正在进行第 ");
int i = 0;
string bs = "";
string s = "";
while (!Flag)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//System.Threading.Thread.Sleep(1000); // 1 second
s = System.DateTime.Now.ToString();
System.Console.Write(bs + "\b\b\b" + i + " 次," + s);
bs = new string('\b', Digits(i) + s.Length + 1); //19 为日期时间字符串长度, 1 是 ","
i ++;
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
static int Digits(int n) //数字所占位数
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
n = System.Math.Abs(n);
n = n/10;
int i = 1;
while (n > 0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
n = n/10;
i++;
}
return i;
}
}
转载于:https://www.cnblogs.com/Microshaoft/archive/2005/05/27/163498.html