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
Form the way it should be:
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?
– Jéf Bueno
Infinity because of the gif? Yes... but after what I have to do I close the form.
– Marco Giovanni