Головна‎ > ‎C# WinForms‎ > ‎Більше‎ > ‎

Фоновий процес

public Form1() { 
     InitializeComponent(); 
     backgroundWorker1.DoWork += backgroundWorker1_DoWork;     
     backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;  
     backgroundWorker1.WorkerReportsProgress = true; 

private void button1_Click(object sender, EventArgs e) 
     backgroundWorker1.RunWorkerAsync(); 

private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) 
     for (int i = 0; i < 100; i++) { 
          Thread.Sleep(1000); 
          backgroundWorker1.ReportProgress(i); 
     } 


private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) 
     progressBar1.Value = e.ProgressPercentage; 
}




        int i = 0;

        void AAA() {
            for (i = 0; i < 1000000; i++)
            {
                Thread.Sleep(2000);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {         
            new Thread(AAA) { IsBackground = true }.Start();         
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Text = i.ToString();
        }