我找到了simple progressbar dialog class并尝试在我的应用中使用它。
除非单击取消和进度条对话框调用worker.CancelAsync();
,否则一切似乎都没问题。发生这种情况时,我的流程仍在等待结束。
很难理解所以我粘贴了我的代码。问题是:当progressbardialog调用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);
}
}