enter image description here I know this is not the first question about access UI thread but I didn't find the answer to solve my problem.
While calling sample method it will display like the message box with abort button.If i click the abort button it will be not hit the abortbutton click event.Here that alertbox UI is freezing.So we can't hit that.
If i remove the dispatcher line it will be work fine.But i want that line for executing AddQueryIntoTable method. If that method will call after 100 milli seconds it will retrive data perfectly.
So i will set 100 ms as sleep mode(Commented line) it will be work fine.But i think this is not the perfect solution for this.
Thanks in advance.
public void SampleMethod()
{
CancelSupportedBackgroundWorker backGroundWorker = new CancelSupportedBackgroundWorker { WorkerSupportsCancellation = true };
CancellationTokenSource source = new CancellationTokenSource();
AlertBox alertBox = new AlertBox
{
WaitingText = "Loading Indicator",
WaitingHeaderText = "It is Loading",
};
alertBox.IsBusy = true;
alertBox.AbortButton.Click += (obje, arg) =>
{
MessageBox.Show("Hit");
};
backGroundWorker.DoWork += (obj, e) =>
{
App.Current.Dispatcher.Invoke(DispatcherPriority.SystemIdle, new Action(
delegate ()
{
try
{
if (info == null)
{
//Thread.sleep(100);
arg.result = AddqueryintoTable((CancellationToken)e.Argument);
if (backGroundWorker.CancellationPending)
{
e.Cancel = true;
return;
}
}
}
catch (ThreadAbortException)
{
Dispatcher.Invoke(() =>
{
alertBox.IsBusy = false;
}, System.Windows.Threading.DispatcherPriority.Background);
e.Cancel = true;
}
}));
};
backGroundWorker.RunWorkerCompleted += (obj, arg) =>
{
if (arg.Cancelled)
{
alertBox.IsBusy = false;
return;
}
alertBox.IsBusy = false;
if (arg != null && arg.Result != null)
{
SetReport(arg.Result);
}
};
backGroundWorker.RunWorkerAsync(source.Token);
}