Tag: winrt async

为什么HttpClient似乎在这里死锁?

我有一个在便携式类库中制作的API,需要使用特定于平台的API来发送HTTP请求。 这是我写的在WinRT上执行HTTP POST的方法: public bool Post(IEnumerable<KeyValuePair> headers, string data) { bool success = false; HttpClient client = new HttpClient(new HttpClientHandler {AllowAutoRedirect = false}); foreach (var header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } try { var task=client.PostAsync(endpoint, new StringContent(data, Encoding.UTF8, “text/xml”)).ContinueWith( postTask => { try { postTask.Wait(client.Timeout); //Don’t wait longer than the client timeout. success = […]

与WhenAll并行执行任务时的任务缓存

所以我有这个小代码块,可以并行执行几个任务。 // no wrapping in Task, it is async var activityList = await dataService.GetActivitiesAsync(); // Select a good enough tuple var results = (from activity in activityList select new { Activity = activity, AthleteTask = dataService.GetAthleteAsync(activity.AthleteID) }).ToList(); // begin enumeration // Wait for them to finish, ie relinquish control of the thread await Task.WhenAll(results.Select(t => […]

使用StreamSocket TCP的WinRT:DataReader.LoadAsyncexception

我正在使用C#在WinRT上编写客户端应用程序,该应用程序通过TCP连接到多个服务器。 对于TCP连接,我使用StreamSocket。 然后将输入和输出字符串包装在DataWriter和DataReader中。 当我连接到多个服务器时,我得到以下exception:“操作标识符无效” 这是方法的代码: private async void read() { while (true) { uint bytesRead = 0; try { bytesRead = await reader.LoadAsync(receiveBufferSize); if (bytesRead == 0) { OnClientDisconnected(this); return; } byte[] data = new byte[bytesRead]; reader.ReadBytes(data); if (reader.UnconsumedBufferLength > 0) { throw new Exception(); } OnDataRead(this, data); } catch (Exception ex) { if (Error […]

GetFilesAsync停止工作

我有这段代码 public static class Storage { public async static Task Exists(string filename) { var folder = await Package.Current.InstalledLocation.GetFolderAsync(“Assets”); var _files= await folder.GetFilesAsync(CommonFileQuery.OrderByName).AsTask().ConfigureAwait(false); var file = _files.FirstOrDefault(x => x.Name == filename); return file != null; } } 并从我的Windows 8商店应用程序调用它; this.IconExists = this.Game != null && Storage.Exists(this.IconName).Result; 因此,如果我在上面的行上放置一个断点并逐步运行它,它可以工作,但是没有中断并且只是运行应用程序会导致应用程序挂起。 几天前,类似的代码正在提交; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; […]

如何等待QueryCompleted事件?

我创建了一个小测试应用程序来获取经度和纬度并将其转换为实际地址: using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Device.Location; using System.Linq; using System.Net; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Maps.Services; using Microsoft.Phone.Shell; using PhoneApp1.Resources; using Windows.Devices.Geolocation; namespace PhoneApp1 { public partial class MainPage : PhoneApplicationPage { private GeoCoordinate Location; public ObservableCollection Addresses { get; set; } […]

如何在WinRT中同时允许多个弹出窗口?

如果在MessageDialog对象上调用ShowAsync命令时,如果另一个MessageDialog对象已经显示给用户但未被解除(即当另一个已经启动时显示弹出窗口),则抛出UnauthorizedAccessException 。 当您有多个线程试图同时提醒用户时,这会使事情变得困难。 我当前的(权宜之计)解决方案只是用try / catch块包围ShowAsync调用并吞下exception。 这不合期望地导致用户错过后续通知。 我能想到的另一种方法是手动实现某种弹出队列。 这似乎是一项过多的工作,但是,考虑到其他框架(如Windows Phone)没有这个问题,并且只会在用户解雇时一个接一个地显示弹出窗口。 有没有其他方法可以解决这个问题?

如何从WinRT / Windowsapp store应用程序发送电子邮件?

我正在开发Windowsapp store应用程序(Windows 8)。 我需要根据存储在应用程序数据中的数据和地址发送电子邮件,而无需用户输入数据或地址。 实现它的正确/简单方法是什么? EitanB