孤星如晨 2018-07-11 06:41 采纳率: 33.3%
浏览 3124
已采纳

c# 写的windows 窗体应用程序 运行后 界面卡主不动 程序照样运行

    本人小白菜菜鸟,刚学会c# 不久,用vs2015写了几个程序,大致内容:循环读取文件夹内word信息,
    界面两个按钮,开始和停止。点开始后,程序运行,然后界面就卡主不动了,点停止没反应,点窗体上的叉叉也没反应,只能任务管理器结束进程。请问这是为什么?
    Ps:代码里面没有timer类的控件。
    部分代码如下:

其中的doc和app是全局变量
private static Word.Application app = new Word.Application();
private static Word.Document doc = null;

 private void button2_Click(object sender, EventArgs e)
        {
            var files = Directory.GetFiles(wordPath, "*.doc");
            foreach (var file in files)
            {
                button2.Text = "进行中。。。";
                wordName = file.Replace(textBox2.Text, "");
                textBox1.Text = wordName;
                textBox1.Refresh();
                string paperName = wordName.Replace(".doc", "");
                if (Sql.existsExam(paperName) > 0) {
                    listBox2.Items.Add ( "此试卷已存在!"+ file.ToString());
                    listBox2.Refresh();
                    continue;
                }
                try
                {
                    exam_id=Sql.getExamId();
                    wordNameSave = wordName.Substring(0, wordName.IndexOf("."));
                    String path = @"d:\word/img_hy/" + exam_id;
                    BaseClass.createFile(path);
                    BaseClass.writeFile("d:\\word\\log.txt", "**********************************************************************", 0, 1);
                    string log = file+"         "+ DateTime.Now.ToString();
                    BaseClass.writeFile("d:\\word\\log.txt",log, 0, 1);
                    doc = app.Documents.Open(file);   //一会要记录word打开的文档
                    string strContent = ReadWordMian.readWordMain(app, doc, path, wordNameSave);
                    Sql.insertExam(ReadWordMian.paperHead,exam_id, grade_id, subject_id);
                    cutExercises(strContent);
                    log = "we have a finish !   " + wordNameSave + "   " + DateTime.Now.ToString();
                    //Console.WriteLine(DateTime.Now.ToString());
                    BaseClass.writeFile("d:\\word\\log.txt", log, 0, 1);
                }
                catch (Exception err) {
                    button2.Text = "批量入库";
                    //listBox2.Items.Add("异常:"+"此文件读取异常"+ file+"  "+err);
                    BaseClass.writeFile("d:\\word\\log.txt", "异常:" + "此文件读取异常" + file + "  " + err, 0, 1);
                }
            }
            // listBox2.Items.Add(error_string);
            doc.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
            app.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
            button2.Text = "批量入库";
        }
  • 写回答

7条回答

  • excaliburw 2018-07-11 07:16
    关注

    通常你描述的情况,是因为你点击的按键的事件方法没有执行完,程序没有从方法里返回,所以主界面操作不了。
    而你的这个button2_Click 事件响应方法里面有很多sql的操作,你可以添加一些断点判断程序卡在哪个地方了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试