我最近创build了一个简单的应用程序来testingHTTP调用吞吐量,这个吞吐量可以用一种经典的multithreading方法以asynchronous方式生成。 该应用程序能够执行预定义数量的HTTP调用,并在最后显示执行它们所需的总时间。 在我的testing中,所有的HTTP调用都是由我的本地IIS服务器进行的,他们检索到一个小文本文件(12字节大小)。 下面列出了asynchronous实现代码中最重要的部分: public async void TestAsync() { this.TestInit(); HttpClient httpClient = new HttpClient(); for (int i = 0; i < NUMBER_OF_REQUESTS; i++) { ProcessUrlAsync(httpClient); } } private async void ProcessUrlAsync(HttpClient httpClient) { HttpResponseMessage httpResponse = null; try { Task<HttpResponseMessage> getTask = httpClient.GetAsync(URL); httpResponse = await getTask; Interlocked.Increment(ref _successfulCalls); } catch (Exception ex) { […]
我想创build一个完成的Task (不是Task<T> )。 是否有内置的.NET来做到这一点? 一个相关的问题: 创build一个完成的任务<T>
我读过的每篇博文都告诉你如何在C#中使用一个asynchronous方法,但由于某种奇怪的原因,从来没有解释如何构build自己的asynchronous方法来使用。 所以我现在有这个代码消耗我的方法: private async void button1_Click(object sender, EventArgs e) { var now = await CountToAsync(1000); label1.Text = now.ToString(); } 而我写的这个方法就是CountToAsync : private Task<DateTime> CountToAsync(int num = 1000) { return Task.Factory.StartNew(() => { for (int i = 0; i < num; i++) { Console.WriteLine("#{0}", i); } }).ContinueWith(x => DateTime.Now); } 是这样,使用Task.Factory ,写一个asynchronous方法的最好方法,或者我应该以另一种方式写这个?
我有这个代码: private async void ContextMenuForGroupRightTapped(object sender, RightTappedRoutedEventArgs args) { CheckBox ckbx = null; if (sender is CheckBox) { ckbx = sender as CheckBox; } if (null == ckbx) { return; } string groupName = ckbx.Content.ToString(); var contextMenu = new PopupMenu(); // Add a command to edit the current Group contextMenu.Commands.Add(new UICommand("Edit this Group", (contextMenuCmd) => […]
我正在开始创build一个应用程序,它将从C#5的asynchronous等待function中获益。 但是我不确定要使用哪个版本的VS和asynchronous运行时。 看看OSstream行榜,我需要再支持Windows XP三年左右。 它看起来像.net 4.5只运行在较新版本的Windows,所以我需要的目标.net 4.0。 开发机器使用Windows 7,所以使用更新版本的VS不是问题。 现在我需要先select一个编译器来做到这一点: VS2010与AsyncCTP VS2012预览(最后一次到达),将目标设置为.net 4.0 单声道(看起来像2.12有asynchronous等待,我更喜欢/用于作为IDE的MonoDevelop VS) 哪一个有更less的代码漏洞? 看看Jon Skeet的博客 ,VS2012 Preview使用了一个比CTP更好的代码生成器。 更重要的是使用哪个运行时? VS2012是否包含可用于.net 4的可再发行的asynchronous运行时? 我设法通过引用AsyncCTP运行时来预览编译代码。 但是由于CTP具有奇怪的许可条件,这看起来不是一个好的长期解决scheme。 或者我应该使用第三方实施? 也许mono有一个? 为了发布这个库,我比较喜欢简单地将dll放在与应用程序相同的目录中,而不是某种安装程序。 如果我的二进制文件在mono + Linux / MacOS上没有变化,我也会喜欢它。 所以运行时应该与任何单声道(可能是2.12)内置的兼容,或者允许在非Windows操作系统上使用。
前言 :我正在寻找一个解释,而不仅仅是一个解决scheme。 我已经知道解决scheme。 尽pipe花了几天的时间研究有关基于任务的asynchronous模式(TAP)的asynchronous和等待的MSDN文章,但我仍然对某些更精细的细节感到困惑。 我正在为Windowsapp store应用程序编写logging器,并且要支持asynchronous和同步logging。 asynchronous方法遵循TAP,同步的方法应该隐藏所有这些,并且像普通的方法一样工作。 这是asynchronouslogging的核心方法: private async Task WriteToLogAsync(string text) { StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFile file = await folder.CreateFileAsync("log.log", CreationCollisionOption.OpenIfExists); await FileIO.AppendTextAsync(file, text, Windows.Storage.Streams.UnicodeEncoding.Utf8); } 现在相应的同步方法… 版本1 : private void WriteToLog(string text) { Task task = WriteToLogAsync(text); task.Wait(); } 这看起来是正确的,但它不起作用。 整个程序永远冻结。 版本2 : 嗯..也许任务没有开始? private void WriteToLog(string text) { Task task […]
使用Parallel.ForEach或Task.Run()asynchronous启动一组任务之间有什么区别? 版本1: List<string> strings = new List<string> { "s1", "s2", "s3" }; Parallel.ForEach(strings, s => { DoSomething(s); }); 版本2: List<string> strings = new List<string> { "s1", "s2", "s3" }; List<Task> Tasks = new List<Task>(); foreach (var s in strings) { Tasks.Add(Task.Run(() => DoSomething(s))); } await Task.WhenAll(Tasks);
我正在挖掘节点7asynchronous/等待function,并像这样在代码中徘徊 async function main() { try { var quote = await getQuote(); console.log(quote); } catch(error) { console.error(error); } } 这似乎是唯一的可能性解决/拒绝或返回/抛出asynchronous/等待,但是,V8不优化代码try / catch块? 有替代品吗?
我开始学习C#5.0中的asynchronous/等待,而我完全不理解它。 我不明白它如何用于并行。 我已经尝试了以下非常基本的程序: using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Task task1 = Task1(); Task task2 = Task2(); Task.WaitAll(task1, task2); Debug.WriteLine("Finished main method"); } public static async Task Task1() { await new Task(() => Thread.Sleep(TimeSpan.FromSeconds(5))); Debug.WriteLine("Finished Task1"); […]
简介 :我想在构造函数中调用asynchronous方法。 这可能吗? 详细信息 :我有一个名为getwritings()分析JSON数据的方法。 一切正常,如果我只是在async方法中调用getwritings() ,并await它的左侧。 然而,当我在我的页面中创build一个LongListView并尝试填充它时,我发现getWritings()出人意料地返回null ,而LongListView是空的。 为了解决这个问题,我试着将getWritings()的返回types改为Task<List<Writing>> ,然后通过getWritings().Result在构造函数中检索getWritings().Result 。 但是,这样做最终会阻塞UI线程。 public partial class Page2 : PhoneApplicationPage { List<Writing> writings; public Page2() { InitializeComponent(); getWritings(); } private async void getWritings() { string jsonData = await JsonDataManager.GetJsonAsync("1"); JObject obj = JObject.Parse(jsonData); JArray array = (JArray)obj["posts"]; for (int i = 0; i < array.Count; i++) […]