ProgressDialog in C #

7

I'm trying to create a form similar to ProgressDialog in android, in C # ..

The idea would be for this to happen:

//criar o controle na thread principal
frmWaitingProgress  fl = new frmWaitingProgress(this);
fl.Show(this);

//fazer todo o processamento na thread principal
for(long i = 0; i < long.MaxValue; i++)
{

}

//depois de fazer o que tiver que fazer simplismente fecha o form
fl.Close();

And in my frmWaitingProgress , another thread would be responsible for updating the "wait" gif.

//Então para isso sobescrevi o OnLoad do método e criei minha thread
protected override void OnLoad(EventArgs e)
{
    Thread trd = new Thread(new ThreadStart(ThreadUpdate));
    trd.Start();
}

private void ThreadUpdate()
{
    while (this.IsDisposed == false)
    {
        if (this.pictureBox1.InvokeRequired)
        {
            this.pictureBox1.BeginInvoke((MethodInvoker)delegate ()
            { 
                this.pictureBox1.Refresh();
                this.pictureBox1.Invalidate();
                this.pictureBox1.Update();
            });
        }
        else
        {
            pictureBox1.Refresh();
            pictureBox1.Invalidate();
            pictureBox1.Update();
        }
        Application.DoEvents();
    }
}

But the form is not updated

Formthewayitshouldbe:

I know there is a possibility of using backgroundworker or doing the processing in another thread, but I would like to do it this way so as not to worry about the places I will use.

Does anyone have any suggestions on how I can do this, or can you tell me if this is possible?

Editing with Henry's suggestion

public class frmTeste 
{
    Task task;
    Thread bgThread;
    public void ShowTest()
    {
        task = new Task(() => {
            bgThread = Thread.CurrentThread;
            new frmWaitingProgress().ShowDialog();
            //new frmWaitingProgress().Show();
        });

        task.Start();
    }
}

And in the call ..

 frmTeste fl = new frmTeste();
 fl.ShowTest();
 for(long i = 0; i < long.MaxValue; i++)
 {
    Application.DoEvents(); //tentei coloca um DoEvents()..
 }
    
asked by anonymous 08.12.2016 / 13:04

1 answer

4

I think you will have to use asynchronous programming, this example below works without much work:

public class myForm : Form {
    Task task;
    Thread bgThread;

    public myForm() {

    }

    // Inicia a task
    public void InvokeProgressDialog() {

        // Inicialização e setup do seu progressDialog
        task = new Task(() => {

            // registra a bgThread
            bgThread = Thread.CurrentThread;

            // chama o progressDialog (form)
            new ProgressDialog().ShowDialog();

        });

        task.Start();
    }

    // aborta a thread (a qual a task está)
    public void CancelProgressDialog() {
        bgThread.Abort();
    }
}

You can invoke / cancel progressDialog by calling the InvokeProgressDialog and CancelProgressDialog methods of any context that has access to the myForm class. Note that two contexts are showing:

  • Context of progressDialog;
  • Context where the InvokeProgressDialog method is called.
  

Note that only a bgThread is registered, if you need it, you can expand this concept by registering more threads.

    
08.12.2016 / 15:03