如何在c++本地DLL中检查BackgroundWorker CancelationPending
How to check BackgroundWorker CancelationPending in C++ native DLL
大家好,我在c++中创建了一个DLL,我在c#中使用它,如:
[DllImport("IMDistortions.dll", CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern void ProcessBarrelDistortion(byte* bytes, int stride, int width, int height, byte pixelSize, double a, double b, double c, double d);
一切都很好,但这个函数我在后台工作DoWork函数调用,我想停止函数使用代码:
if(cancel)return;
在我的c++ DLL中,取消是一个指针到工作CancelationPending,但CancelationPending是一个属性,所以我不能得到指针到它:
bool *cancel=&worker.CancelationPending;
然后将其作为参数发送到我的函数中。有人能帮我解决这个问题吗?也在寻找报告进度,但不是那么多。
您可以使用回调函数(类似的解决方案可以应用于'report progress')
在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);
}
}
相关文章:
- valgrind-hellgrind与泄漏检查的结果不同
- C++模板来检查友元函数的存在
- 检查输入是否不是整数或数字
- 试图让变量检查数组中的某些内容
- 检查值是否在集合p1和p2中,但不在p3中
- C++概念:如何使用'concept'检查模板化结构的属性?
- 概念TS检查忽略私有访问修饰符
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- 如何在C++中检查2D数组中负值的输入验证
- C++:正在检查LinkedList中的回文-递归方法-错误
- 使用for循环检查数组中的重复项
- 如何检查一个c++字符串中有多少相同的字符/数字
- 检查不带转换的扫描格式
- 如何检查线程是否锁定
- 清除前检查矢量
- 如何处理来自核心指南检查器的关于gsl::at的静态分析警告
- C++LDAP检查用户是否是特定组的成员
- 检查TCHAR数组输入是否为带符号整数C++
- 用概念检查属性的类型