zoukankan      html  css  js  c++  java
  • 多线程写文件异常(正由另一进程使用,因此该进程无法访问该文件)的解决方法

    正由另一进程使用,因此该进程无法访问该文件。
       在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
       在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)

    这是由于多线程出现互斥,一个文件没关闭,继续写入数据流而产生的异常。

    经过lock加锁没能解决问题,后来觉得用互斥量比较不错,经测试问题解决,异常通知没有了。

    view plaincopy to clipboardprint?
    01.namespace filewrite  
    02.{  
    03.    public partial class Form1 : Form  
    04.    {  
    05.        Mutex mtx = new Mutex();  
    06.        public Form1()  
    07.        {  
    08.            InitializeComponent();  
    09.        }  
    10. 
    11.        private void button1_Click(object sender, EventArgs e)  
    12.        {  
    13.            for (int i = 0; i < 30; i++)  
    14.            {  
    15.                Thread checkclientOnline = new Thread(checkOnline);  
    16.                checkclientOnline.Start();  
    17.            }  
    18. 
    19.        }  
    20.        //写入日志  
    21.        public static void WriteToLog1(string Title)  
    22.        {  
    23.              
    24. 
    25.            if (Title == "") return;  
    26.            string FileName = Application.StartupPath;  
    27.            //Object thisLock = new Object();  
    28.              
    29.            {  
    30.                FileStream fs = new FileStream(FileName + "http://www.cnblogs.com/itecho/admin/file://tiplog.txt/", FileMode.Append, FileAccess.Write, FileShare.Write);  
    31.                Byte[] bTitle = UnicodeToMBCS(Title);  
    32.                fs.Write(bTitle, 0, bTitle.Length);  
    33.                fs.Close();  
    34.            }  
    35.        }  
    36. 
    37.        public static Byte[] UnicodeToMBCS(String src)  
    38.        {  
    39.            Encoding enc = Encoding.GetEncoding(936); ////Dont use codepage 52936, but 54936 or 936  
    40.            int len = src.Length;  
    41. 
    42.            Byte[] tmpb = new Byte[len * 2];  
    43. 
    44.            tmpb = enc.GetBytes(src);  
    45. 
    46.            //string tmphead=tmpb.Length.ToString();  
    47.            //tmphead=tmphead.PadLeft(4,'0');             
    48. 
    49.            //tmpb=enc.GetBytes(tmphead+src);  
    50.            return tmpb;  
    51.        }  
    52.        void checkOnline()  
    53.        {  
    54.            while (true)  
    55.            {  
    56.                try 
    57.                {  
    58.                      
    59.                    mtx.WaitOne();  
    60.                    //Write file here  
    61.                    WriteToLog1("hhh");  
    62.                    mtx.ReleaseMutex();  
    63.                   ;  
    64.                }catch(Exception e)  
    65.                {  
    66.                    Console.WriteLine(e.ToString());  
    67.                }  
    68.            }  
    69.        }  
    70.    }  
    71.} 


     

  • 相关阅读:
    S5pv210 android VGA 1440*900 视频播放/3D 演示效果实拍视频
    毕业3年,工资从5k到20k的经历——真的还是假的啊?
    转载.WinCE6.0 Camera驱动整体结构
    半夜来认识一下S5PV210 的VBPDE和VFPDE
    微软的windows 8授权真的要这么贵?谁要?!
    基于S5PC100的FIMC的部分解释——一篇让我理解透彻2440和S5PV210 摄像头camera控制器的文章
    转.Buffer Management by the Camera Driver (Windows Embedded CE 6.0)
    微软?想干掉苹果,自己做平板,还想做手机,有优秀的合作伙伴诺基亚?简直是笑话,难怪鲍ceo 被评为最应该下台的CEO了
    魅族经过M8——》M9——》MX 已经走向国际?
    wince6.0 S5pv210 之Sate210 VGA 镜像1440*900@60HZ/1280*1024@70HZ分辨率测试镜像(南嵌电子科技作品)
  • 原文地址:https://www.cnblogs.com/itecho/p/1823428.html
Copyright © 2011-2022 走看看