Winforms 模式进度对话框-如何传入委托?

Winforms 模式进度对话框-如何传入委托?,winforms,.net-4.0,Winforms,.net 4.0,我有一个带有进度条的基本表单,希望传入一个类似这样的委托: ProgressDialog.ShowAndExecute(delegate); 我不知道如何将代理连接到进度消息 void ShowAndExecute() { // Handle form disabling and whatnot... thread = new Thread(new ThreadStart(ExecuteCommand)); while (thread.IsAlive) {

我有一个带有进度条的基本表单,希望传入一个类似这样的委托:

ProgressDialog.ShowAndExecute(delegate);
我不知道如何将代理连接到进度消息

void ShowAndExecute()
{
    // Handle form disabling and whatnot...

    thread = new Thread(new ThreadStart(ExecuteCommand));

    while (thread.IsAlive)
    {
        Application.DoEvents();
        Thread.Sleep(100);
    }
}

// Example of the method I would like to pass in
void ExecuteCommand()
{
    for (int i = 0; i < 10; i++) Thread.Sleep(1000);
}
void ShowAndExecute()
{
//处理表单禁用等等。。。
线程=新线程(新线程开始(ExecuteCommand));
while(thread.IsAlive)
{
Application.DoEvents();
睡眠(100);
}
}
//我想传递的方法的示例
void ExecuteCommand()
{
对于(inti=0;i<10;i++)线程,Sleep(1000);
}
我考虑创建一个命令应该实现的接口。他们可以在更新发生时触发事件,但我如何让调用线程知道它已被触发


如何处理通过事件报告进度并基于这些事件采取行动(移动进度条)的代理的传入?这是一个静态对话框(便于在整个应用程序中调用)

只有
ExecuteCommand
知道其当前进度。因此,某些接口或类必须传递给
ExecuteCommand
方法。
ExecuteCommand
将设置进度,然后由侦听器的实现决定如何处理进度。通常,侦听器会检查这是否是一个正在进行的重大更改**,如果是,它将
启动一个调用以更新进度条

**-如果您在短时间内处理数千个项目,那么您希望避免调用过多的
BeginInvoke
,否则会锁定UI线程

void ExecuteCommand(IThreadController tc)
{
    for (int i = 0; i < 10; i++) {
        Thread.Sleep(1000);
        tc.setProgress("processing ...", (i+1), 10);
    }
}
void ExecuteCommand(IThreadController tc)
{
对于(int i=0;i<10;i++){
睡眠(1000);
tc.setProgress(“处理…”,(i+1),10);
}
}

帮我找到了需要去的地方。谢谢