mikebai.com

  • Home
  • dev
  • DotNET
  • M365
  • 搞笑
  • 杂七杂八
  • FocusDict
個人BLOG
it developer
  1. Main page
  2. DotNET
  3. Main content

多线程例子

2012-01-17 98hotness 0likes 0comments

source from:http://www.codeproject.com/KB/cs/workerthread.aspx


后台处理类


    public class BackWorker
    {
        public delegate void DelegateAddString(String s);
        public delegate void DelegateThreadFinished();


        public DelegateAddString m_DelegateAddString;
        public DelegateThreadFinished m_DelegateThreadFinished;


        // Main thread sets this event to stop worker thread:
        public ManualResetEvent m_EventStopThread;


        // Worker thread sets this event when it is stopped:
        public ManualResetEvent m_EventThreadStopped;



        public BackWorker()
        {
            // initialize events
            m_EventStopThread = new ManualResetEvent(false);
            m_EventThreadStopped = new ManualResetEvent(false);
        }


        public void Start()
        {
            int i;
            String s;


            for (i = 1; i <= 101; i++)
            {
                // make step
                s = "Step number " + i.ToString() + " executed";


                Thread.Sleep(1000);


                // Make synchronous call to main form.
                // MainForm.AddString function runs in main thread.
                // To make asynchronous call use BeginInvoke
                //m_form.Invoke(m_form.m_DelegateAddString, new Object[] { s });
                this.m_DelegateAddString(s);


                // check if thread is cancelled
                if (m_EventStopThread.WaitOne(0, true))
                {
                    // clean-up operations may be placed here
                    // ...


                    // inform main thread that this thread stopped
                    m_EventThreadStopped.Set();


           &

Tag: Nothing
Last updated:2012-01-17

mikebai

This person is a lazy dog and has left nothing

Like
< Last article
Next article >

COPYRIGHT © 2025 mikebai.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang