none
請問執行跨執行緒時候的執行緒suspend RRS feed

  • 問題

  • 我是用個執行緒讓progressbar去不斷的累加,想要用一個button2去暫停他

    可是遇到跨執行緒的問題,當我執行suspend的時候他顯示執行緒沒有執行所以無法暫停

    請問要如何才能正確的暫停呢??以下是我的程式碼
    using System;
    using System.Threading;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
     
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public delegate void mydelegate();
     
            ThreadStart mythreadstart1;
            
            Thread mythread1;
        
     
            public Form1()
            {
                InitializeComponent();
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                start();
            }
     
            private void start()
            {
     
                 mythreadstart1 = new ThreadStart(ddkbefore);
            
                 mythread1 = new Thread(mythreadstart1);
                
                mythread1.Start();
            
                
     
            }
     
            private void ddkbefore()
            {
                this.Invoke(new mydelegate(ddk));
                
            }
     

            private void ddk()
            {
                for (int a = 1; a < 99; a++)
                {
                    
                    progressBar1.Value++;
                
     
                }
            }
     
        
     
            private void button2_Click(object sender, EventArgs e)
            {
                mythread1.Suspend();
            }
     
        }
    }
    2007年3月3日 上午 08:04