How to make this a beautiful and correct the code?
I enable parent window.
Create thread progress bar.
perform code
and close the progress bar
var iCount = 3;
while(iCount>0)
{
if (myProgressBar!= null && myProgressBar.IsHandleCreated)
{
iCount = -1;
break;
}
iCount--;
Thread.Sleep(1000);
}
if (iCount != -1)
return false;//MessageBox
all function:
this.Enabled = false;
MyProgressBar myProgressBar = null;
try
{
var thread2 = new Thread(delegate()
{
myProgressBar = new MyProgressBar(this);
myProgressBar.ShowDialog();
});
thread2.Start();
Thread.Sleep(100);
var iCount = 3;
while(iCount>0)
{
if (myProgressBar!= null && myProgressBar.IsHandleCreated)
{
iCount = -1;
break;
}
iCount--;
Thread.Sleep(1000);
}
if (iCount != -1)
return false;//Mess开发者_StackOverflow中文版ageBox
for (int i = 0; i < list.Count; i++)
{
//more code
}
}
catch (Exception e)
{
}
finally
{
myProgressBar.CloseMe();
this.Enabled = true;
}
Have you seen the BackgroundWorker
class?
It will simplify your logic greatly.
精彩评论