我在dos下运行一个c#程序,在对数据库进行一连串的插入操作的时候,如果中途点击关闭dos窗口,
则必须把之前插入到数据库的数据全部清空
请问该如何实现,c#中有没监听程序异常中止的监听器

解决方案 »

  1.   

    恩 是的 。。运行 XXX.exe +参数    
      

  2.   

    我在网上帮你查找了下  有人解决的  人家是用windows API才捕捉消息来实现
    代码如下  测试通过  
    顺便学习下
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Diagnostics;namespace ConsoleApplication17
    {    public delegate bool ConsoleCtrlDelegate(int dwCtrlType);
        class Program
        {
            [DllImport("kernel32.dll")]
            private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine, bool Add);
            //当用户关闭Console时,系统会发送次消息
            private const int CTRL_CLOSE_EVENT = 2;
            static void Main(string[] args)
            {
                Program mc = new Program();
                Console.ReadLine();
            }        public Program()
            {
                // 用API安装事件处理
                ConsoleCtrlDelegate newDelegate = new ConsoleCtrlDelegate(HandlerRoutine);
                bool bRet = SetConsoleCtrlHandler(newDelegate, true);
                if (bRet == false)  //安装事件处理失败
                {
                    Debug.WriteLine("失败");
                }
                else
                {
                    Console.WriteLine("ok");
                    Console.Read();
                }
            }
            /// <summary>
            /// 处理消息的事件
            /// </summary>
            private static bool HandlerRoutine(int CtrlType)
            {
                switch (CtrlType)
                {
                    case CTRL_CLOSE_EVENT:       //用户要关闭Console了
                        Debug.WriteLine("Close");
                        break;
                }            return false;
            }
        }
    }
      

  3.   

    要5个小时?  事先的文本文件为什么就不能改成csv,excel什么的  直接在程序里操作excel数据  要快的多