在 C++ 上使用 C# 异步方法

Jun*_*une 5 c# c++ dllimport async-await visual-studio-2017

我在 C# 中创建了一个类库(.net 框架)项目,我想在我的 C++ 客户端项目中使用该 DLL。

我已经在 MSDN 和在线上查看了有关如何制作它的说明。所以我可以在我的 C++ 项目中看到 C# 代码的结果。除了 C# 异步方法。

那里发生了运行时错误。

这是我的 C# dll 代码的一部分。

namespace NethereumDLLTest
{
    [Guid("1C3D53B3-54C5-452B-9505-3F58ABEB35BA")]
    public interface INethereumDLL
    {
        void Run();
    }

    [Guid("C22875F1-B74D-4C99-B446-ED705CE86D3B")]
    public class Class1 : INethereumDLL
    {

        //there are some methods here.

        public void Run()
        {
            Console.WriteLine("NethereumDLLTEST");

            RunGeth();

            Task task = new Task(ActionMethod);
            task.Start();
            Console.WriteLine("Main Logic");

            GetAccountBalance().Wait();
            Console.ReadLine();
        }

        static async Task GetAccountBalance()
        {
            for (int i = 10; i > 0; i--)
            {
                Thread.Sleep(1000);
            }
            var web3 = new Web3("http://localhost:8545");
            var balance = await  web3.Eth.GetBalance.SendRequestAsync("0xb3a11b62596b8b6313dddc30e9b11607c8f9fb38");

            Console.WriteLine($"Balance in Wei: {balance.Value}");
            var etherAmount = Web3.Convert.FromWei(balance.Value);
            Console.WriteLine($"Balance in Ether: {etherAmount}");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这里是 cpp 客户端的代码。

#import "C:\Users\june\source\repos\NethereumDLLTest\NethereumDLLTest\bin\Debug\NethereumDLLTest.tlb" 

using namespace std;
using namespace NethereumDLLTest;

int main()
{

    CoInitialize(NULL);

    {
        INethereumDLLPtr ptr(__uuidof(Class1));
        ptr->Run();
    }
    CoUninitialize();

}
Run Code Online (Sandbox Code Playgroud)

我可以在“GetAccountBalance”函数上方找到所有日志消息,但在那之后它似乎没有被执行。

不能在 C++ 中使用 C# 异步函数?或者我做错了什么?

运行时错误捕获图像