Najlepszy sposób na wyświetlenie formularza postępu, gdy metoda wykonuje kod?
Mam metodę ładowania WinForm, która zajmuje dużo czasu, aby zebrać dane do wyświetlenia użytkownikowi.
Wyświetlam formularz z dużą czcionką ze słowem „Ładowanie” podczas wykonywania tej metody.
Jednak czasami ten błąd pojawia się i formularz postępu „Ładowanie” nie zamyka się, a ostatecznie moja cała aplikacja po prostu wyjdzie:
Błąd podczas tworzenia uchwytu okna. at System.Windows.Forms.NativeWindow.CreateHandle (CreateParams cp)
Czy jest lepszy sposób wyświetlania mojego formularza postępu / ładowania podczas wykonywania kodu w metodzie ładowania?
To jest mój kod:
//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)
{
}