I wrote a function that uses DLL for its purposes. the class where this function is present has callback for dll for monitoring this function state and a field that I must use in another class.
While I am calling this function in target class I must contineusly get this field value (for progress bar) or pass “Halt” in this callback but it only possible when function finishes execute/
How could I do this?
There is My code. I need to cancel engine via progressbar dialog Cancel
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);
}
}
//the ProgressDialog is from http://www.hardcodet.net/2008/01/wpf-progress-dialog
I think you mentioned it, but here is the callback pattern:
This also shows how a callback can be used to interrupt/cancel the long operation