简体   繁体   中英

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. The DLL is then imported into a C# program and the function is called.

The C++ function processes files which can take a while so i was looking for a way to track the progress of the 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:

C++ function:

void process(float *status)
{
    //process file code
    //............//

    *status = processed / total * 100;
}

And then in the C# code:

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.

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);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM