Tag: async await

针对特定exception的unit testing异步方法

有没有人有一个如何在Windows 8 Metro应用程序中unit testing异步方法的示例,以确保它抛出所需的exception? 给定一个具有异步方法的类 public static class AsyncMathsStatic { private const int DELAY = 500; public static async Task Divide(int A, int B) { await Task.Delay(DELAY); if (B == 0) throw new DivideByZeroException(); else return A / B; } } 我想使用新的Async.ExpectsException构造编写测试方法。 我试过了 :- [TestMethod] public void DivideTest1() { Assert.ThrowsException(async () => { int Result […]

了解多任务的异步/等待模式

目前,我很难理解使用异步和等待模式的多任务处理。 为了测试一个案例,我编写了以下代码来理解一些基础知识; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private int global_int = 10; public async Task RunAsyncTask() { // This method runs asynchronously. await Task.Run(() => Calculate()); return global_int; } private int Calculate() { Console.WriteLine(“Ticket count: ” + –global_int); return global_int; } private async void Start_Button_Click(object sender, RoutedEventArgs e) […]

xUnit和Moq不支持async – 等待关键字

我试图发现如何将async和await关键字应用于我的xUnit测试。 我使用的是xUnit 1.9和Async CTP 1.3。 这是我的测试用例 我有一个接口,它指定一个异步方法调用 public interface IDoStuffAsync { Task AnAsyncMethod(string value); } 我有一个消耗接口并调用异步方法的类 public class UseAnAsyncThing { private readonly IDoStuffAsync _doStuffAsync; public UseAnAsyncThing(IDoStuffAsync doStuffAsync) { _doStuffAsync = doStuffAsync; } public async Task DoThatAsyncOperation(string theValue) { await _doStuffAsync.AnAsyncMethod(theValue); } } 在我的测试中,我希望检查方法DoThatAsyncOperation是否使用正确的值调用方法,因此我模拟了接口并使用Moq来validation调用 [Fact] public async void The_test_will_pass_even_though_it_should_fail() { var mock = new Mock(); var […]

C#UWP XAML-更新控件“同步”

在这个问题中,我已经包含了一些链接,这些链接表明我已经完成了一些寻找解决方案的工作。 我正在开发一个带触摸屏和GPIO的UWP应用程序。 UI具有停止按钮,重置按钮和文本块。 GPIO用于物理启动按钮,电机和2个限位开关。 电机可以旋转,直到它进入限位开关。 用于控制硬件的代码(例如,Motor.Forward())已经编写和测试,但为了简洁起见,将其排除在此问题之外。 出于同样的原因,排除了停止按钮的代码。 如果这些方法中的步骤将同步执行…可能会通过以下代码描述所需的行为: //Event handler for when physical start button is pushed private async void StartButtonPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args) { Start(); } private void Start() { //Update UI stopButton.IsEnabled = true; resetButton.IsEnabled = false; textBlock.Text = “Motor turning”; Motor.Forward(); while(Motor.HasNotHitEndLimit()) Motor.Off(); //Update UI stopButton.IsEnabled = false; resetButton.IsEnabled = true; textBlock.Text […]

.Net调用异步方法并等待

我有一个ansyc方法 public Task GetCar() { } 我可以调用此方法异步并等待: Car car = await GetCar() 如何使用MethodInfo.Invoke调用该方法并异步等待结果。 MethodInfo method = obj.GetMethod(“GetCar”); method.Invoke( obj, null)

表观BufferBlock.Post/Receive/ReceiveAsync种族/错误

交叉发布到http://social.msdn.microsoft.com/Forums/en-US/tpldataflow/thread/89b3f71d-3777-4fad-9c11-50d8dc81a4a9 我知道……我并没有真正使用TplDataflow来发挥它的最大潜力。 ATM我只是使用BufferBlock作为消息传递的安全队列,其中生产者和消费者以不同的速率运行。 我看到一些奇怪的行为让我感到难以理解如何继续。 private BufferBlock messageQueue = new BufferBlock(); public void Send(object message) { var accepted=messageQueue.Post(message); logger.Info(“Send message was called qlen = {0} accepted={1}”, messageQueue.Count,accepted); } public async Task GetMessageAsync() { try { var m = await messageQueue.ReceiveAsync(TimeSpan.FromSeconds(30)); //despite messageQueue.Count>0 next line //occasionally does not execute logger.Info(“message received”); //……. } catch(TimeoutException) { //do something […]

在c#中启动许多异步任务的语法

我在c#中使用新的async / await工具时遇到了麻烦。 这是我的情景: static async Task ManageSomeRemoteTask(int Id, bool flag) { var result = await serviceClient.AuthenticateIdAsync(Id); [… Setup Some Data …] await serviceClient.LongAndSlowRemoteCallAsync(Data); } static void SendATonOfJunkToSomeWebServiceThatDoesntSupportBatches { var myTasks = Dictionary<int, Task>(); while(IdsLeftToProcess > 0 ) { Task t = ManageSomeRemoteTask(Id, true); myTasks.Add(IdsLeftToProcess ,t); myTasks[IdsLeftToProcess].Start(); IdsLeftToProcess –; } Task.WaitAll(myTasks.Values.ToArray()); //Wait until they are […]

如何在解决方案中列出缺少等待调用的所有Task-或Task – 返回方法调用?

如何在解决方案中列出缺少await调用的所有Task – 或Task返回方法调用? 我尝试在Google上搜索一些内容,但没有弹出任何内容。 例如: public Task DoWork() { return Task.FromResult(true); // would be listed, even though it’s “correct” } public async Task DoMoreWork() { await DoWork(); // would not be listed DoWork(); // would be listed DoWork(); // would be listed a second time in this same method } public void DoEvenMoreWork() { DoWork(); […]

如何在VS 2010 for WP7中使用async-await和WCF?

Async CTP非常好用。 我看到了一些Windows手机的例子,全部使用: (程序集AsyncCtpLibrary_Phone.dll,v2.0.50727,AsyncCtpExtensions)。 var client = new WebClient(); string data = await client.DownloadStringTaskAsync(new Uri(url)); 或类似上面的代码。 如何使用async / await方法为我的服务? (WCF) 今天所有方法都是这样工作的: service.MyRequestCompleted += service_myRequestCompleted; service.MyRequestAsync(); 我找不到一种方法来使用服务的扩展方法。

图像被分配给ListView的另一个行项目

我知道async await非常方便在设置适配器之前为ListView / GridView准备数据。 例如: // in Activity.cs async void OnCreate(Bundle SavedInstanceState) { SetContentView(…); ListView listView = FindViewById(…); AdapterData data = await Task.Run(() => GetDataFromWorkerThread); listView.SetAdapter(data); } 但我需要的是: // in ListViewAdapter.cs public override View GetView (int position, View convertView, ViewGroup parent) { if(convertView == null) { // create new view } ImageView imgView = convertView.FindViewById(Resouce.Id.img_view_id); […]