欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法

 更新時(shí)間:2015年08月22日 10:29:36   作者:我心依舊  
這篇文章主要介紹了C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法,涉及C#多線程操作文件讀寫的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

namespace WfpApp
{
 public partial class Form2 : Form
 {
  object obj = new object();
  public Form2()
  {
   InitializeComponent();
   System.Threading.Thread thread;
   string[] users = new string[] { "zkk", "admin", "administrator", "soft", "iany", "nec", "necsl" };
   for (int i = 0; i < users.Length; i++)
   {
    thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(WriteLock));
    thread.Start(users[i]);
   }
  }
  public void WriteThread(object user)
  {
   string path = System.Windows.Forms.Application.StartupPath + "\\app\\";
   if (!System.IO.Directory.Exists(path))
    System.IO.Directory.CreateDirectory(path);
   path = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
   StringBuilder sb = new StringBuilder();
   sb.AppendLine("----------------------------" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--------------------------");
   sb.AppendLine(user.ToString());
   sb.AppendLine("---------------------------------------------------------------------------------");
   sb.AppendLine();
   //if (!System.IO.File.Exists(path))
   // System.IO.File.Create(path).Close();
   System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
   System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream, Encoding.Default);
   sw.Write(sb.ToString());
   sw.Close();
   sw.Dispose();
   fileStream.Close();
   fileStream.Dispose();
  }
  public void WriteLock(object user)
  {
   lock (obj)
   {
    string path = System.Windows.Forms.Application.StartupPath + "\\app\\";
    if (!System.IO.Directory.Exists(path))
     System.IO.Directory.CreateDirectory(path);
    path = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("----------------------------" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--------------------------");
    sb.AppendLine(user.ToString());
    sb.AppendLine("---------------------------------------------------------------------------------");
    sb.AppendLine();
    if (!System.IO.File.Exists(path))
     System.IO.File.Create(path).Close();
    System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
    System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream, Encoding.Default);
    sw.Write(sb.ToString());
    sw.Close();
    sw.Dispose();
    fileStream.Close();
    fileStream.Dispose();
   }
  }
 }
}

希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Unity shader實(shí)現(xiàn)高斯模糊效果

    Unity shader實(shí)現(xiàn)高斯模糊效果

    這篇文章主要為大家詳細(xì)介紹了Unity shader實(shí)現(xiàn)高斯模糊效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C#緩存之SqlCacheDependency用法實(shí)例總結(jié)

    C#緩存之SqlCacheDependency用法實(shí)例總結(jié)

    這篇文章主要介紹了C#緩存之SqlCacheDependency用法,在C#程序設(shè)計(jì)中有一定的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-08-08
  • C#使用AutoResetEvent實(shí)現(xiàn)同步

    C#使用AutoResetEvent實(shí)現(xiàn)同步

    這篇文章主要為大家詳細(xì)介紹了C#使用AutoResetEvent實(shí)現(xiàn)同步的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • c# 空合并運(yùn)算符“??”的使用詳解

    c# 空合并運(yùn)算符“??”的使用詳解

    本篇文章是對c#中空合并運(yùn)算符“??”的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Winform中GridView分組排序功能實(shí)現(xiàn)方法

    Winform中GridView分組排序功能實(shí)現(xiàn)方法

    這篇文章主要介紹了Winform中GridView分組排序功能實(shí)現(xiàn)方法,以實(shí)例形式詳細(xì)說明了分組排序的實(shí)現(xiàn)方法,并附帶完整的DBHelp類代碼,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-11-11
  • C#同步和異步調(diào)用方法實(shí)例

    C#同步和異步調(diào)用方法實(shí)例

    c#同步和異步很簡單,這里給大家提供一個(gè)小例子供大家參考
    2013-11-11
  • C# 9 新特性——record的相關(guān)總結(jié)

    C# 9 新特性——record的相關(guān)總結(jié)

    這篇文章主要介紹了C# 9 新特性——record的相關(guān)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用c# 9的新特性,感興趣的朋友可以了解下
    2021-02-02
  • 使用C#開發(fā)ActiveX控件

    使用C#開發(fā)ActiveX控件

    activex控件以前也叫做ole控件,它是微軟ie支持的一種軟件組件或?qū)ο?,可以將其插入到web頁面中,實(shí)現(xiàn)在瀏覽器端執(zhí)行動(dòng)態(tài)程序功能,以增強(qiáng)瀏覽器端的動(dòng)態(tài)處理能力。通常activex控件都是用c++或vb語言開發(fā),本文介紹另一種方式,使用c#語言開發(fā)activex控件。
    2017-02-02
  • C#圖像對比度調(diào)整的方法

    C#圖像對比度調(diào)整的方法

    這篇文章主要介紹了C#圖像對比度調(diào)整的方法,涉及C#實(shí)現(xiàn)圖像對比度操作的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • C#字體池技術(shù)實(shí)現(xiàn)代碼詳解

    C#字體池技術(shù)實(shí)現(xiàn)代碼詳解

    在本篇文章里小編給大家整理的是關(guān)于C#字體池技術(shù)實(shí)現(xiàn)代碼詳解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2019-11-11

最新評論