是否有可能为一个.NET Windows服务中的I / O回调调用C#的等待不阻止? [英] Is it possible for an I/O callback within a .NET Windows Service that calls C#'s await to not block?

查看:101
本文介绍了是否有可能为一个.NET Windows服务中的I / O回调调用C#的等待不阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,在ASP.NET的辅助线程返回到池时伺机使用,而I / O发生在后台,这是伟大的可扩展性。

I know that in ASP.NET that the worker thread goes back to the pool when await is used while the I/O happens in the background, which is great for scalability.

我的Windows服务是一个socket服务器,并使用开始/结束的风格异步套接字I / O。混合我的魔法,我知道了。我在EndRead回调,接收请求做工作。我不希望这样做回调阻塞线程。

My Windows Service is a socket server, and it uses Begin / End style async socket I/O. Mixing my magic, I know. I'm in the EndRead callback, receiving a request to do work. I don't want the thread that made the callback to block.

我使用Nito.AsyncEx库,使用AsyncContext.Run(()=> DoMyBiddingHopefullyInTheBackground(...))...

I'm using the Nito.AsyncEx library, using AsyncContext.Run(() => DoMyBiddingHopefullyInTheBackground(...))...

private void EndRead(IAsyncResult result)
{
    int nRead = m_socketStream.EndRead(result);
    ...
    AsyncContext.Run(() => DoMyBiddingHopefullyInTheBackground(...))
}

private async Task DoMyBiddingHopefullyInTheBackground(...)
{
    await DoSomeAsyncIoWork();
}

使用AsyncContext是我能找到的非异步标记code调用使用的await异步标记code的唯一途径。

Using AsyncContext was the only way I could find for non-async-marked code to call async-marked code that uses await.

威尔AsyncContext.Run阻止叫我的承口端回调例程线程?如果是这样,有什么办法让这个线程返回到线程池的异步过程中/等待I / O?

Will AsyncContext.Run block the thread that called my socket End callback routine? If so, is there any way to make it so that that thread goes back to the thread pool during the async / await I/O?

推荐答案

如果你想运行一个异步方法,你不想等待它完成,试试这个:

If you want to run an async method and you don't want to wait for it to complete, try this:

Task.Run(() => DoMyBiddingInTheBackground());

这篇关于是否有可能为一个.NET Windows服务中的I / O回调调用C#的等待不阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆