我正在研究C#应用程序,并在这里面临一个奇怪的问题。我有一个使用C++构建的.dll文件,我必须从C#应用程序中的这个.dll文件中调用一些函数。在C#程序中避免这种竞态条件
考虑下面的示例代码:
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文件
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;
}
问题是,当我请从.dll中StartMain()
功能,它计算用于int
一些值和char
必须分配给变量(实际返回到C#应用程序)
但是,在StartMain()
之后,C#应用程序应该等待StartMain()
函数完成(在这种情况下, 3-4秒,并且dll本身会触发两个/三个其他进程),然后才能继续进行,否则,C#应用程序中的变量x
和sb
将具有空值/无意义的值。
如何在这种情况下达到相同的效果?
我没有看到问题。函数'StartMain'将工作,直到工作完成。工作完成后,C#应用程序将获得执行控制权并将继续调用'ReturnIntExp'。这里没有问题。 – Dialecticus 2012-02-22 16:18:27