简体   繁体   English

如何在C#中为save方法实现异步模式?

[英]How can I implement asynchronous pattern for save method in C#?

I am working on a project where I am implementing a SaveSettings method which saves lot of settings to a xml file. 我正在一个项目中,我正在实现一个SaveSettings方法,该方法将许多设置保存到xml文件中。

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. 问题是这样做需要花费时间,这就是为什么当我在窗体上单击“保存”按钮时,UI会挂起/停止一会儿。

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. 我想让SaveSettings方法异步进行BeginSave / EndSave,以便在我调用BeginSave时,UI可以顺利进行。 I have no BackgroundWorker as I am using .Net Compact Framework. 我没有使用.Net Compact Framework的BackgroundWorker。

Any guidance on implementing Asynchronous pattern please... 有关实现异步模式的任何指导,请...

The Save() of XDocument can be implemented as: XDocumentSave()可以实现为:


public void Save(string xmlFilePath)
{
    Thread thread = new Thread(new ParameterizedThreadStart(SaveSettings));
    thread.Start(xmlFilePath);
}

private void SaveSettings(object data)
{
    string xmlFilePath;
    if ((xmlFilePath = data as string) != null)
    {
        this.SaveSettingsFile(xmlFilePath);
    }
}

private void SaveSettingsFile(string xmlFilePath)
{ 
    // Save the file contents
}

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. 如果需要,您还可以使用反射器并获取BackgroundWorker类的代码。 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 在MSDN上也有关于此的文章: Microsoft .NET Compact Framework背景处理技术

最简单的方法是使用.Net线程

If you're on .Net 4 (or newer), consider using Tasks . 如果您使用的是.Net 4(或更高版本),请考虑使用Tasks They're an easier way to deal with asynchronous behavior that you're spinning up. 它们是处理正在旋转的异步行为的更简单方法。

I have tried to put together a simplistic implementation. 我试图将其简化。 It is untested. 未经测试。 Also instead of using a Thread see if you can use a ThreadPool thread in the compact framework. 另外,也可以使用紧凑型框架中的ThreadPool线程代替使用Thread Hope it helps. 希望能帮助到你。

public class SettingsType {}

public class Settings
{
    private Thread _worker;

    public void SaveSettings(SettingsType type)
    {
        // save logic
    }

    public void BeginSaveSettings(SettingsType type)
    {
        _worker = new Thread(() => SaveSettings(type)) {IsBackground = true};
        _worker.Start();
    }

    public bool EndSaveSettings(TimeSpan timeOut)
    {
        return _worker.Join(timeOut);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM