如何在 C# 中实现 save 方法的异步模式?
我正在开发一个项目,其中我正在实现 SaveSettings 方法,该方法将大量设置保存到 xml 文件中。
问题是这需要时间,这就是为什么当我单击表单上的“保存”按钮时,我的 UI 只是挂起/停止一段时间。
该方法如下所示,
public void SaveSettings(SettingsType settingsType)
{
if (!File.Exists(_settingsFile))
{
File.Create(_settingsFile);
}
var xmlDoc = XDocument.Load(_settingsFile);
switch (settingsType)
{
case SettingsType.Measurement:
SaveMeasurementSettings(ref xmlDoc);
break;
case SettingsType.Display:
SaveDisplaySettings(ref xmlDoc);
break;
case SettingsType.Common:
SaveCommonSettings(ref xmlDoc);
break;
case SettingsType.View:
SaveViewSettings(ref xmlDoc);
break;
case SettingsType.InputChannel:
SaveInputChannelSettings(ref xmlDoc);
break;
default:
break;
}
xmlDoc.Save(_settingsFile);
}
我想让 SaveSettings 方法与 BeginSave/EndSave 异步,这样当我调用 BeginSave 时,我的 UI 应该能够顺利进行。我没有BackgroundWorker,因为我使用的是.Net Compact Framework。
有关实现异步模式的任何指导,请...
I am working on a project where I am implementing a SaveSettings method which saves lot of settings to a xml file.
Problem is it takes time to do that, that's why when I click Save button on my form my UI just hangs/stops for a while.
The method looks like below
public void SaveSettings(SettingsType settingsType)
{
if (!File.Exists(_settingsFile))
{
File.Create(_settingsFile);
}
var xmlDoc = XDocument.Load(_settingsFile);
switch (settingsType)
{
case SettingsType.Measurement:
SaveMeasurementSettings(ref xmlDoc);
break;
case SettingsType.Display:
SaveDisplaySettings(ref xmlDoc);
break;
case SettingsType.Common:
SaveCommonSettings(ref xmlDoc);
break;
case SettingsType.View:
SaveViewSettings(ref xmlDoc);
break;
case SettingsType.InputChannel:
SaveInputChannelSettings(ref xmlDoc);
break;
default:
break;
}
xmlDoc.Save(_settingsFile);
}
I want to make SaveSettings method asynchronous something BeginSave/EndSave so that when I call BeginSave my UI should go smooth. I have no BackgroundWorker as I am using .Net Compact Framework.
Any guidance on implementing Asynchronous pattern please...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
XDocument
的Save()
可以实现为:The
Save()
ofXDocument
can be implemented as:最简单的方法是使用 .Net Thread
The simplest way is using .Net Thread
看看这个上接受的答案 帖子。如果需要,您还可以使用 Reflector 并获取 BackgroundWorker 类的代码。 这里是一个可以帮助您入门的实现。
MSDN 上还有一篇关于此的文章:Microsoft .NET Compact Framework 后台处理技术
Take a look at the accepted answer on this post. You could also use reflector and grab the code for the BackgroundWorker class if you wanted. Here is an implementation of it to get you started.
There is also an article on MSDN about this: Microsoft .NET Compact Framework Background Processing Techniques
如果您使用的是 .Net 4(或更高版本),请考虑使用 任务。它们是处理您正在启动的异步行为的更简单的方法。
If you're on .Net 4 (or newer), consider using Tasks. They're an easier way to deal with asynchronous behavior that you're spinning up.
我试图整合一个简单的实现。它未经测试。另外,不要使用
Thread
,看看是否可以在紧凑框架中使用ThreadPool
线程。希望有帮助。I have tried to put together a simplistic implementation. It is untested. Also instead of using a
Thread
see if you can use aThreadPool
thread in the compact framework. Hope it helps.