Search code examples
c#dllc++-cliprogress-barbackgroundworker

C# progressbar update from c++ using managed c++ cli


I am new to C# and C++ mixed programming.

My C# application has a ProgressBar. On click of button, a c++ function is called through c++/CLI wrapper class DLL. It is a time consuming function. Hence I want to show the status update on my c# application's ProgressBar.

The UI should not be blocked, hence backgroundworker is used; in its DoWork, I invoke the DLL method. Please suggest with code.

[One clue I got is I need to use delegate, but I am new to C++/CLI hence unable to do.]

Code snippets will greatly help.


Solution

  • You need to have a managed method (either C# or C++/CLI) that updates the progress bar, which takes a percentage as a parameter. Have the managed method deal with whatever threading is necessary; you'll likely need to Invoke onto the UI thread.

    Convert that into a C++ function pointer, and pass that to your long-running C++ function. Have the long-running C++ function call the function pointer regularly.

    You could use a BackgroundWorker here, but I'd skip it, personally. You'd have to do a lot of plumbing to get everything working right, and if all you're interested in is reporting progress, it's easier to do that directly. Instead, I'd take the direct path, and just call your C++ method on a new Thread.