我有一个实时数据,不停地更新到一个xml文件中,另有一个程序,又实时地从这个xml文件中读数据,如何做到两者不会因为进程而发生错误呢,即如何在一个读时,写的可以暂停一下,等读完再更新。

解决方案 »

  1.   

    屁都锁不上。
    看看 StreamWriter 类。。 again:
    try
    {
    StreamWriter sw = new StreamWriter(ClientPrefix + r["ChannelDirectoryName"].ToString()+"\\Cengross.txt",true);
    this.CreateCatalog(channelId,r["ChannelDirectoryName"].ToString());
    sw.Close();
    }
    catch
    {
    System.Threading.Thread.Sleep(100);
    goto again;
    }
      

  2.   

    Use "Mutex" class, sample code as follows:
    private Mutex mUnique = null;//In function to write data
    if( mUnique == null ) mUnique = new Mutex();
    mUnique.WaitOne();//Write data heremUnique.ReleaseMutex();//In function to read data
    if( mUnique == null ) mUnique = new Mutex();
    mUnique.WaitOne();//Read data heremUnique.ReleaseMutex();
      

  3.   

    楼上的,楼主是两个程序,mutex好用嘛????
      

  4.   

    Sample code for two programs using "Mutex" class
    Mutex mUnique = new Mutex( false, UniqueString );
    mUnique.WaitOne();//Read or write data heremUnique.ReleaseMutex();