I have a program that takes 10-20 seconds to start. I need to show a window with a progress bar when the program starts up. I know BackgroundWorker's are the correct way to do this but I unfortunately don't have the time to rework the whole gui using threads right now. Here is some code I'm trying but it is not working. Can anyone help..?
using System;
using System.Threading;
using System.Windows;
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Thread t = new Thread(ShowLoadingWindow);
t.SetApartmentState(ApartmentState.STA);
t.Priority = ThreadPriority.Highest;
t.Start();
DoSomeLongTask();
keepLooping = false;
}
bool keepLooping = true;
private void ShowLoadingWindow()
{
LoadingWindow lw = new LoadingWindow();
lw.Show();
while (keepLooping)
{
Thread.Sleep(1);
}
lw.Close();
}
private void DoSomeLongTask()
{
for (int i = 0; i < 20000; i++)
{
GC.Collect();
}
}
}
}
The loading window is just a bare window with a progress bar. Howcome this doesn't work?