Thread,Invoke

來源:互聯網
上載者:User
代碼


///////////////////////////////調用類



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Text;
using System.Windows.Forms;

namespace WinFormAddDataLoading
{
partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
/// <summary>
/// 設定顯示資訊
/// </summary>
/// <param name="message"></param>
public void SetMessage(string message)
{ this.label1.Text = message; }
/// <summary>
/// 設定ProgressBar控制項的最大值
/// </summary>
/// <param name="count"></param>
public void SetMaxCount(int count)
{ this.progressBar1.Maximum = count; }
/// <summary>
/// 設定ProgressBar的value(進度)
/// </summary>
/// <param name="values"></param>
public void SetValue(int values)
{ this.progressBar1.Value = values; }

private void Form2_Load(object sender, EventArgs e)
{
///設定樣式
this.progressBar1.Style = ProgressBarStyle.Continuous;
}
/// <summary>
/// 關閉表單
/// </summary>
public void Closed()
{ this.Close(); }
}
public class LoadingClass
{
private Thread thread = null;
private Form2 f = null;
public LoadingClass()
{
f = new Form2();
thread = new Thread(new ThreadStart(delegate { f.ShowDialog(); }));
}
public void Show(string message, int max, int values)
{
f.Show();
f.SetMaxCount(max);
f.SetMessage(message);
f.SetValue(values);
f.Refresh();
}
public void ch()
{
f.Close();

try
{
thread.Abort();
}
catch { }
}
}
}




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 WinFormAddDataLoading
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private LoadingClass f = null;
private void button1_Click(object sender, EventArgs e)
{
f= new LoadingClass();
for (int i = 0; i < 100; i++)
{
f.Show("內容:" + i.ToString(), 100, i);
//this.Invoke(new MethodInvoker(delegate { f.Show("內容:" + i.ToString(), 100, i); }));
Thread.Sleep(10);
}
f.ch();
//this.button1.Enabled = false;
//Thread thread = new Thread(new ThreadStart(run));
//thread.Start();
}
private void run()
{

for (int i = 0; i < 100; i++)
{
this.Invoke(new MethodInvoker(delegate { f.Show("內容:" + i.ToString(), 100, i); }));
Thread.Sleep(10);
}
f.ch();
// this.button1.Enabled = true;
}
}
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.