简体   繁体   English

如何创建异步功能?

[英]How can i create an async function?

I have several member functions inside my C++ class which i packaged them as a dll and tried to used them in a C# application. 我的C ++类中有几个成员函数,这些成员函数将它们打包为dll,并试图在C#应用程序中使用它们。
I tried to create two async functions by simply executing them using threads and then detaching them() so that they wouldn't block the caller thread till they end. 我试图通过使用线程简单地执行两个异步函数,然后分离它们来创建两个异步函数,以使它们直到结束都不会阻塞调用者线程。 in C++ based application whenever i use threads to call functions this way, they work, but when i tried to call one of my async functions from c#, my app either crashed or hangs!! 在基于C ++的应用程序中,每当我使用线程以这种方式调用函数时,它们都起作用,但是当我尝试从c#调用我的异步函数之一时,我的应用程序崩溃或挂起!
these are my so called async functions!: 这些是我所谓的异步函数!:

void xGramManipulator::CreateMonoGramAsync()
{
    thread t(&xGramManipulator::ReadMonoGram, this);
    t.detach();
}

void xGramManipulator::CreateBiGramAsync()
{
    thread t = thread(&xGramManipulator::ReadBiGram, this);
    t.detach();
}

And this is the wrapper function in c which resides in a dll : 这是驻留在dll中的c中的包装函数:

//header
CDLL_API void CreateMonoGramAsync(void);
//cpp
CDLL_API void CreateMonoGramAsync(void)
{
    xG.CreateMonoGramAsync();
}

and this is the c# application calling that function : 这是调用该函数的c#应用程序:

 private void btnTest_Click(object sender, EventArgs e)
 {
     try
     {
        CreateBiGramAsync();
        CreateMonoGramAsync();
     }
     catch (Exception)
     {
     }

}

What should i do to have a truly async and non blocking member functions in my class? 我应该怎么做才能在课堂上拥有真正的异步和非阻塞成员函数? What am i missing here? 我在这里想念什么?

My guess is that you are not specifying the calling convention when importing these into C# - by default C# will use stdcall while C++ will export as cdecl . 我的猜测是,在将这些导入到C#中时没有指定调用约定-默认情况下,C#将使用stdcall而C ++将导出为cdecl You would have to : 您将必须:

[DllImport("CDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void CreateMonoGramAsync();

alternatively, you can of course specify returntype __stdcall when declaring your methods in C++. 或者,您当然可以在用C ++声明方法时指定returntype __stdcall I'm also assuming you've otherwise declared the function properly (with extern "C" __declspec(dllexport) , etc.) 我还假设您已经正确地声明了该函数(带有extern "C" __declspec(dllexport)等)

If you are using .NET 4.5, you could use the Task class in order to achieve an asynchronous method call. 如果使用的是.NET 4.5,则可以使用Task类来实现异步方法调用。

Consider the following example: 考虑以下示例:

    static void Foo()
    {
        Thread.Sleep(2000);
        Console.WriteLine("Foo called asynchronously!");
    }

    static void Main(string[] args)
    {
        var bar = Task.Factory.StartNew(() => { Foo(); });

        Console.WriteLine("Called synchronously!");

        Console.ReadLine();
    }

Whenever this code executes, an asynchronous call to Foo() will be started, which waits two seconds, and then writes Foo called asynchronously! 每当执行此代码时,将启动对Foo()的异步调用,该调用将等待两秒钟,然后以Foo called asynchronously!写入Foo called asynchronously! to the console. 到控制台。 The other Console.WriteLine call is synchronous, and is run right after the asynchronous call has been started. 另一个Console.WriteLine调用是同步的,并且在异步调用启动后立即运行。

The output would be: 输出为:

Called synchronously!
Foo called asynchronously!

Since the caller's thread isn't blocked by the call to Foo() . 由于调用方的线程不受Foo()的调用的阻塞。

You could do the same with your code, and you could even remove the threading you currently have in your methods to make them asynchronous. 您可以对代码执行相同的操作,甚至可以删除方法中当前具有的线程以使其异步。 Reason being is that even though they create their own thread, the .NET runtime is still going to wait for those functions to return. 原因是,即使它们创建了自己的线程,.NET运行时仍将等待这些函数返回。 You would be better off calling them like so: 您最好这样打给他们:

Task.Factory.StartNew(CreateBiGramAsync());

That way they'll actually be called asynchronously from .NET itself, and they won't block the caller thread. 这样,它们实际上可以从.NET本身被异步调用,并且它们不会阻塞调用者线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将带有ref参数的函数转换为异步函数? - How can I convert a function with a ref paramter to an async function? 我该如何创建一个控制台应用程序,以实现异步/等待 - How can I create a console application that utlilitize async/await 如何创建用于处理异步数据传输的包装器? - How can I create a wrapper for handling Async data transfer? 如何从异步函数返回字符串? - How can I return a string from an async function? 如何将输出值绑定到异步Azure功能? - How can I bind output values to my async Azure Function? 如何使用 Moq 为异步函数抛出异常 - How can I throw Exception for async function using Moq 如何在没有停止的情况下在另一个异步方法中创建调用异步方法 - How can I create call an async method inside another async method without it stalling 有没有一种方法可以在创建时启动异步任务,但不能在创建时启动异步任务 Xamarin - Is there a way I can start async task on create but not async oncreate Xamarin 如何在函数中创建 IDataParameter 并返回它? - How can I create IDataParameter in a function and return it? 如何将从异步函数引发的异常传播到正在等待的异步void函数? - How can I propagate an exception thrown from an asynchronous function to an awaiting async void function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM