1

I want to parse an xml file, serialize data and send it to clients. As you know parsing files may take a bit so i often get the exception:

[IOException: The process cannot access the file 'E:...\file.xml' because it is being used by another process.]

I decided to add a try/catch block to my action controller. If there is an exception, do some thread.sleep and try again. My new code looks like below:

public PartialViewResult _warningsView(string containerId)
{
    var myXslTrans = new XslCompiledTransform();
    FileStream fileStream = null;
    warnings result = null;
    bool isFileProcessed = false;
    while (!isFileProcessed)
    {
        try
        {
            myXslTrans.Load(Properties.Settings.Default.DataFolder + "/alert.xslt");
            myXslTrans.Transform(Properties.Settings.Default.DataFolder + "/alert.xml", Properties.Settings.Default.DataFolder + "/TransAlert.xml");
            XmlSerializer serializer = new XmlSerializer(typeof(warnings));
            fileStream = new FileStream(Properties.Settings.Default.DataFolder + "/TransAlert.xml", FileMode.Open);
            result = (warnings)serializer.Deserialize(fileStream);

            isFileProcessed = true;
        }
        catch
        {
            Thread.Sleep(100);
        }
        finally
        {
            if (fileStream != null)
            {
                fileStream.Dispose();
            }
        }
    }
      return new Ext.Net.MVC.PartialViewResult
            {
                RenderMode = RenderMode.AddTo,
                ContainerId = containerId,
                Model = result.warningList,
                WrapByScriptTag = false 
            };                
}

This solution worked for me but i think it will make my program slow. Is there a better way to do that?

8
  • 3
    Why is the file sometimes used by another process? I would investigate this rather than use Sleep() in the controller method. Commented Aug 20, 2014 at 15:12
  • As West said... or if you can't stop it being used for some valid reason, perhaps you could take a temp copy of the xml file and process that? i.e. don't use the filestream and perhaps load the whole file into memory if it isn't too big. Commented Aug 20, 2014 at 15:13
  • should use a memoryStream for the transform or a tempFile. Commented Aug 20, 2014 at 15:15
  • the _warningsView method is called by all clients at the some time. So the TransAlert.xml file bill be processed by many threads at the some time Commented Aug 20, 2014 at 15:16
  • does your xml file get updated? Is that when you get the error? Commented Aug 20, 2014 at 16:51

2 Answers 2

1

Thanks every body. I just found this solution: instead of creating new file with XslCompiledTransform, i will go for creating a temp file (unique file name each request) or a MemoryStream.

Sign up to request clarification or add additional context in comments.

Comments

0

Yep, sleeping while waiting for an opportunity to read will make your app very slow.

It looks like you're reading from the same file over and over again. You could add the file to your project as an embedded resource and read it that way. Since your operation is read-only it ought to work without exceptions, but may still be slow.

Check this out for more info down this rabbit-hole: http://social.msdn.microsoft.com/Forums/en-US/c2d5a464-5a04-4107-9fba-26ff5f8ef720/multiple-access-to-embedded-resources-inside-a-net-assembly?forum=netfxbcl

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.