我正在编写一个连续的轮询循环来监视某些事件发生,然后在UI线程上采取一些操作.
我正在编写以下代码
public static void HandlePopup(this HostedControl control, string className, string caption, System.Action callback)
{
var popupTask = Task.Factory.StartNew(() =>
{
Thread.Sleep(5000); // just wait for 5 seconds.
}, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).ContinueWith((prevTask) =>
{
AutomationElementCollection collection = null;
do
{
} while (true);
}, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()).ContinueWith((prevTask) =>
{
if (!prevTask.IsFaulted)
{
if (control.InvokeRequired)
{
control.Invoke(callback);
}
else
{
callback();
}
}
}, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
try
{
////popupTask.Wait();
}
catch (AggregateException ex)
{
ex.Handle(exnew =>
{
return true;
}); …Run Code Online (Sandbox Code Playgroud) 我正在编写一个示例windows phone 8应用程序.我还安装了Async for .NET Framework 4,Silverlight 4和5以及Windows Phone的框架.
但是,等待一个方法不等待,我的mainpage.xaml加载试图访问尚未填充的属性.
这是我的代码.
public static ObservableCollection<Model.CatalogCategory> Products { get; set; }
private async void Application_Launching(object sender, LaunchingEventArgs e)
{
ApplicationViewModel vm = new ApplicationViewModel();
Products = await vm.LoadLocalDataAsync();
}
Run Code Online (Sandbox Code Playgroud)
在此方法之后,加载mainpage.xaml,它尝试访问"Products"并抛出空引用异常.
我需要采取不同的方法吗?