0

I tried FileIO.WriteTextAsync(); with many events including Grid.Loaded and RichEditBox.TextChanged, but that method is asynchronous and conflicts with process called previous cycle. As a result, System.IO.FileLoadException with following description:

The process cannot access the file because it is being used by another process.

So what can I do in this case?

4

1 回答 1

1

事件TextChanged触发太频繁 - 您可以在一秒钟内键入多个字符 - 处理TextChanged事件对于 UI 线程来说太繁重了。

您可以在 Timer Tick 事件处理程序中执行此保存操作,设置足够长的时间间隔以确保操作在下一个 Tick 发生之前完成。

DispatcherTimer timer; 

public MainPage()
{
    this.InitializeComponent();

    timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(10) };
    timer.Tick += Timer_Tick;
    timer.Start();
}

private async void Timer_Tick(object sender, object e)
{
    await FileIO.WriteTextAsync(file, tbInput.Text);
}
于 2018-07-19T10:29:08.600 回答