Лучший способ отобразить форму прогресса, пока метод выполняет код?
У меня есть метод загрузки WinForm, который занимает много времени, чтобы собрать некоторые данные для отображения пользователю.
Я показываю форму с крупным шрифтом со словом «Загрузка» во время выполнения этого метода.
Однако иногда появляется эта ошибка, и форма выполнения «Загрузка» не закрывается, и в конечном итоге все приложение просто завершается:
Ошибка создания дескриптора окна. в System.Windows.Forms.NativeWindow.CreateHandle (cp CreateParams)
Есть ли лучший способ отобразить мою форму прогресса / загрузки, когда я выполняю код в методе загрузки?
Это мой код:
//I launch a thread here so that way the Progress_form will display to the user
//while the Load method is still executing code. I can not use .ShowDialog here
//or it will block.
//Progress_form displays the "Loading" form
Thread t = new Thread(new ThreadStart(Progress_form));
t.SetApartmentState(System.Threading.ApartmentState.STA);
t.IsBackground = true;
t.Start();
//This is where all the code is that gets the data from the database. This could
//take upwards of 20+ seconds.
//Now I want to close the form because I am at the end of the Load Method
try
{
//abort the Progress_form thread (close the form)
t.Abort();
//t.Interrupt();
}
catch (Exception)
{
}