www.pudn.com > CS_threadTest_20110820B.rar > Form1.cs, change:2011-08-20,size:2833b


// this program created by bill.zhang, in 8/20,2011; 
// the program just for test the delegrate function as c&c++ callback function; 
// and the program for the task management as the mini OS; 
 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 
 
 
namespace CS_threadTest 
{ 
    public partial class Form1 : Form 
    { 
        static Form1 Form1app = new Form1(); 
        Thread ThreadApp; 
        public delegate void ControlHandler(); 
        static int counter = 0; 
        static int breakstyle = 0; 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
        public void mmtest() 
        { 
            for (; ; ) 
            { 
                if (breakstyle == 0) 
                { 
                    System.Threading.Thread.Sleep(1000); 
                     
                    counter++; 
                    //Form1app.show(); 
                    try 
                    { 
                        this.Invoke(new ControlHandler(show)); 
                    } 
                    finally 
                    { 
                    } 
                } 
                else 
                { 
                    break; 
                } 
            } 
        } 
        public static void dspshowtest(Form1 dspForm1) 
        { 
            dspForm1.textBox1.Text = System.DateTime.Now.ToString("G");// counter.ToString(); 
            dspForm1.Update(); 
        } 
        public void show() 
        { 
            textBox1.Text = System.DateTime.Now.ToString("G");// counter.ToString(); 
            Update(); 
        } 
        public void mytestshow() 
        { 
            ThreadApp = new Thread(mmtest); 
            ThreadApp.Start(); 
        } 
        private void buttonCreateThead_Click(object sender, EventArgs e) 
        { 
            buttonCreateThead.Enabled = false; 
            breakstyle = 0; 
            Class1 Class1app = new Class1(); 
            Class1app.testthreadapp = new Class1.testthread(mytestshow); 
            Class1app.test(); 
        } 
 
        private void buttonBreak_Click(object sender, EventArgs e) 
        { 
            buttonCreateThead.Enabled = true; 
            breakstyle = 1; 
            dspshowtest(this); 
            //this.Invoke(new ControlHandler(thisdspshowtest)); 
        } 
 
        private void Form1_FormClosed(object sender, FormClosedEventArgs e) 
        { 
 
        } 
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
        { 
            if (breakstyle == 1) 
            { 
                //ThreadApp.Abort(); 
                System.Threading.Thread.Sleep(10000); 
            } 
        } 
    } 
}