0

This is how I create a thread that does domething reps times:

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

The thread class:

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.");
    }
}

(I create a new class to store some variables since I can't pass these in ThreadStart().)

Now I would like to have a simple visualisation of the process, let's say printing the current repetition in a text field on a form. How would I use the i from the loop to do that?

Gerals
  • 251
  • 1
  • 5
  • 8

2 Answers2

0

you can do it with a backgroundworker, it´s one of the easiest threads :-) below i have post you a sample that i have createt to teach some friends the use of the backgroundworker ;-)

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

jwillmer
  • 3,570
  • 5
  • 37
  • 73
0

In the simplest form you proved a callback in the Nuker class

public Nuker(string target, int reps, Action reportCallback){..}

In the loop you just call reportCallback(i);

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

with

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

But, probably you want to use the BackgroundWorker that has build in methods for reporting progress on the UI thread. Just check the examples on MSDN.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108