国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

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

2020-01-24 01:30:57
字體:
供稿:網(wǎng)友

本文實(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 + "http://app//";   if (!System.IO.Directory.Exists(path))    System.IO.Directory.CreateDirectory(path);   path = path + "http://" + 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 + "http://app//";    if (!System.IO.Directory.Exists(path))     System.IO.Directory.CreateDirectory(path);    path = path + "http://" + 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();   }  } }}

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 商洛市| 济宁市| 无为县| 恩施市| 洛川县| 阿拉善左旗| 太谷县| 三门县| 库尔勒市| 时尚| 曲松县| 怀安县| 凌云县| 三亚市| 革吉县| 定结县| 饶平县| 昌邑市| 赤水市| 兖州市| 建平县| 拉萨市| 沁水县| 平远县| 庄浪县| 凌云县| 宣城市| 鲁山县| 天柱县| 古浪县| 黔南| 屏东县| 中西区| 丹寨县| 辉南县| 上犹县| 洛南县| 腾冲县| 兴和县| 铁力市| 建湖县|