请大侠帮忙看看,这段代码存在着哪些潜在的问题。谢谢!
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Thread t1 = new Thread(Logger.Write);
                t1.Name = "t1";
                Thread t2 = new Thread(Logger.Write);
                t2.Name = "t2";                t1.Start();
                t2.Start();            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
    class Logger
    {
        const int C = 10;        public static void Write()
        {
            using (FileStream fs = new FileStream("c:\\a.txt", FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    int i = 0;                    while (i < C)
                    {
                       // 下行代码如果注释,可以运行,但不知道是否存在潜在的问题。 
                         //Thread.Sleep(100);
                        sw.WriteLine(Thread.CurrentThread.Name + "  --  " + DateTime.Now.ToLocalTime());
                        i++;
                    }
                }
            }
        }
    }

解决方案 »

  1.   

    "c:\\a.txt"运行没问题,只是调试的时候,会出现多外线程同时处抢资源的情况
      

  2.   

    注释掉可能出现问题,可能只有一个线程工作,另一个线程等待.因为CPU时间片轮询不过来.到时候你退出线程的时候就会有问题了!
      

  3.   

     try
                {
                    StreamReader TxtReader = new StreamReader(@"D:\Documents and Settings\Administrator\桌面\NewText1\1.txt", System.Text.Encoding.Default);
                    string strFileContent = TxtReader.ReadToEnd(); TxtReader.Close();                StreamWriter TxtWriter = new StreamWriter(@"D:\Documents and Settings\Administrator\桌面\NewText1\1.txt", true, System.Text.Encoding.Default);
                    TxtWriter.Write(strFileContent);//改写的对象
                    TxtWriter.Close();
                    MessageBox.Show("文件打开成功");
                }
                catch (Exception)
                {
                    
                    MessageBox.Show("文件打开失败");
                }
      

  4.   

    "c:\\a.txt"资源占用需要做控制;
    最好加锁。
      

  5.   

    严重问题
    你的StreamWriter sw 没有关闭啊,这很严重的
      

  6.   

    操作同一个文件,fs和sw对象要close()关键问题不是出在线程上吧,线程可以多个,但不能同时操作一个文件对象Lz试下这个是问题所在吗?因为我看你文件对象用后都没关闭...
      

  7.   

    用lock锁一下吧,
    或者有个类可以实现多线程同时读,单线程写入的功能
      

  8.   

    多个线程访问一个资源的时候,你一定要控制这个资源的同时,同时打开一个文件会不会出错我不知道,你可以写一个单线程的程序,在不关闭的情况下打开两次,估计会出错,也就是你的程序出错的原因。
    对于如何控制资源,我只有概念性的认识也没研究过,你可以查一些资料,不是信号量,邮箱,互斥体等等
    比如同时写文件应该有一定的规则吧,比如一替一行的写,你控制一个bool型变量,其中一个写的时候另一个设置为就绪状态。只能提供一下思路。
    另外,记得关闭资源。
      

  9.   

    sw需要close() 不是线程的问题,线程可以多个,不过不能同时操作一个文件 你这儿sw没有close()
      

  10.   

    我没显示写关闭是因为用了 using block
    这个能具体点吗?谢谢!
      

  11.   

    像这样:
    static class Logger
    {
        private const int C = 10;
        private static readonly object locker = new object();    public static void Write()
        {
            lock (locker)
            {
                using (FileStream fs = new FileStream("e:\\a.txt", FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        int i = 0;                    while (i < C)
                        {
                            Thread.Sleep(100);
                            sw.WriteLine(Thread.CurrentThread.Name + "  --  " + DateTime.Now.ToLocalTime());
                            i++;
                        }
                    }
                }
            }
        }
    }
      

  12.   

    不是 close() 的问题, using 中的语句自己最后会调用 dispose(), 不需要手动关闭
    个人认为是线程同步的问题, 文件被一个线程以写方式打开, 另一个写方式打开的尝试应该会被拒绝, 故报错
    简单点的方式是public static void Write()
    {
        static object l_obj = new object();
        lock (l_obj) {
            using (FileStream fs = new FileStream("c:\\a.txt", FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    int i = 0;                while (i < C)
                    {
                        // 下行代码如果注释,可以运行,但不知道是否存在潜在的问题。 
                        //Thread.Sleep(100);
                        sw.WriteLine(Thread.CurrentThread.Name + "  --  " + DateTime.Now.ToLocalTime());
                        i++;
                    }
                }
            }
        }
    }
      

  13.   

    写 log 的话, 我一般在程序初始化时就把 log 文件打开, 然后提供一个 WriteLog() 写该文件, WriteLog() 中用 lock 做简单的线程同步保证, 程序退出时关闭 log 文件
      

  14.   

    刚用 楼上的lock 貌似还有问题,在网上找到一个,经测试没有问题
    private Mutex mWriter = null;
    //你的方法
            if (mWriter == null) mWriter = new Mutex();
            mWriter.WaitOne();
            try
            {
                //your code
            }
            catch
            {
            }
            mWriter.ReleaseMutex();//
      

  15.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.IO;
    namespace ConsoleApplication7 {
    class Program {
    static void Main(string[] args) {
    try {
    Thread t1 = new Thread(Logger.Write);
    t1.Name = "t1";
    Thread t2 = new Thread(Logger.Write);
    t2.Name = "t2";
    Thread t3 = new Thread(Logger.Write);
    t3.Name = "t3"; t1.Start();
    t2.Start();
    t3.Start(); }
    catch (Exception ex) {
    Console.WriteLine(ex.Message);
    }
    }
    }
    class LoggerText:IDisposable
    {
    static private LoggerText _instance;
    static private object _lockObj;
    static private object _lockInstanceObj = new object();
    private FileStream _fs;
    private StreamWriter _sw;
    private LoggerText(){
     _fs = new FileStream("c:\\a.txt", FileMode.OpenOrCreate, FileAccess.Write);
     _sw = new StreamWriter(_fs);
    _lockObj = new object();
    }
    public void wirteIn(string text){
    lock(_lockObj)
    {
    _sw.WriteLine(text);
    _sw.Flush();
    }
    }
    static public LoggerText Instance(){
    lock (_lockInstanceObj) {
    if (_instance == null) {
    _instance = new LoggerText();
    }
    }
    return _instance;
    } #region IDisposable Members void IDisposable.Dispose() {
    _sw.Close();
    _fs.Close();
    } #endregion
    }
    class Logger {
    const int C = 1000;
    public static void Write() {
    int i = 0;
    while (i < C) {
    // 下行代码如果注释,可以运行,但不知道是否存在潜在的问题。 
    //Thread.Sleep(100);
    LoggerText.Instance().wirteIn(Thread.CurrentThread.Name + "  --  " + DateTime.Now.ToLocalTime() + "  --  " + i.ToString());
    i++;
    }
    }
    }
    }
    注意 单例LoggerText在应用开始时初始化(Instance)在应用结束时干掉(Dispose())
      

  16.   

    以上代码经测试 1000个线程并发运行,没有问题mian 函数改为static void Main(string[] args) {
    try {
    for (int i = 1; i < 100; i++) {
    Thread t = new Thread(Logger.Write);
    t.Name = "t"+i.ToString();
    t.Start();
    }
    }
    catch (Exception ex) {
    Console.WriteLine(ex.Message);
         }
    }