似乎Monitor在WinRT商店应用程序中无法正常工作.我有以下代码:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var tasks = Enumerable.Range(0, 10).Select((i)=>new Task(DoWork)).ToArray();
foreach (var task in tasks)
{
task.Start();
}
Task.WaitAll(tasks);
}
static object lockObject = new Object();//typeof(MainPage)
protected async void DoWork()
{
bool taken =false;
Monitor.Enter(lockObject, ref taken);
Debug.WriteLine("In");
await Task.Delay(1000);
Debug.WriteLine("Out");
if (taken) Monitor.Exit(lockObject);
}
Run Code Online (Sandbox Code Playgroud)
在输出窗口中,我看到:
In
In
In
In
In
In
In
Out
Out
Out
Out
Out
Out
Out
In
Out
A first chance exception of type 'System.Threading.SynchronizationLockException' occurred in App4.exe
Run Code Online (Sandbox Code Playgroud)
这意味着Monitor没有锁定关键区域.有没有人知道我做错了什么?
请求:
我来自不同的互联网位置,json对象.
这些包含我放在BingMap上的多个Geocoordinates.这非常有效.
问题:
但是当我从互联网位置获取数据时,我得到了阻止ui.有没有办法在后台运行?
我尝试了异步功能,但我也有阻止UI ...
这里有一些代码来电
public async void Caller_Click(){
await jsonDataClass.DoOperations();
}
Run Code Online (Sandbox Code Playgroud)
jsonDataClass中的方法
public async Task<bool> DoOperations(){
// do requests and some stuff..
var fetchedElements = getdata(); // not async, because its in a portable lib
foreach (var element in fetchedElements)
OnEvent(element); // raises an event to assing the element to the Bing map
}
Run Code Online (Sandbox Code Playgroud) c# bing-maps portable-class-library windows-runtime winrt-async
我怎么知道异步(等待)操作是否已经在运行并等待应用程序中的完成.我在视图中有两个按钮,每个按钮绑定到两个不同的异步方法.如果单击button1并启动异步并等待其结果.并且在那个时候点击button2.我需要显示一条消息,即已经运行的异步方法存在,并停止执行第二个异步方法.我怎样才能实现这一目标?
我似乎无法从Windows应用商店的后台任务中读取文件.这是读取文件内容的代码:
async private static Task<string> ReadAsync(string FileName)
{
var folder = ApplicationData.Current.LocalFolder;
var file = await folder.GetFileAsync(FileName);
Windows.Storage.Streams.IRandomAccessStreamWithContentType inputStream = null;
try
{
inputStream = await file.OpenReadAsync();
}
catch (Exception ex)
{
throw (ex);
}
string content = string.Empty;
using (Stream stream = inputStream.AsStreamForRead())
{
using (StreamReader reader = new StreamReader(stream))
{
try
{
// *** program exits on this line
content = await Task.Run(() => reader.ReadToEnd());
}
catch(Exception ex)
{
// no error is caught
content = ex.Message;
} …Run Code Online (Sandbox Code Playgroud) c# windows-runtime winrt-xaml winrt-async windows-store-apps
我正在编写一个WP8应用程序,并希望在应用程序停用或关闭时保存一些数据.
我已经尝试了新的WinRT api,但在停用应用程序时无法正常工作:|
代码:
public async Task CacheData()
{
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
string data = "Hello!";
///WinRT not Working !!!
// Get a reference to the Local Folder
// Create the file in the local folder, or if it already exists, just replace it
StorageFile storageFileIsolated = await localFolder.CreateFileAsync("Data.data", CreationCollisionOption.ReplaceExisting);
Stream writeStream = await storageFileIsolated.OpenStreamForWriteAsync();
using (StreamWriter writer = new StreamWriter(writeStream))
{
await writer.WriteAsync(data);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但与老api一起工作正常
string data = "Hello!";
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using …Run Code Online (Sandbox Code Playgroud) 在WinRT中是否有任何类作为WP中的MarketPlaceReview或MarketPlaceSearch任务?
谢谢.
我创建了一个小测试应用程序来获取经度和纬度并将其转换为实际地址:
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<string> Addresses { get; set; }
// Constructor
public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
protected override async void OnNavigatedTo( NavigationEventArgs e )
{
await …Run Code Online (Sandbox Code Playgroud) winrt-async ×7
c# ×5
winrt-xaml ×2
async-await ×1
asynchronous ×1
bing-maps ×1
storagefile ×1
windows-8 ×1