3 回答
TA贡献1828条经验 获得超3个赞
excelHelper.InsertNewRows();在我看来,你的问题是,工作线程和进度条线程之间没有任何通信。两个线程都在运行,没有任何有关另一个线程及其进度的信息。
您可以重写后台线程,因此它能够将百分比作为参数,该参数在您调用线程时显示。
例如伪代码:
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贡献1878条经验 获得超4个赞
肯定有资源可以帮助您解决这个问题,但是我花了大约 2 天的时间来解决这个问题,所以我将在这里帮助您并减轻您的头痛。进度条本身位于主 UI 线程下(就像表单中的任何对象一样),需要由主线程处理。无论你想做什么,都可以通过这样的线程来处理
String a, b; a = txtUsername.Text; b = txtPassword.Password; Thread Login = new Thread(() => CompleteLogin(a, b)); InversePbVisibility(); Login.Start();
InversePbVisibility() 方法将被您正在执行的使进度条对用户可见的任何操作所替换。附带说明一下,在声明的线程上运行的任何方法都只能传递变量,而不能传递主线程控制下的任何内容。
TA贡献1820条经验 获得超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.
编辑。我忘了说我将表单设置为 100% 不透明度,因此每当我使用它时,进度条似乎都会漂浮在我的 winform 上方。
- 3 回答
- 0 关注
- 107 浏览
添加回答
举报