与另一个线程进行通信

时间:2011-03-06 21:41:00

标签: c# multithreading gtk

这就是我创建一个执行reps次内容的线程的方法:

protected virtual void RedButtonClicked(object sender, System.EventArgs e)
{
    Nuker n = new Nuker(target, reps);
    bombThread = new Thread(new ThreadStart(n.nuke));
    bombThread.Start();
}

线程类:

public class Nuker
{
    private string target;
    private int reps;
    //...
    public void nuke()
    {
        for(int i=0; i<reps; ++i)
        {
            ICBM.nuke(target);
            Thread.Sleep(5500);
        }
    System.Console.WriteLine("Done.");
    }
}

(我创建了一个新类来存储一些变量,因为我无法在ThreadStart()中传递这些变量。)

现在我希望对该过程进行简单的可视化,假设在表单上的文本字段中打印当前重复。我如何使用循环中的i来做到这一点?

2 个答案:

答案 0 :(得分:0)

你可以用背景工作者来做,这是最简单的线程之一:-) 下面我给你发了一个样本,我已经创建了一些朋友,教他们使用背景工作者; - )

private BackgroundWorker bw = new BackgroundWorker();

public Form1()
{
    InitializeComponent();
    bw.WorkerReportsProgress = true;
    bw.WorkerSupportsCancellation = true;
    bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
}

public void buttonStart_Click(object sender, EventArgs e)
{
    if (bw.IsBusy != true)
        bw.RunWorkerAsync(12); //Start
}

public int Pils(int i)
{
    Thread.Sleep(2000);
    bw.ReportProgress(70, "In the middle of the work..");
    Thread.Sleep(2000);
    bw.ReportProgress(90, "Returning the result..");
    Thread.Sleep(2000);
    return (2 * i);
}

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    bw.ReportProgress(20, "Waiting for cancel.."); 
    Thread.Sleep(2000);
    if ((bw.CancellationPending == true))
        e.Cancel = true;
    else
    {
        bw.ReportProgress(50, "Starting process..");
        e.Result = Pils((int)e.Argument);
    }
}

private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    bw.ReportProgress(100, "Work done.."); 
    if ((e.Cancelled == true))
         textBox1.Text = "Canceled!";
    else if (e.Error != null)
         textBox1.Text = ("Error: " + e.Error.Message);
    else textBox1.Text = e.Result.ToString();
}

private void buttonCancel_Click(object sender, EventArgs e)
{
    if (bw.WorkerSupportsCancellation == true)
        bw.CancelAsync();
}

private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    listBox1.Items.Add((e.ProgressPercentage.ToString() + "%") + " - " + e.UserState as String);
}

url to the blogpost:link

答案 1 :(得分:0)

在最简单的形式中,您证明了Nuker

中的回调

public Nuker(字符串目标,int reps,Action reportCallback){..}

在循环中,您只需拨打reportCallback(i);

Nuker n = new Nuker(target, reps, ReportMethod);

private void ReportMethod(int currentIdx)
{
    if (InvokeRequired) // Invoke if UI update
    ...
}

但是,您可能希望使用已构建方法的BackgroundWorker来报告UI线程上的进度。只需查看MSDN上的示例。