开发者

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

开发者 https://www.devze.com 2023-02-02 09:15 出处:网络
I am working on a project where I am implementing a SaveSetti开发者_开发百科ngs method which saves lot of settings to a xml file.

I am working on a project where I am implementing a SaveSetti开发者_开发百科ngs 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...


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
}


The simplest way is using .Net Thread


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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消