页面加载期间的文件读取异步导致死锁。 [英] File Read Async during Page Load results in a deadlock.

查看:80
本文介绍了页面加载期间的文件读取异步导致死锁。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,


在我的环球应用程序中,我正在读取MainPage加载时的大数据结构,因为我需要在主页中填充一些数据,其中包含来自文件。


仅供参考,当我读取和写入文件并存储在appdata中时,我将数据序列化,您可以从我的代码片段中看到。但是,我在Windows应用程序中看到有些时候,我看到页面加载永远挂起,而在我的Windows Phone应用程序中,它根本不起作用。


我可能错了,但我有点怀疑它可能是来自ReadContent()和相互之间表现不佳的UI的等待。在Windows上它有90%的时间可以工作,但是在Windows手机中,它从来没有工作过。


任何关于如何更好地处理这种情况的想法都会非常有帮助和赞赏。

 

private void Page_Loaded(object sender,RoutedEventArgs e)

{
尝试
{
if((Application.Current as App).ApplicationDataSource == null)
{
ApplicationUtilities.LoadDataFromDataSource();
}

this.lstMatches.ItemsSource =(Application.Current as App).ApplicationDataSource.Matches;
this.lstTeams.ItemsSource =(Application.Current as App).ApplicationDataSource.Teams;
}

catch(exception ex)
{
string exception = ex.Message.ToString();
抛出前;
}
}
}

public static void LoadDataFromDataSource()
{
try
{
UltimateCricketScorerDataSource dataSource = new UltimateCricketScorerDataSource();
任务< ReadResults> loadResults = ReadContent< UltimateCricketScorerDataSource>(myKey,dataSource.GetType());

if(loadResults.Result!= null&& loadResults.Result.Success == true)
{
dataSource = loadResults.Result.Result as UltimateCricketScorerDataSource;
(Application.Current as App).ApplicationDataSource = dataSource;
}
其他
{
//找不到档案。第一次创建它。
ApplicationUtilities.CreateFileOnFirstLoad();
}

//返回loadResults;
}
catch(Exception ex)
{
string excep = ex.ToString();
throw;
}
}
private static async任务< ReadResults> ReadContent< type>(字符串键,类型t)
{
var rr = new ReadResults();

尝试
{
var ms = new MemoryStream();
DataContractSerializer serializer = new DataContractSerializer(t);

StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(Key);

using(IInputStream inStream = await file.OpenSequentialReadAsync())
{
rr.Result =(type)serializer.ReadObject(inStream.AsStreamForRead());
}

rr.Success = true;
}
catch(FileNotFoundException)
{
rr.Success = false;
}
返回rr;
}

Ruhina Taj

解决方案

您好Ruhina,


请尝试以可读方式格式化您的帖子:将代码放在代码块中,但不是文本。我这次为您解决了这个问题,并将您的帖子移到了一个更合适的论坛(您在HTML / JavaScript论坛上发布)。


您在加载数据时绝对没有正确使用等待我不希望你发布的代码得到正确的结果。 LoadDataFromDataSource调用ReadContent,但在尝试使用结果之前不等待ReadContent的任务完成。
如果ReadContent在LoadDataFromDataSource检查loadResult.Result并且如果ReadContent尚未完成时挂起,那么我怀疑一切正常。如果ReadContent已经完成,那么一切都正常。


相反,LoadDataFromDataSource应该被标记为异步任务,应该等待对ReadContents的调用。这种方式在ReadContents调用完成之前不会继续,并且通过返回Task,LoadDataFromDataSource调用者可以等待LoadDataFromDataSource
完成。


Hi experts,

In my Universal app, I am reading a big data structure upon MainPage load as I need to fill some data in my MainPage with the contents from the file.

FYI, I serialize my data when I read and write to a file and store in appdata as you can see that from my code snippet. However, I see that some times in Windows app, I see the Page load hangs forever and in my Windows Phone App, it never works at all.

I could be wrong but I kind of suspect that it could be the await from the ReadContent() and the UI that is not behaving well with each other. On windows it works like 90% of the time, but in Windows phone, it never worked.

Any idea on how to handle this case better would be very helpful and appreciated.

     

private void Page_Loaded(object sender, RoutedEventArgs e)

{ try { if ((Application.Current as App).ApplicationDataSource == null) { ApplicationUtilities.LoadDataFromDataSource(); } this.lstMatches.ItemsSource = (Application.Current as App).ApplicationDataSource.Matches; this.lstTeams.ItemsSource = (Application.Current as App).ApplicationDataSource.Teams; } catch (Exception ex) { string exception = ex.Message.ToString(); throw ex; } } } public static void LoadDataFromDataSource() { try { UltimateCricketScorerDataSource dataSource = new UltimateCricketScorerDataSource(); Task<ReadResults> loadResults = ReadContent<UltimateCricketScorerDataSource>(myKey, dataSource.GetType()); if (loadResults.Result != null && loadResults.Result.Success == true) { dataSource = loadResults.Result.Result as UltimateCricketScorerDataSource; (Application.Current as App).ApplicationDataSource = dataSource; } else { //File Not Found. Create it for the first time. ApplicationUtilities.CreateFileOnFirstLoad(); } //return loadResults; } catch (Exception ex) { string excep = ex.ToString(); throw; } } private static async Task<ReadResults> ReadContent<type>(string Key, Type t) { var rr = new ReadResults(); try { var ms = new MemoryStream(); DataContractSerializer serializer = new DataContractSerializer(t); StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(Key); using (IInputStream inStream = await file.OpenSequentialReadAsync()) { rr.Result = (type)serializer.ReadObject(inStream.AsStreamForRead()); } rr.Success = true; } catch (FileNotFoundException) { rr.Success = false; } return rr; }

Ruhina Taj

解决方案

Hi Ruhina,

Please try to format your post readably: put the code in a code block, but not the text. I fixed that for you this time and moved your post to a more appropriate forum (you posted in the HTML/JavaScript forum).

You are definitly not using await properly when loading the data and I would not expect correct results from the code you posted. LoadDataFromDataSource calls ReadContent but then doesn't wait for ReadContent's task to finish before to tries to use the result. I suspect that everything works correctly if the timing happens to work out that ReadContent has finished before LoadDataFromDataSource checks loadResult.Result and hangs if ReadContent has not finished.

Instead, LoadDataFromDataSource should be marked async Task and should await the call to ReadContents. This way it won't continue on until the ReadContents call has finished, and by returning a Task the LoadDataFromDataSource caller can wait for LoadDataFromDataSource to finish.


这篇关于页面加载期间的文件读取异步导致死锁。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆