winrt-async

Metro App FileIO.WriteTextAsync Multiple Threads

扶醉桌前 提交于 2019-12-07 07:59:04
问题 I have a method which is called frequently from multiple threads. It involves writing to disk using await FileIO.WriteTextAsync . This works fine when it is called from a single thread, but once I start doing this in multiple threads, I get this error: The process cannot access the file because it is being used by another process. I know what the error means, but I'm not sure how to work around it. Normally, I would create a lock(object) statement, to ensure that the file is being accessed by

Why does HttpClient appear to deadlock here?

放肆的年华 提交于 2019-12-07 00:33:27
I have an API made in a portable class library which needs to reach out to platform specific APIs for sending HTTP requests. Here is the method I wrote to do an HTTP POST on WinRT: public bool Post(IEnumerable<KeyValuePair<string, string>> 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 {

GetFilesAsync stops working

与世无争的帅哥 提交于 2019-12-06 04:16:12
问题 I've this piece of code public static class Storage { public async static Task<bool> 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; } } and calling it from my Windows 8 Store application; this.IconExists = this.Game != null && Storage.Exists(this.IconName)

Task caching when performing Tasks in parallel with WhenAll

蓝咒 提交于 2019-12-05 16:39:28
So I have this small code block that will perform several Tasks in parallel. // 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 => t.AthleteTask)); // Set the athletes foreach(var pair in results) { pair.Activity.Athlete = pair.AthleteTask.Result; }

WinRT: DataReader.LoadAsync Exception with StreamSocket TCP

会有一股神秘感。 提交于 2019-12-05 12:43:08
I'm programming a client app on WinRT in C# which connects to several servers by TCP. For the TCP connection I use StreamSocket. The Input and Output Strings are then wrapped in a DataWriter and a DataReader. When I connect to more than one server I get the following exception: "Operation identifier is not valid" This is the code of the method: 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

A method was called at an unexpected time

随声附和 提交于 2019-12-05 12:11:17
问题 I'm trying to iterate all files in a directory using GetFilesAsync, but every time I call the GetResults method, it throws an exception that says System.InvalidOperationException: A method was called at an unexpected time The code is simply var files = myStorageFolder.GetFilesAsync(); //runs fine var results = files.GetResults(); //throws the exception I'm new to Win 8 dev so I might be missing something obvious. Edit (solved) I'm running my console application, but now that the program runs

Metro App FileIO.WriteTextAsync Multiple Threads

馋奶兔 提交于 2019-12-05 09:31:25
I have a method which is called frequently from multiple threads. It involves writing to disk using await FileIO.WriteTextAsync . This works fine when it is called from a single thread, but once I start doing this in multiple threads, I get this error: The process cannot access the file because it is being used by another process. I know what the error means, but I'm not sure how to work around it. Normally, I would create a lock(object) statement, to ensure that the file is being accessed by only one thread at a time. However, this is an asynchronous method and as such I can't use the await

GetFilesAsync stops working

我是研究僧i 提交于 2019-12-04 11:47:19
I've this piece of code public static class Storage { public async static Task<bool> 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; } } and calling it from my Windows 8 Store application; this.IconExists = this.Game != null && Storage.Exists(this.IconName).Result; So if I put a break point on the above line and run it step by step, it works, but without

A method was called at an unexpected time

妖精的绣舞 提交于 2019-12-03 22:18:17
I'm trying to iterate all files in a directory using GetFilesAsync, but every time I call the GetResults method, it throws an exception that says System.InvalidOperationException: A method was called at an unexpected time The code is simply var files = myStorageFolder.GetFilesAsync(); //runs fine var results = files.GetResults(); //throws the exception I'm new to Win 8 dev so I might be missing something obvious. Edit (solved) I'm running my console application, but now that the program runs async, the files.GetResult() method no longer exists. static void Main(string[] args) { var files =

Saved files sometime only contains NUL-characters

我与影子孤独终老i 提交于 2019-12-03 15:30:40
We have a problem in our Windows 8.1 application (WinRT) that sometimes our saved file gets corrupt. The files have a correct file size, but the file only contains NUL-characters. The file should contain a serialized object as XML. In an attempt to find the issue we do not overwrite the file, we do the following: Serialize the current object to a temp file. Check the content of the temp file Copy the current file (to .timestamp.xml.bak) Move/replace the temp file to the current file Most of the time this all works fine, but sometimes the .timestamp.xml.bak-file and the content file get corrupt