I have found a simple progressbar dialog class and am trying to use it in my app.
All seems to be ok except when I click Cancel and Progress bar dialog calls worker.CancelAsync();
. When this happens, my process is still waiting for the end.
It is difficult to understand so I am pasting my code. The question is: how to completely terminate 'engine' function running when progressbardialog calls worker.CancelAsync();
class LibWrap //containing the LONG process with dll
{
public bool cancelThisBoringProcess;
public int currentPecentage;
public delegate void pfnCallback(int progress, out bool cancel);
public void showProgress(int progress, out bool cancel)
{
cancel = cancelThisBoringProcess;
currentPecentage = progress;
}
[DllImport("Lib.DLL", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern byte* bufferOp(byte* data,pfnCallback del);
public unsafe BitmapFrame engine(BitmapFrame incomingFrame)
{
//...
fixed (byte* inBuf = incoming)
{
var callback = new pfnCallback(showProgress);
byte* outBuf = bufferOp(inBuf, callback);//this is DLL function with callback to get out percentage //and pass cancel
GC.KeepAlive(callback);
//....
}
}
}
class Main
{
void OnClick(object sender, RoutedEventArgs e)
{
ProgressDialog dlg = new ProgressDialog("");
LibWrap lwrap = new LibWrap();
DoWorkEventHandler handler = delegate { BitmapFrame bf = lwrap.engine(img)); };
dlg.AutoIncrementInterval = 100;
dlg.IsCancellingEnabled = true;
dlg.Owner = Application.Current.MainWindow;
dlg.RunWorkerThread(handler);
}
}