Tag: c# 5.0

经常性背景任务

我刚刚开始尝试使用“任务”而不是线程,并尝试使用后台“清理”任务实现一个对象,该任务每5分钟运行一次,只要该对象正在使用但不应阻止垃圾收集。 一些粗糙的东西(显然不起作用……) public class Foo : IDisposable { private CancellationTokenSource _tokenSource = new CancellationTokenSource(); public Foo() { Cleanup(); } public void Dispose() { _tokenSource.Cancel(); } public void Cleanup() { Task.Delay(TimeSpan.FromSeconds(5), _tokenSource.Token).ContinueWith(t => { //Do Work if (!_tokenSource.IsCancellationRequested) { Cleanup(); } }, TaskContinuationOptions.NotOnCanceled); } } 实施的正确方法是什么? 编辑为了回应I3arnon的问题,我正在使用IDisposable,因为当我完成对象时,我希望它被垃圾收集。 例如,如果没有下面的f.Dispose()(取消任务,f似乎不是Garbage Collected,或者取消了清理任务。有没有更好的方法来实现? var f = new Foo(); var r […]

ReactiveUI 5.0.2中缺少ReactiveAsyncCommand

我刚从https://github.com/reactiveui/ReactiveUI.Samples/blob/master/ReactiveUI_4Only.Samples.sln开始学习ReactiveUI。 我通过nuget下载最新版本但我无法在ReactiveUI.Xaml中找到类ReactiveAsyncCommand。

C#async /等待控制台应用程序中的奇怪行为

我构建了一些异步/等待演示控制台应用程序并得到奇怪的结果。 码: class Program { public static void BeginLongIO(Action act) { Console.WriteLine(“In BeginLongIO start… {0} {1}”, (DateTime.Now.Ticks – ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(1000); act(); Console.WriteLine(“In BeginLongIO end… \t{0} {1}”, (DateTime.Now.Ticks – ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId); } public static Int32 EndLongIO() { Console.WriteLine(“In EndLongIO start… \t{0} {1}”, (DateTime.Now.Ticks – ticks) / TimeSpan.TicksPerMillisecond, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(500); Console.WriteLine(“In EndLongIO […]

如果我使用异步,不应该使用更少的线程数?

我的理解是,如果我使用异步,线程会发出Web请求并继续前进。 当响应回来时,另一个线程从那里接收它。 因此,有较少数量的捆绑线程处于空闲状态。 这是不是意味着最大活线程数会下降? 但在下面的示例中,不使用异步的代码最终使用较少数量的线程。 有人可以解释为什么吗? 没有异步的代码(使用较小的线程): using System; using System.Diagnostics; using System.IO; using System.Net; using System.Threading; namespace NoAsync { internal class Program { private const int totalCalls = 100; private static void Main(string[] args) { for (int i = 1; i <= totalCalls; i++) { ThreadPool.QueueUserWorkItem(GoogleSearch, i); } Thread.Sleep(100000); } private static void GoogleSearch(object […]

如何在C#<6中模拟C#6空条件

使用C#6.0,我可以做到这一点 var isEqual = x.Id == y.Id && x.UpdatedAt == y.UpdatedAt && x.Name == y.Name && x.RulesUrl == y.RulesUrl && x.OngoingChallenges?.Count == y.OngoingChallenges?.Count && x.MembershipIds?.Count == y.MembershipIds?.Count; 有没有什么好的解决方案,用C#<6.0做到这一点? 我的意思是这部分 && x.OngoingChallenges?.Count == y.OngoingChallenges?.Count && x.MembershipIds?.Count == y.MembershipIds?.Count; 因为在旧项目中我们没有可能使用C#6.0。 如何有效地写isEqual ?

将图像从图像控件存储到StorageFile

如何从Windowsapp store应用中的图像控制将图像存储到StorageFile? 我正在使用以下链接,但这对我没用 StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync(” “, new Uri(user.ProfilePicUrl), new RandomAccessStream()); http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefile.createstreamedfileasync.aspx 我需要明确解决我的问题。 我怎样才能做到这一点?

如何编写C#5异步?

我有以下情况: 当输入命令时(为了测试,它是一个控制台应用程序,当它准备就绪时,我希望它将是一个WebService)我执行一些代码,当需要进一步的用户输入时,我立即返回命令解释器。 当给出新输入时,我希望处理从我离开它的地方继续。 这听起来很像c#5 async-await模式,我决定尝试一下。 我在想这个: public void CommandParser() { while(true) { string s = Console.ReadLine(); if (s == “do_something”) Execute(); else if (s == “give_parameters”) SetParameters(); //… } } MySettings input; public async void Execute() { //do stuff here MyResult result = null if (/*input needed*/){ input = new MySetting(); result = await input.Calculate(); […]

在.NET 4.0中等待替代?

什么是.NET 4.0中await关键字的最佳替代方案? 我有一个方法,需要在异步操作后返回一个值。 我注意到wait()方法完全阻塞了线程,从而使异步操作无效。 在释放UI线程的同时运行异步操作有哪些选择?

如何在不使用Rx Framework的情况下限制事件的速度

我想限制一个事件的速度,如何在不使用Microsoft Rx框架的情况下实现这一目标。 我在Rx的帮助下做到了这一点。 但我正在尝试的是,我需要根据时间段限制Map的View更改事件。 是否可以在不使用Rx的情况下实现相同的function。 我不允许使用Rx,我必须保持二进制大小尽可能小。

为什么WebClient.DownloadStringTaskAsync()会阻塞? – 新的异步API /语法/ CTP

出于某种原因,在下面的程序启动后会暂停。 我相信WebClient().DownloadStringTaskAsync()是原因。 class Program { static void Main(string[] args) { AsyncReturnTask(); for (int i = 0; i < 15; i++) { Console.WriteLine(i); Thread.Sleep(100); } } public static async void AsyncReturnTask() { var result = await DownloadAndReturnTaskStringAsync(); Console.WriteLine(result); } private static async Task DownloadAndReturnTaskStringAsync() { return await new WebClient().DownloadStringTaskAsync(new Uri(“http://www.weather.gov”)); } } 据我所知,我的程序应该立即从0到15开始计数。 难道我做错了什么? 我在原始的Netflix下载示例(使用CTP获得)时遇到了同样的问题 – […]