I have a form which take a few seconds to load. So therefore, I want to show a little form with the text 'Loading, please wait'. When the form is finished loading, the loading form must be closed.
So, I made a simple class which Shows a loading form in a thread:
public class ShowLoadingForm
{
Thread _thread;
public void Show()
{
try
{
_thread = new Thread(new ThreadStart(ShowForm));
_thread.SetApartmentState(ApartmentState.STA);
_thread.IsBackground = true;
_thread.Start();
}
catch (Exception ex)
{
ErrorMessages.UnknownError(true, ex);
}
}
private void ShowForm()
{
LoadingForm f = new LoadingForm();
f.TopMost = true;
f.ShowInTaskbar = true;
f.SetText(" Loading... ");
f.Show();
}
public void Close()
{
_thread.Abort();
}
}
In the main form I have:
_loadingForm = new ShowLoadingForm();
_loadingForm.Show();
BUT. After this piece of code, I do something on the main form: this.Opacity = 0;. At this point, I can see in the debugger that the thread is stopped working and a ThreadStateException
is thrown and the loading form is disappeared.
Why is this?