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()..
}