无法在未处理的异常上发布文件

时间:2015-12-30 08:15:15

标签: winforms visual-studio c#-4.0 file-io unhandled-exception

SCENARIO

我正在开发文件同步应用程序,我们正在比较文件并将文件从本地计算机(也称为Station)上传到远程计算机(即服务器)。为此,我们有两个文件

  1. 的manifest.xml
  2. ncsync.xml
  3. 此外,我们在各种平板电脑上运行多个500+客户端。在上传文件时,我锁定这两个文件以保持客户端/站/服务器之间的一致性。发布后,文件将被释放。这是我的代码。 Lock.cs

    /**************************************************************\
     * Author       : Amit 
     * File         : Lock.cs
     * CreationDate : 09/05/2015 PST
     * Description  : Locks file to be updated, so that operations are not aborted in middle if 
     *                some file is open and cannot be updated.
     *
     *  
     * 
     \**************************************************************/
    
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Windows.Forms;
    
    namespace SharedLibraries
    {
        /// <summary>
        ///     Lock before any write operation to Publishing Point and Client updating the Manuals Directory.
        ///     That all the files you need are are writable. So you do not get into the position of File in Use scenario
        ///     in the middle of your writes. Best to check right at the beginning.
        ///     eg Publishing or on Client side MoveCacheToManual()
        ///     Usage:
        ///     Prepare your objects
        ///     NCSyncLock nsl = new NCSyncLock(ListOfFilesToWrite)
        ///     nsl.Lock() --- Insures everything is ready.
        ///     prepare - eg move things to cache
        ///     nsl.UnLock() ----
        ///     //Do the write
        /// </summary>
        public class NCSyncLock :IDisposable
        {
            private bool bLocked;
            private readonly List<string> _files;
            private FileStream[] streams = null;
    
            public NCSyncLock() { _files = new List<string>(); }
    
            public NCSyncLock(string DestDir, List<string> files)
            {
                _files = new List<string>();
                streams= new FileStream[2];
                foreach (var item in files)
                {
                    AddFile(DestDir, item);
                }
                Lock();
            }
    
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="DestDir"></param>
            /// <param name="file"></param>
            public void AddFile(string DestDir, string file)
            {
                _files.Add(Common.GetFullFileName(DestDir, file));
            }
    
            /// <summary>
            /// 
            /// </summary>
            public void Lock()
            {
                var count = _files.Count;
                int i;
                if (!bLocked)
                {
                    for (i = 0; i <count ; i++)
                    {
    
                        try
                        {
                            streams[i] = new FileStream( _files[i],FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
                        }
                        catch (IOException ex)
                        {
                            if (ex.Message.Contains("The network path was not found."))
                            {
                                MessageBox.Show(ex.Message, NcConstants.EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
    
                            }
                            else
                            {
                                var msg = string.Format("WARNING:In order to proceed, please close file {0} and click OK.", _files[i]);
                                MessageBox.Show(msg, NcConstants.EXCEPTION, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
    
                            }
                            // Displace Diaglog box to either cancel or close the file.
    
    
                        }
                    }
                    bLocked = true;
                }
    
            }
    
    
    
    
    
              #region IDISPOSABLE
    
            private bool _disposed;
    
            //Dispose method
            public void Dispose()
            {
                if (!_disposed)
                {
                    if (streams != null)
                    {
                        for (int i = 0; i < streams.Length; i++)
                        {
                            streams[i].Close();
                            streams[i].Dispose();
                        }
                    }
                    _disposed = true;
                }
                GC.SuppressFinalize(this);
            }
    
            //destructor
            ~NCSyncLock()
            {
                Dispose();
            }
    
            #endregion
        }
    }
    

    其用法

     NCSyncLock nsl = new NCSyncLock(ListOfFilesToWrite);
        nsl.Lock(); --- Insures everything is ready.
        ///    Code for fileTransfer
        nsl.Dispose();
    

    ISSUE

    根据代码首次发布之后,锁必须被释放,因为我正在调用Dispose(),但即使之后,使用Dispose(),当我进行下一次发布时,没有关闭我的应用程序,它说,显示。 xml正在被另一个进程使用,并且对于ncsync.xml也是如此。 我想释放这两个人之后被锁定:

    1. 每次成功转移。
    2. 未处理的例外,

0 个答案:

没有答案