[問題] 執行緒與進度條更新
最近在實作進度條更新
在網上也看了一些範例
但輪到自己實作時卻卡住了
環境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;
}
}
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.20.98.14
※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1435121678.A.892.html
→
06/25 23:55, , 1F
06/25 23:55, 1F
→
06/25 23:56, , 2F
06/25 23:56, 2F
→
06/25 23:56, , 3F
06/25 23:56, 3F
→
06/25 23:57, , 4F
06/25 23:57, 4F
→
06/27 17:03, , 5F
06/27 17:03, 5F
→
06/27 17:04, , 6F
06/27 17:04, 6F
→
06/27 17:05, , 7F
06/27 17:05, 7F
→
06/27 17:06, , 8F
06/27 17:06, 8F
→
06/27 17:07, , 9F
06/27 17:07, 9F
→
06/27 17:08, , 10F
06/27 17:08, 10F
→
06/27 17:09, , 11F
06/27 17:09, 11F
→
06/27 17:10, , 12F
06/27 17:10, 12F
→
07/17 17:53, , 13F
07/17 17:53, 13F