zoukankan      html  css  js  c++  java
  • 雕虫小技: 给枯燥的 .Net 控制台程序(字符界面)来点儿心跳 (关于退格 '\b' 的使用)

    /*
    我写的 Blog 随笔的样例代码绝大部分都是,控制台 Console 程序
    里面有一些关于"字符界面"的小技巧,都挺简单的,再次提炼出来,废话不多说了!
    都是雕虫小技,见笑:
    《给枯燥的 .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 = ""//用于记录上次的位数
            string s = "";

            
    for (int i = 0; i < Count + 1; i++)
            
    {
                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 是 ","
            }

        }


        
    private static void DoWait()
        
    {
            Wait(
    ref b); //由委托调用的真正函数
        }


        
    private static void Wait(ref bool Flag) //Flag 可在其他线程改
        {
            System.Console.Write(
    "正在进行第    ");
            
    int i = 0;
            
    string bs = "";
            
    string s = "";
            
    while (!Flag)
            
    {
                
    //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 ++;
            }

        }


        
    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;
        }

    }

  • 相关阅读:
    sqlalchemy 查询姿势总结
    sqlalchemy 常用总结
    rsyslog 移植与配置方案介绍
    软件设计随想录
    C语言面对对象设计模式汇编
    关于linux kernel slab内存管理的一点思考
    linux PMBus总线及设备驱动分析
    Linux x86_64 APIC中断路由机制分析
    单板控制领域模型设计与实现
    Linux mips64r2 PCI中断路由机制分析
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/163498.html
Copyright © 2011-2022 走看看