keijikk
1/20/2016 - 1:19 AM

アクセス競合が起きやすいファイルを開く。

アクセス競合が起きやすいファイルを開く。

        public static Stream OpenWorkfileWithRetry( string filePath )
        {
            const int retryCountMax = 10;
            const int retryInterval = 100;

            FileStream fs = null;
            for (int retryCount = retryCountMax; retryCount > 0; retryCount--)
            {
                try
                {
                    fs = File.Open( filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read );
                    return fs;
                }
                catch (IOException)
                {
                    if (fs != null)
                    {
                        fs.Dispose();
                        fs = null;
                    }
                }
                Thread.Sleep( retryInterval );
            }
            return null;
        }