Progressidialog in C#

Asked

Viewed 290 times

8

I’m trying to create a form similar to Progressdialog on android, in C#..

The idea would be that this would 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 gif of "wait".

//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

inserir a descrição da imagem aqui

Form the way it should be:

inserir a descrição da imagem aqui

I know there’s a possibility of using the backgroundworker or do the processing in another thread, but I’d like to do it this way so I don’t "worry" in the places I’m going to use.

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

Editing at the suggestion of Henrique

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 on the call..

 frmTeste fl = new frmTeste();
 fl.ShowTest();
 for(long i = 0; i < long.MaxValue; i++)
 {
    Application.DoEvents(); //tentei coloca um DoEvents()..
 }
  • Your idea is not cool. You want to open a form that shows infinite progress, this?

  • Infinity because of the gif? Yes... but after what I have to do I close the form.

1 answer

5


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 the progressiDialog by calling the methods InvokeProgressDialog and CancelProgressDialog from any context that has access to the myForm class. Note that two contexts are being displayed:

  • Context of the progressiDialog;
  • Context from which the method is called InvokeProgressDialog.

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

  • Thank you for the answer, I’m going to test it this afternoon, but I’m going to give +1 now.

  • I tried it this way, the ShowDialog is called within the Task, but the form doesn’t even open, I just tried Show and nothing happened either :/

  • How strange, can show the code snippet ?

  • I edited the question @Henrique inserting the code.

  • @Marcogiovanni guy, here normal wheel... have you debugged with breakpoints? No error appears? What’s in the frmWaitingProgress constructor ?

  • Yes I’ve debugged, no error appears, it calls the new ProgressDialog().ShowDialog(), but during the for, it keeps processing and does not open the screen, you tried to put a for until long.MaxValue?, in the builder has nothing more... I will make a separate project to test.

  • Yes, I did it with infinity just below the method call InvokeProgressDialog. Your form does not even open that is strange.. If you want I can post all the code for you see, but it is very simple. In my progressiDialog I have a picturebox that has a gif, simple like this.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.