下面的程序使用命名管道技术,定时器触发时就会将管道关闭,然后新建这个类对象,这个类对象重新建立了管道,但是不知道怎么回事,第一触发事件没有出错,第二次触发就会报管道已经存在的错误。我明明是在新建这个对象时,已经释放掉管道了啊,怎么还会报这个错呢?并且为什么第一次触发的事件就不会报错???
希望好心人帮忙看一下程序。。
using System;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using System.Security.Principal;
using System.Timers;
using System.Diagnostics;  namespace RestartToolManagement
{    public class ToolManage
    {
        static System.Timers.Timer Timer1 = new System.Timers.Timer(15000);        System.Diagnostics.ProcessStartInfo StartInfo = new ProcessStartInfo();        
        
        NamedPipeServerStream pipeServer =
                  new NamedPipeServerStream("testpipe", PipeDirection.InOut);        StreamWriter sw;
        StreamReader sr;
        //StreamReader sr;
        //static int count = 0;
       
        public void run()
        {
            ///启动工具
            ///
           // count++;
            StartInfo.FileName = "NewTools.exe";
            StartInfo.WorkingDirectory = @"E:\vs2008\NewTools\NewTools\bin\debug";
            System.Diagnostics.Process Proc = new Process();            try
            {
                // 启动外部程序   
                Proc = System.Diagnostics.Process.Start(StartInfo);
            }
            catch (System.ComponentModel.Win32Exception e)
            {
                Console.WriteLine("系统找不到指定的程序文件。\r{0}", e);
                return;
            }
            ///建立两个管道流
            ///
             sw = new StreamWriter(pipeServer);
             sr = new StreamReader(pipeServer);         
      
            Console.WriteLine("NamedPipeServerStream object created.");
                        // 等待客户端的连接
            Console.Write("Waiting for client connection...");
            //if (count == 1)
            //{
                pipeServer.WaitForConnection();
            //}            Console.WriteLine("Client connected.");            //传递任务执行参数
           //同时启动定时器
            
            try
            {
                // Read user input and send that to the client process.
                sw.AutoFlush = true;
                Console.Write("开始传递任务执行参数run");
                sw.WriteLine("run");
                Timer1.Elapsed += new ElapsedEventHandler(Timer1_ElapsedEventHandler);
                Timer1.Start();            }
            // Catch the IOException that is raised if the pipe is broken
            // or disconnected.
            catch (IOException e)
            {
                Console.WriteLine("ERROR: {0}", e.Message);
            }            ///管道进入监听状态
            ///收到工具的不同响应进行不同处理
            ///
            
           
            string temp;
            while ((temp = sr.ReadLine()) != null)
            {
                Console.WriteLine("Received from tools: {0}", temp);
                if (temp == "the tool is alive")
                {
                    Console.WriteLine("收到工具存活消息,将定时器清0");
                    //定时器清0
                    Timer1.Close();
                    //定时器启动
                    Timer1.Start();
                                    }
                if (temp == "the task is finished")
                {
                    Console.WriteLine("当前任务执行完毕,置工具状态表为空闲");
                    //释放定时器资源
                    Timer1.Close();
                    break;
                    //置工具状态表为空闲
                    ///待补
                    ///
                }
                            }           Console.Write("Press Enter to continue...");
           Console.ReadLine(); 
        }        /// <summary>
        /// 
        ///  触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer1_ElapsedEventHandler(object sender,
       ElapsedEventArgs e)
        {
            Console.WriteLine("timer1TimeOut");            //重启工具,启动成功后在发送任务执行参数
            //是不是要关掉定时器和管道呢????
            ///这要看管道是不是一直连着,还是每执行一个任务就重新建立一次。。??????????????
            ///
            /// 
            ///重启工具           // sr.Close();
            //sw.Close();
            try
            {
                pipeServer.Dispose();            }
            catch (IOException ex)
            {
                Console.WriteLine("ERROR: {0}", ex.Message);
            }            ToolManage test1 = new ToolManage();
            test1.run();        }//private static void Timer1_ElapsedEventHandler(object sender,ElapsedEventArgs e)    }//class ToolManage    /// <summary>
    /// 测试类
    /// 
    /// </summary>
   
    class Program
    {
        static void Main(string[] args)
        {
            ToolManage test2=new ToolManage();
            test2.run();
        }
    }
}