I am working on a C# application and facing a strange issue here. I have a .dll which has been built in C++, and I have to call some functions from this .dll from C# app.
Consider this sample code:
public partial class MainWindows: Window
{
public MainWindow()
{
InitializeComponent();
ConfigurationFunctions.StartMain(); //Start main is the DLL function in C++
int x = ConfigurationFunctions.ReturnIntExp();
StringBuilder sb = ConfigurationOptions.ReturnSomethingExp();
}
}
C++ .cpp file
EXPORT_API int xExp;
EXPORT_API char cExp;
EXPORT_API StartMain()
{
//Calculate `x` and `y` in this program values here and allocate to variables to be returned from function
xExp = x;
cExp = c;
}
EXPORT_API int ReturnIntExp()
{
return xExp;
}
EXPORT_API char ReturnSomethingExp()
{
return cExp;
}
The problem is when I run the StartMain() function from the .dll, it calculate some values for for int and char which have to be allocated to variables (that are actually returned to C# application)
But, after StartMain() the C# application should wait till the StartMain() function is complete (which in this case takes approx. 3-4 secs and the dll itself fires two/three other processes) and only then proceed further or else, the variables x and sb in C# application will have empty/meaningless values.
How can I achieve the same in this case?
Add a third exported variable
ReturnIsReady()that returnstrueif the other two methods have meaningful data, andfalseotherwise. Then simply have a loop with a sleep to check that value until it changes.A better option would be to implement a mutex or similar structure, but spin waiting should be sufficient for a single wait of that length at start up.
Alternatively, why is
StartMain()returning before filling in the values?