C#實(shí)現(xiàn)多線程寫(xiě)入同一個(gè)文件的方法
更新時(shí)間:2015年08月22日 10:29:36 作者:我心依舊
這篇文章主要介紹了C#實(shí)現(xiàn)多線程寫(xiě)入同一個(gè)文件的方法,涉及C#多線程操作文件讀寫(xiě)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#實(shí)現(xiàn)多線程寫(xiě)入同一個(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();
}
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)通過(guò)ffmpeg從flv視頻文件中截圖的方法
這篇文章主要介紹了C#實(shí)現(xiàn)通過(guò)ffmpeg從flv視頻文件中截圖的方法,實(shí)例分析了C#使用ffmpeg操作flv文件的技巧,需要的朋友可以參考下2015-03-03
C#事件中的兩個(gè)參數(shù)詳解(object sender,EventArgs e)
這篇文章主要介紹了C#事件中的兩個(gè)參數(shù)詳解(object sender,EventArgs e),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Unity的IPreprocessBuildWithReport實(shí)用案例深入解析
這篇文章主要為大家介紹了Unity的IPreprocessBuildWithReport實(shí)用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
winform中的ListBox和ComboBox綁定數(shù)據(jù)用法實(shí)例
這篇文章主要介紹了winform中的ListBox和ComboBox綁定數(shù)據(jù)用法,實(shí)例分析了將集合數(shù)據(jù)綁定到ListBox和ComboBox控件的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12

