Tag: 异步

我应该使用等待异步Action方法吗?

我需要在我的Controller中实现一个异步Action,用于长时间运行的外部API调用。 看一些教程,我已经实现了我的方法: [AsyncTimeout(200)] public async Task DoAsync() { // Execute long running call. return View(); } 我的问题是,这足以使真正的非块异步吗? 我是否还需要应用await运算符,如果是,我该怎么做?

entity framework异步发出上下文或查询?

我的下面的查询有异步问题。 我有单例上下文,我试图执行以下查询: var query = await (from parent in Context.ParentTable join child in Context.ChildTable on parent.ID equals child.ID into allResult from ResultValue in allResult.DefaultIfEmpty() where ResultValue.TenantId == tenantId select new Result { Code = parent.Code, Type = parent.Type, ID = ResultValue == null ? 0 : ResultValue.Id }).ToListAsync(); 我的单例上下文如下所示: public class BaseRepository { private readonly […]

DownloadStringAsync等待请求完成

我使用此代码来检索url内容: private ArrayList request(string query) { ArrayList parsed_output = new ArrayList(); string url = string.Format( “http://url.com/?query={0}”, Uri.EscapeDataString(query)); Uri uri = new Uri(url); using (WebClient client = new WebClient()) { client.DownloadStringAsync(uri); } // how to wait for DownloadStringAsync to finish and return ArrayList } 我想使用DownloadStringAsync因为DownloadString挂起了应用程序GUI,但我希望能够根据request返回结果。 我怎么能等到DownloadStringAsync完成请求?

在.NET 4.5中找不到HttpClient

我试图在.NET 4.5中使用新的HttpClient ,但Visual Studio抱怨它不存在。 我有System.Net ,但是当我键入System.Net.Http ,它也会抱怨它。 我应该为这个类下载一个新的distributable吗?

C#异步套接字服务器接收问题

我已在此处实现了有关此post的服务器应用程序: http : //www.codeguru.com/csharp/csharp/cs_network/sockets/article.php/c8781#Client1 总结:我正在使用异步套接字ala BeginAccept(..),BeginReceive(..)。 我的服务器能够处理多个客户端,一切正常,直到客户端执行两个或多个同步发送操作,而无需等待一段时间。 客户端没有收到任何错误,因此没有得到通知,服务器没有收到第二条消息! 如果客户等待约。 第一次发送操作后100ms,一切正常。 我认为当我使用TCP时,我可以确保服务器收到消息。 (除了抛出exception)! 你能帮我解决一下吗? 这是我在服务器中实现的WaitForData(..)和OnDataReceive(..)方法 public void WaitForData(MyClient client) { try { if (pfnCallBack == null) { pfnCallBack = new AsyncCallback(OnDataReceived); } iarResult = client.Socket.BeginReceive(client.DataBuffer, 0, client.DataBuffer.Length, SocketFlags.None, pfnCallBack, client); } catch (SocketException se) { MessageBox.Show(“SocketException@WaitForData” + se.Message); } } public void OnDataReceived(IAsyncResult asyn) { try […]

c#.net为什么Task.Run似乎以不同于其他代码的方式处理Func ?

作为.NET 4.5的一部分的新Task.Run静态方法似乎没有像人们期望的那样运行。 例如: Task t = Task.Run(()=>5); 编译好,但是 Task t = Task.Run(MyIntReturningMethod); … public Int32 MyIntReturningMethod() { return (5); } 抱怨MyIntReturningMethod返回了错误的类型。 也许我只是不了解正在调用Task.Run的哪个重载。 但在我看来,上面的lambda代码看起来很像Func ,而MyIntReturningMethod肯定与Func兼容 对于发生了什么的任何想法? 迈克尔

我该怎么做才能在.NET 2.0中使用Task ?

.NET 4.0有TPL,它包含很好的Task类来封装异步编程模型。 我正在开发一个必须是.NET 2.0的应用程序,但我想避免重写Task。 有什么建议?

异步并等待For循环

我有一个Windows服务,根据计划运行各种作业。 确定要运行的作业后,会将一个计划对象列表发送到迭代列表并运行每个作业的方法。 问题是由于外部数据库调用,某些作业最多可能需要10分钟才能运行。 我的目标是没有一个工作阻止其他队列,基本上一次有多个运行。 我认为使用async和await可以解决这个问题,但我以前从未使用过这些。 现行代码: public static bool Load(List scheduleList) { foreach (Schedule schedule in scheduleList) { Load(schedule.ScheduleId); } return true; } public static bool Load(int scheduleId) { // make database and other external resource calls // some jobs run for up to 10 minutes return true; } 我尝试更新到这段代码: public async static Task LoadAsync(List […]

从同步方法调用异步方法

我试图从同步方法运行异步方法。 但我无法等待 异步方法,因为我在同步方法中。 我不能理解TPL,因为这是我第一次使用它。 private void GetAllData() { GetData1() GetData2() GetData3() } 每个方法都需要先前的方法来完成,因为第一个方法的数据用于第二个方法。 但是,在每个方法中我都想启动多个Task操作以加快性能。 然后我想等他们所有人完成。 GetData1看起来像这样 internal static void GetData1 () { const int CONCURRENCY_LEVEL = 15; List<Task> dataTasks = new List<Task>(); for (int item = 0; item < TotalItems; item++) { dataTasks.Add(MyAyncMethod(State[item])); } int taskIndex = 0; //Schedule tasks to concurency level (or all) […]

C#中SQL Server存储过程的异步调用

是否可以通过C# 异步调用SQL Server存储过程 ? 我有一个存储过程写一个特定数据库的备份(这可能需要很长时间才能完成),我想在Windows窗体中显示备份过程的进度(为此我使用http://www.wisesoft .co.uk / articles / tsql_backup_restore_progress.aspx )。 或者我应该使用Backgroundworker控件并在backgroundjob(自己的线程)中运行SP?