countrymeister
asked on
Responsive WPF UI
I ahve a WPF application not implementing any mVVM.
I have button click event, where I would like to show a progress Image, and then start a background thread to get some data.
In the background thread when the data has ben retrieved, I want to hide the Progress image.
private void Button_Click(object sender, RoutedEventArgs e)
{
//imgProgress.Visibility = System.Windows.Visibility. Visible;
Application.Current.Dispat cher.Invok e(System.W indows.Thr eading.Dis patcherPri ority.Norm al, new Action(
delegate()
{
imgProgress.Visibility = System.Windows.Visibility. Visible;
}));
StartBackgroundThread();
}
public void StartBackgroundThread()
{
BackgroundWorker bgWorker = new BackgroundWorker();
bgWorker.WorkerSupportsCan cellation = true;
bgWorker.DoWork += new DoWorkEventHandler(bgWorke r_DoWork);
bgWorker.RunWorkerComplete d += new RunWorkerCompletedEventHan dler(bgWor ker_RunWor kerComplet ed);
bgWorker.RunWorkerAsync();
}
void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgWork = (BackgroundWorker)sender;
if ((bgWork.CancellationPendi ng == true)) { e.Cancel = true; }
else
{
GetData(0;
}
}
void bgWorker_RunWorkerComplete d(object sender, RunWorkerCompletedEventArg s e)
{
imgProgress.Visibility = System.Windows.Visibility. Hidden;
}
THE UI freezes when the data retrival takes a long time, so does the Progress image.
I have button click event, where I would like to show a progress Image, and then start a background thread to get some data.
In the background thread when the data has ben retrieved, I want to hide the Progress image.
private void Button_Click(object sender, RoutedEventArgs e)
{
//imgProgress.Visibility = System.Windows.Visibility.
Application.Current.Dispat
delegate()
{
imgProgress.Visibility = System.Windows.Visibility.
}));
StartBackgroundThread();
}
public void StartBackgroundThread()
{
BackgroundWorker bgWorker = new BackgroundWorker();
bgWorker.WorkerSupportsCan
bgWorker.DoWork += new DoWorkEventHandler(bgWorke
bgWorker.RunWorkerComplete
bgWorker.RunWorkerAsync();
}
void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgWork = (BackgroundWorker)sender;
if ((bgWork.CancellationPendi
else
{
GetData(0;
}
}
void bgWorker_RunWorkerComplete
{
imgProgress.Visibility = System.Windows.Visibility.
}
THE UI freezes when the data retrival takes a long time, so does the Progress image.
Try making bgWorker a private class variable instead of local. This will prevent it from being GC, which might be causing the behavior you described.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Please close this question, no expert helped