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

发布于 2024-10-10 06:27:53 字数 1346 浏览 0 评论 0原文

我正在开发一个项目,其中我正在实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

葬﹪忆之殇 2024-10-17 06:27:53

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
}

The Save() of XDocument can be implemented as:


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
}
送君千里 2024-10-17 06:27:53

最简单的方法是使用 .Net Thread

The simplest way is using .Net Thread

待天淡蓝洁白时 2024-10-17 06:27:53

看看这个上接受的答案 帖子。如果需要,您还可以使用 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

ゞ记忆︶ㄣ 2024-10-17 06:27:53

如果您使用的是 .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.

爱冒险 2024-10-17 06:27:53

我试图整合一个简单的实现。它未经测试。另外,不要使用Thread,看看是否可以在紧凑框架中使用ThreadPool线程。希望有帮助。

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);
    }
}

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. 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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文