C#常用简单线程实例

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 NetWorkCreeper
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            ThreadStart tstart = new ThreadStart(AddItem);
            Thread thread = new Thread(tstart);
            thread.Start();
        }


        public void AddItem()
        {
            for (int index = 0; index < 100000; index++)
            {

      //lbxBox是ListBox控件
                lbxBox.Items.Add(string.Format("Item {0}", index));
            }
        }

        private void btnLook_Click(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format("ListBox中一共有{0}项{1}", this.lbxBox.Items.Count, Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "." + DateTime.Now.Millisecond)));
        }
    }
}

注意,在调试的时候不要直接执行;要选择“调试”-》“开始执行(不调试)”;这一点一定要谨记,否则会运行报错。

posted @ 2013-08-12 16:25  永不言败,自信相伴  阅读(351)  评论(0编辑  收藏  举报