Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文件创建阻止UWP加载_C#_Xml_File_Uwp - Fatal编程技术网

C# 文件创建阻止UWP加载

C# 文件创建阻止UWP加载,c#,xml,file,uwp,C#,Xml,File,Uwp,UWP对我来说太过分了,很抱歉这个问题 为什么文件创建会阻止我的UWP加载 public static async Task CreateFile() { StorageFolder storageFolder = ApplicationData.Current.LocalFolder; var item = await ApplicationData.Current.LocalFolder.TryGetItemAsync("sFile.xml"); if (item =

UWP对我来说太过分了,很抱歉这个问题

为什么文件创建会阻止我的UWP加载

public static async Task CreateFile()
{
    StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    var item = await ApplicationData.Current.LocalFolder.TryGetItemAsync("sFile.xml");
    if (item == null)
    {
        StorageFile file = await storageFolder.CreateFileAsync("sFile.xml");
        await FileIO.WriteLinesAsync(file, GlobalVars.fileStrings);
    }
}
这个函数是从我的main方法调用的

public GroupedItemsPage()
{
    this.InitializeComponent();
    this.navigationHelper = new NavigationHelper(this);
    this.navigationHelper.LoadState += navigationHelper_LoadState;
    try
    {
        SampleDataSource.CreateFile().Wait();
        Debug.WriteLine("Continue");
    }
    catch (Exception ex)
    {
        var msg = new MessageDialog(ex.StackTrace);
        Task.Run(async () => { await msg.ShowAsync(); }).Wait();
        throw ex.InnerException;
    }
    GlobalVars.LastFreeSignalCheckTimer.Tick += SampleDataSource.LastFreeSignalCheckTimer_Tick;
    GlobalVars.LastFreeSignalCheckTimer.Interval = new TimeSpan(0, 0, 0, 120);
    GlobalVars.LastFreeSignalCheckTimer.Start();
}
CreateFile
函数永远不会返回。为什么呢

编辑:添加了主方法

public GroupedItemsPage()
{
    this.InitializeComponent();
    this.navigationHelper = new NavigationHelper(this);
    this.navigationHelper.LoadState += navigationHelper_LoadState;
    try
    {
        SampleDataSource.CreateFile().Wait();
        Debug.WriteLine("Continue");
    }
    catch (Exception ex)
    {
        var msg = new MessageDialog(ex.StackTrace);
        Task.Run(async () => { await msg.ShowAsync(); }).Wait();
        throw ex.InnerException;
    }
    GlobalVars.LastFreeSignalCheckTimer.Tick += SampleDataSource.LastFreeSignalCheckTimer_Tick;
    GlobalVars.LastFreeSignalCheckTimer.Interval = new TimeSpan(0, 0, 0, 120);
    GlobalVars.LastFreeSignalCheckTimer.Start();
}

我认为问题在于SampleDataSource.CreateFile().Wait()中的.Wait();使用wait SampleDataSource.CreateFile();相反

Wait()是一个阻塞操作,如果要使用它,必须小心,因为可以很容易地构建死锁。在大多数情况下,等待是更好的选择

在这种情况下,您将使用Wait()阻塞UI线程,以防止在同一线程中成功执行CreateFile()方法。相反,如果使用wait,代码应该可以工作,因为在等待时线程不会被阻塞,以便可以执行其他代码(如CreateFile())


另一种解决方案是使用Task.Run在后台执行该方法,这样您就可以使用wait()等待,因为该方法不是在同一个被阻止的线程中执行的(但阻止整个UI线程仍然是丑陋的代码设计)。

我认为问题在于SampleDataSource.CreateFile().wait()中的.wait();使用wait SampleDataSource.CreateFile();相反

Wait()是一个阻塞操作,如果要使用它,必须小心,因为可以很容易地构建死锁。在大多数情况下,等待是更好的选择

在这种情况下,您将使用Wait()阻塞UI线程,以防止在同一线程中成功执行CreateFile()方法。相反,如果使用wait,代码应该可以工作,因为在等待时线程不会被阻塞,以便可以执行其他代码(如CreateFile())


另一种解决方案是使用Task.Run在后台执行该方法,这样您就可以使用wait()等待,因为该方法不是在同一个被阻止的线程中执行的(但阻止整个UI线程仍然是难看的代码设计)。

您遇到了死锁。这就是为什么您永远不应该阻塞
async
code

页面
的构造函数中调用async
CreateFile
方法是一种不好的做法,而不是调用async
CreateFile
方法,您可以在页面初始化后通过处理页面的
加载
事件来调用它。然后您可以
等待
异步
方法,就像您应该的那样:

public sealed partial class GroupedItemsPage : Page
{
    public GroupedItemsPage()
    {
        this.InitializeComponent();
        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += navigationHelper_LoadState;
        this.Loaded += GroupedItemsPage_Loaded;
    }

    private async void GroupedItemsPage_Loaded(object sender, RoutedEventArgs e)
    {
        try
        {
            await SampleDataSource.CreateFile();
            Debug.WriteLine("Continue");
        }
        catch (Exception ex)
        {
            var msg = new MessageDialog(ex.StackTrace);
            await msg.ShowAsync();
            throw ex.InnerException;
        }
        GlobalVars.LastFreeSignalCheckTimer.Tick += SampleDataSource.LastFreeSignalCheckTimer_Tick;
        GlobalVars.LastFreeSignalCheckTimer.Interval = new TimeSpan(0, 0, 0, 120);
        GlobalVars.LastFreeSignalCheckTimer.Start();
    }
}

你正陷入僵局。这就是为什么您永远不应该阻塞
async
code

页面
的构造函数中调用async
CreateFile
方法是一种不好的做法,而不是调用async
CreateFile
方法,您可以在页面初始化后通过处理页面的
加载
事件来调用它。然后您可以
等待
异步
方法,就像您应该的那样:

public sealed partial class GroupedItemsPage : Page
{
    public GroupedItemsPage()
    {
        this.InitializeComponent();
        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += navigationHelper_LoadState;
        this.Loaded += GroupedItemsPage_Loaded;
    }

    private async void GroupedItemsPage_Loaded(object sender, RoutedEventArgs e)
    {
        try
        {
            await SampleDataSource.CreateFile();
            Debug.WriteLine("Continue");
        }
        catch (Exception ex)
        {
            var msg = new MessageDialog(ex.StackTrace);
            await msg.ShowAsync();
            throw ex.InnerException;
        }
        GlobalVars.LastFreeSignalCheckTimer.Tick += SampleDataSource.LastFreeSignalCheckTimer_Tick;
        GlobalVars.LastFreeSignalCheckTimer.Interval = new TimeSpan(0, 0, 0, 120);
        GlobalVars.LastFreeSignalCheckTimer.Start();
    }
}

你确定它永远不会回来吗?是否有异常发生?@DaveSmits添加了我的主要方法的一个示例。没有引发错误。当调用无限期阻塞时,请进入调试器并查看调用堆栈。这将允许您看到调用正在等待什么。顺便说一句,这看起来非常糟糕:
Task.Run(async()=>{await msg.ShowAsync();}).Wait()我建议您永远不要再使用用户等待方法。。那是杀戮加上等待你确定它永远不会回来吗?是否有异常发生?@DaveSmits添加了我的主要方法的一个示例。没有引发错误。当调用无限期阻塞时,请进入调试器并查看调用堆栈。这将允许您看到调用正在等待什么。顺便说一句,这看起来非常糟糕:
Task.Run(async()=>{await msg.ShowAsync();}).Wait()我建议您永远不要再使用用户等待方法。。这是致命的结合,但我不能在这里找到一个解决办法。它是同步方法中的异步调用。Run是丑陋的调用设计,等待()它阻止它。那么,我应该如何在同步方法中调用异步方法呢?不过,我在这里找不到解决方案。它是同步方法中的异步调用。Run是丑陋的调用设计,等待()它阻止它。那么,我应该如何在同步方法中调用异步方法呢?我考虑过,但有一些疑问,这就是为什么我甚至没有测试它。干得好,先生。我想了想,但有点怀疑,这就是为什么我没有测试它。干得好,先生。