本帖最后由 rija0026 于 2010-12-02 12:23:44 编辑

解决方案 »

  1.   

    简单的生产者,消费者同步控制,通过Event进行同步读写控制
    public class DataObject
        {
            public long IntValue { get; set; }
            public string StrValue { get; set; }
        }    class Program
        {
            static AutoResetEvent writeEvent = new AutoResetEvent(true);
            static AutoResetEvent readEvent = new AutoResetEvent(false);        static DataObject dataObject = new DataObject();
            static void Main()
            {
                //Write Thread
                ThreadPool.QueueUserWorkItem(status =>
                    {
                        while (true)
                        {
                            if (writeEvent.WaitOne(1))
                            {
                                dataObject.IntValue = DateTime.Now.Ticks;
                                dataObject.StrValue = Guid.NewGuid().ToString();
                                Console.WriteLine("Write:{0},{1}", dataObject.IntValue, dataObject.StrValue);
                                readEvent.Set();
                            }
                        }
                    }, null);
                //Read Thread
                ThreadPool.QueueUserWorkItem(status =>
                {
                    while (true)
                    {
                        if (readEvent.WaitOne(1))
                        {
                            Console.WriteLine("Read:{0},{1}", dataObject.IntValue, dataObject.StrValue);
                            writeEvent.Set();
                        }
                    }
                }, null);            Console.Read();
            }
        }
    你也可以使用系统提供的ReaderWriterLock类去实现类似的功能