none
Avoid race condition in this program RRS feed

  • Question

  • 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?

        
    Wednesday, February 22, 2012 4:16 PM

Answers

  • Is your C++ managed or unmanaged? If it is managed, you could pass a AutoResetEvent^ as an argument to StartMain (http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx), than wait on the event.

    If not, you can make a C++/CLR wrapper that uses a win32 event (http://msdn.microsoft.com/en-us/library/windows/desktop/ms686915%28v=vs.85%29.aspx). The C++/CLR function can wait for the handle and then return only after the handle has been fired.

    Another alternative would be getting the HANDLE from msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx

    Good luck

    • Proposed as answer by Paul Zhou Monday, February 27, 2012 2:01 AM
    • Marked as answer by Paul Zhou Thursday, March 1, 2012 2:21 AM
    Wednesday, February 22, 2012 4:45 PM

All replies

  • Is your C++ managed or unmanaged? If it is managed, you could pass a AutoResetEvent^ as an argument to StartMain (http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx), than wait on the event.

    If not, you can make a C++/CLR wrapper that uses a win32 event (http://msdn.microsoft.com/en-us/library/windows/desktop/ms686915%28v=vs.85%29.aspx). The C++/CLR function can wait for the handle and then return only after the handle has been fired.

    Another alternative would be getting the HANDLE from msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx

    Good luck

    • Proposed as answer by Paul Zhou Monday, February 27, 2012 2:01 AM
    • Marked as answer by Paul Zhou Thursday, March 1, 2012 2:21 AM
    Wednesday, February 22, 2012 4:45 PM
  • If StartMain() starts a new thread to do the work, then simply make it return the handle to that thread.  Then use the Windows API function WaitForSingleObject() using that handle.  It is the simplest, I think.  It all depends on the final behavior that you are looking for.

    Jose R. MCP

    Wednesday, February 22, 2012 4:56 PM