zoukankan      html  css  js  c++  java
  • 数字猜大小(经典多线程和自动生成控件的小例子)和确定程序的运行时间方法

    一:上图

    二:相关代码

    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;
    
    namespace gessNumGame
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            System.Threading.Thread G_thread;//定义线程
            Random G_random = new Random();//定义随机数对象
    
            int G_int_num;//定义变量用于存放存机数
    
            private void btn_start_Click(object sender, EventArgs e)
            {
                RemoveControl();//清理上一局
                btn_start.Enabled = false;//停用开始按钮
                int p_int_x=10;//第一个按钮横坐标
                int p_int_y=60;//第一个按钮纵坐标
                for (int i = 0; i < 100; i++)//生成100个按钮
                {
                    Button btn = new Button();//生成一个按钮
                    btn.Name = (i + 1).ToString();//设定名字
                    btn.Text = (i + 1).ToString();//设定文本显示
                    btn.Width = 35;//按钮宽度
                    btn.Height = 35;//按钮高度
                    btn.Location = new Point(p_int_x, p_int_y);//按钮位置
                    btn.Click += new EventHandler(bt_Click);//定义button按钮的事件
    
                    p_int_x += 36;//下一个按钮横坐标
                    if ((i+1) % 10 == 0)//下一行
                    {
                        p_int_x = 10;//下一行第一个按钮横坐标
                        p_int_y += 36;//下一行第一个按钮纵坐标
                    }
                    Controls.Add(btn);//将button按钮放入窗体控件集合中
    
                    G_thread = new System.Threading.Thread
                        (//新建一个计时和产生随机数的线程
                        delegate()//使用匿名方法将代码块传递为委托参数
                        {
                            int P_int_count = 0;//初始化计数器
                            while (true)//开始无限循环
                            {
                                P_int_count = //计数器累加
                                    ++P_int_count > 100000000 ? 0 : P_int_count;//设置最大数值
                                this.Invoke(//将代码交给主线程执行
                                    (MethodInvoker)delegate//使用匿名方法
                                    {
                                        lbl_time.Text = //窗体中显示计数
                                            P_int_count.ToString();
                                    });
                                System.Threading.Thread.Sleep(1000);//线程睡眠1秒
                            }
                        }
                    );
                    G_thread.IsBackground = true;//设置线程为后台线程
                    G_thread.Start();//开始执行线程
                    G_int_num = G_random.Next(1, 100);//生成随机数
                }
            }
            void bt_Click(object sender, EventArgs e)
            {
                Control P_control = sender as Control;//将sender(存放的是指向实体对象的指针)转换为control类型对象
                if (int.Parse(P_control.Name) > G_int_num)
                {
                    P_control.BackColor = Color.Red;//设置按钮背景为红色
                    P_control.Enabled = false;//设置按钮停用
                    P_control.Text = "";//更改按钮文本
                }
                if (int.Parse(P_control.Name) < G_int_num)
                {
                    P_control.BackColor = Color.Red;//设置按钮背景为红色
                    P_control.Enabled = false;//设置按钮停用
                    P_control.Text = "";//更改按钮文本
                }
                if (int.Parse(P_control.Name) == G_int_num)
                {
                    G_thread.Abort();//终止计数线程
                    MessageBox.Show(string.Format(//显示游戏信息
                        "恭喜你猜对了!共猜了{0}次 用时{1}秒",
                        GetCount(), lbl_time.Text), "恭喜!");
                    btn_start.Enabled = true;//启用开始按钮
                }
            }
            /// <summary>
            /// 用于查找窗体中Enable属性为False控件的数量
            /// 用于计算玩家有多少次没有猜中
            /// </summary>
            /// <returns>返回没有猜中数量</returns>
            string GetCount()
            {
                int P_int_temp = 0;//初始化计数器
                foreach (Control c in Controls)//遍历控件集合
                {
                    if (!c.Enabled)
                        P_int_temp++;//计数器累加
                }
                return P_int_temp.ToString();//返回计数器信息
            }
            /// <summary>
            /// 用于清空窗体中动态生成的按钮
            /// </summary>
            void RemoveControl()
            {
                for (int i = 0; i < 100; i++)//开始遍历100个按钮
                {
                    if (Controls.ContainsKey(//窗体中是否有此按钮
                        (i + 1).ToString()))
                    {
                        for (int j = 0; j < Controls.Count; j++)//遍历窗体控件集合
                        {
                            if (Controls[j].Name == //是否查找到按钮
                                (i + 1).ToString())
                            {
                                Controls.RemoveAt(j);//删除指定按钮
                                break;
                            }
                        }
                    }
                }
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                Environment.Exit(0);//强行关闭窗体,应用程序即强制退出。
            }
        }
    }

    三:另一种确定程序的运行时间方法

    截图

    代码

    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;
    
    namespace DisplayRunTime
    {
        public partial class Frm_Main : Form
        {
            public Frm_Main()
            {
                InitializeComponent();
            }
    
            private DateTime G_DateTime;//声明时间字段
    
            private void Frm_Main_Load(object sender, EventArgs e)
            {
                G_DateTime = DateTime.Now;//得到系统当前时间
                Thread P_th = new Thread(//创建线程
                    () =>//使用Lambda表达式
                    {
                        while (true)//无限循环
                        {
                            TimeSpan P_TimeSpan =//得到时间差
                                DateTime.Now - G_DateTime;
                            Invoke(//调用窗体线程
                                (MethodInvoker)(() =>//使用Lambda表达式
                                {
                                    tssLabel_Time.Text =//显示程序启动时间
                                        string.Format(
                                        "系统已经运行: {0}天{1}小时{2}分{3}秒",
                                        P_TimeSpan.Days, P_TimeSpan.Hours, 
                                        P_TimeSpan.Minutes, P_TimeSpan.Seconds);
                                }));
                            Thread.Sleep(1000);//线程挂起1秒钟
                        }
                    });
                P_th.IsBackground = true;//设置为后台线程
                P_th.Start();//开始执行线程
            }
        }
    }

    四:程序下载

    加载如下地址即可下载:

    http://files.cnblogs.com/hongmaju/NumGame.zip

  • 相关阅读:
    Xcode7下模拟器输入文本无法显示系统键盘的解决办法
    Mac系统下开启和关闭隐藏文件的方法
    iOS常用第三方开源框架和优秀开发者博客等
    UILabel 的一个蛋疼问题
    学习进度条
    学习进度条
    四则运算2
    学习进度条
    第一次被要求连接数据库的课堂测试
    课后作业06多态与接口
  • 原文地址:https://www.cnblogs.com/hongmaju/p/3734018.html
Copyright © 2011-2022 走看看