3 回答

TA貢獻(xiàn)1828條經(jīng)驗 獲得超3個贊
excelHelper.InsertNewRows();在我看來,你的問題是,工作線程和進(jìn)度條線程之間沒有任何通信。兩個線程都在運(yùn)行,沒有任何有關(guān)另一個線程及其進(jìn)度的信息。
您可以重寫后臺線程,因此它能夠?qū)俜直茸鳛閰?shù),該參數(shù)在您調(diào)用線程時顯示。
例如偽代碼:
public void progressbarThread(int percentage)?
{
? ? // Invoke the percentage to your progressbar, then terminate this thread
}
public void InsertNewRows()?
{
? ? // Do something...
? ? // 10%
? ? Thread backgroundThread = new Thread(new ThreadStart(() => progressBarThread(10)));
? ? backgroundThread.Start();
? ? // Do something...
? ? // 50%
? ? Thread backgroundThread = new Thread(new ThreadStart(() => progressBarThread(50)));
? ? backgroundThread.Start();
? ? // etc. etc.
}

TA貢獻(xiàn)1878條經(jīng)驗 獲得超4個贊
肯定有資源可以幫助您解決這個問題,但是我花了大約 2 天的時間來解決這個問題,所以我將在這里幫助您并減輕您的頭痛。進(jìn)度條本身位于主 UI 線程下(就像表單中的任何對象一樣),需要由主線程處理。無論你想做什么,都可以通過這樣的線程來處理
String a, b; a = txtUsername.Text; b = txtPassword.Password; Thread Login = new Thread(() => CompleteLogin(a, b)); InversePbVisibility(); Login.Start();
InversePbVisibility() 方法將被您正在執(zhí)行的使進(jìn)度條對用戶可見的任何操作所替換。附帶說明一下,在聲明的線程上運(yùn)行的任何方法都只能傳遞變量,而不能傳遞主線程控制下的任何內(nèi)容。

TA貢獻(xiàn)1820條經(jīng)驗 獲得超9個贊
I use this form class: -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Yournamespace
{
public partial class frmProgressBar : Form
{
public Action Worker { get; set; }
System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
public const long MINIMUM_DISPLAY_TIME = 300; // Minimum time to display the progress bar is 300 milliseconds
public frmProgressBar(Action worker)
{
InitializeComponent();
if(worker == null)
{
throw new ArgumentNullException();
}
Worker = worker;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Task.Factory.StartNew(Worker).ContinueWith(t => { this.Close(); }, TaskScheduler.FromCurrentSynchronizationContext());
}
protected override void OnClosed(EventArgs e)
{
// the code that you want to measure comes here
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
if (elapsedMs < MINIMUM_DISPLAY_TIME) //ensure progress bar is displayed for at least 100 milliseconds, otherwise it just looks like the parent main form glitches.
{
long lDelay = MINIMUM_DISPLAY_TIME - elapsedMs;
Thread.Sleep((int)lDelay);
}
}
}
}
The form design looks like this : -
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/n4XqU.jpg
And I call it like this: -
using (Yournamespace.frmProgressBar frm = new Yournamespace.frmProgressBar(Yourprocess))
{
frm.ShowDialog(this);
}
Yourprocess is the code that you want to execute that is causing the delay.
The main problem with this implementation is Yourprocess cannot return a value or take parameters. I am sure the code can be changed to accomodate this but I did not have time so I use globals to pass in data and to see results(shame on me).
This is not my code, although I have modified it, came from a you tube video - Wait Form Dialog.
編輯。我忘了說我將表單設(shè)置為 100% 不透明度,因此每當(dāng)我使用它時,進(jìn)度條似乎都會漂浮在我的 winform 上方。
- 3 回答
- 0 關(guān)注
- 146 瀏覽
添加回答
舉報