コールバック関数を使用できます (同様のソリューションを「レポートの進行状況」に適用できます)。
あなたのC++ .dllで
//#include <iostream>
typedef bool ( *CancellationPending)();
extern "C" __declspec(dllexport) void ProcessBarrelDistortion
(
unsigned char* bytes,
int stride,
int width,
int height,
unsigned char pixelSize,
double a,
double b,
double c,
double d,
CancellationPending cancellationPending //callback
)
{
bool cancellationPending = cancellationPending();
if (cancellationPending)
{
return;
}
//std::cout << cancellationPending;
}
C# プロジェクトで
public delegate bool CancellationPending();
[DllImport("YourDll.dll", CallingConvention = CallingConvention.StdCall)]
public static unsafe extern void ProcessBarrelDistortion
(byte* bytes,
int stride,
int width,
int height,
byte pixelSize,
double a,
double b,
double c,
double d,
CancellationPending cancellationPending);
static void Main(string[] args)
{
var bg = new BackgroundWorker {WorkerSupportsCancellation = true};
bg.DoWork += (sender, eventArgs) =>
{
Console.WriteLine("Background work....");
Thread.Sleep(10000);
};
bg.RunWorkerAsync();
unsafe
{
ProcessBarrelDistortion(null, 0, 0, 0, 0, 0, 0, 0, 0,
() => bg.CancellationPending);
}
bg.CancelAsync();
unsafe
{
ProcessBarrelDistortion(null, 0, 0, 0, 0, 0, 0, 0, 0,
() => bg.CancellationPending);
}
}