[英]How can i track the progress of a function in C++ and C#
I have a function written in C++ and compiled into a DLL.我有一个用 C++ 编写的 function 并编译成 DLL。 The DLL is then imported into a C# program and the function is called.
然后将 DLL 导入 C# 程序并调用 function。
The C++ function processes files which can take a while so i was looking for a way to track the progress of the function. C++ function 处理文件可能需要一段时间,所以我正在寻找一种方法来跟踪 function 的进度。
I was thinking i could pass a pointer to a float to the function, run the function in a thread from C# and then print the value of the float:我在想我可以将一个指向浮点数的指针传递给 function,在 C# 的线程中运行 function,然后打印浮点数的值:
C++ function: C++ function:
void process(float *status)
{
//process file code
//............//
*status = processed / total * 100;
}
And then in the C# code:然后在 C# 代码中:
static extern void process(float *status);
int Main()
{
float status = 0;
thread.start(process(&status));
while(status != 100)
{
Console.Write(status);
}
}
The only problem with this is that i cannot pass a pointer to a thread as an argument.唯一的问题是我不能将指向线程的指针作为参数传递。
How can i achieve this??我怎样才能做到这一点?
If you own the C++ code, you can replace the status
variable with a callback.如果您拥有 C++ 代码,则可以将
status
变量替换为回调。
If not, you can just make the local variable for status a global one you like this:如果没有,您可以将状态的局部变量设置为您喜欢的全局变量:
volatile float status;
void ThreadWorker()
{
process(ref status);
}
int Main()
{
Thread t = new Thread(ThreadWorker);
t.Start();
while(status != 100)
{
Console.Write(status);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.