zoukankan      html  css  js  c++  java
  • 正由另一进程使用,因此该进程无法访问该文件

    /// <summary>
            /// 写入日志
            /// </summary>
            /// <param name="log"></param>
            private void WriteLog(string log)
            {
                //edit at 2012.10.17 改成无锁异步写如日志文件
                using (FileStream fs = new FileStream(DataLogFile, FileMode.Append, FileAccess.Write, FileShare.Write, 1024, FileOptions.Asynchronous))
                {
                    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(log+"\r\n");
                    IAsyncResult writeResult = fs.BeginWrite(buffer, 0, buffer.Length,
                        (asyncResult) =>
                        {
                            FileStream fStream = (FileStream)asyncResult.AsyncState;
                            fStream.EndWrite(asyncResult);
                        },
                        fs);
                    //fs.EndWrite(writeResult);//这种方法异步起不到效果
                    fs.Close();
                }
     
                //lock (lockObj)
                //{
     
                //    //using (StreamWriter sw = File.AppendText(DataLogFile))
                //    //{
     
                //    //    sw.WriteLine(log);
                //    //    sw.Flush();
                //    //    sw.Close();
                //    //}
                //}
            }class Program

    {
        static void Main(string[] args)
        {
            ThreadPool.QueueUserWorkItem(run);
            ThreadPool.QueueUserWorkItem(run);
            Console.ReadLine();
        }
        private static void run(object state)
        {
            while (true)
            {
                Append();
            }
        }
        public static void Append()
        {
            try
            {
                //lock (typeof(Program))
                //{
                    using (var writer = File.AppendText("1.txt"))
                    {
                        writer.Write(DateTime.Now.ToString("HHmmss"));
                    }
                    Console.WriteLine("Done");
                //}
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }           
        }
    }
            static System.Threading.Semaphore _mutex = new System.Threading.Semaphore(11"IOHelper.Save");
            private static bool Save(string fileName, string text, bool isAppend)
            {
                try
                {
                    if (_mutex.WaitOne(2000false))//进程间同步。
                    {
                        using (StreamWriter writer = new StreamWriter(fileName, isAppend))
                        {
                            writer.Write(text);
                        }
                    }
                    return true;
                }
                catch (Exception err)
                {
                    Log.WriteLogToTxt(err);
                }
                finally
                {
                    try
                    {
                        _mutex.Release();
                    }
                    catch
                    {

                    }
                }
                return false;
            }
     
     
    http://www.cnblogs.com/cyq1162/archive/2013/03/29/2988035.html
  • 相关阅读:
    sqlmap注入分类
    sqlmap简单中文说明
    【Python Learning第一篇】Linux命令学习及Vim命令的使用
    模拟退火算法从原理到实战【基础篇】
    平面上给定n条线段,找出一个点,使这个点到这n条线段的距离和最小。
    使用VMWareWorkstation10搭建学习环境笔记
    洛谷P1313 计算系数【快速幂+dp】
    浅析Numpy.genfromtxt及File I/O讲解
    持续交付中高效率与高质量
    持续集成CI与自动化测试
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2989900.html
Copyright © 2011-2022 走看看