最近在實作進度條更新
在網上也看了一些範例
但輪到自己實作時卻卡住了
環境VS2005
執行緒應該有進入更新 但卻像卡在for迴圈的地方
然後才進行更新
實在想不出為什麼了
https://www.dropbox.com/s/pu8tyqt2otm723u/WindowsApplication1.zip?dl=0
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 WindowsApplication1
{
public partial class Form1 : Form
{
int m_all = 0;
int max = 300;
Thread t = null;
public Form1()
{
InitializeComponent();
t = new Thread(new ThreadStart(threadwork));
}
private void button1_Click(object sender, EventArgs e)
{
m_all = 0;
t.Start();
//無腦迴圈
for (m_all = 0; m_all < max; m_all++)
{
for (int j = 0; j < 1000; j++)
for (int k = 0; k < 10000; k++)
int m = j + k;
}
}
delegate void SetUpdate();
void threadwork()
{
if (progressBar1.InvokeRequired)
{
SetUpdate d = new SetUpdate(threadwork);
progressBar1.Invoke(d, new object[] { });
}
else
{
while (true)
{
double value = 1.0 * Convert.ToDouble(m_all) / max * 100;
progressBar1.Value = Convert.ToInt32(value);
progressBar1.Refresh();
if (m_all >= max)
{
break;
}
}
}
}
}