我写了一个从数据库读出路径字段,然后验证此路径文件是否存在的工具,结果程序一跑起来后,只要一切换界面,标题栏显示的当前记录条目数就停止了,呈假死状态,但查看进程发现其实还在正常运行。以前请教过一个人,他告诉我一个线程当然会这样,但我不知如何更改程序,我将源代码附上,麻烦各位高手帮我看一下,不胜感激!using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Threading;namespace 验证文件数目
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void display_info(string info)
        {
            this.Text = info;
        }
        static DataTable GetSchemaTable(string connectionString)
        {
            using (OleDbConnection connection = new
                       OleDbConnection(connectionString))
            {
                connection.Open();
                DataTable schemaTable = connection.GetOleDbSchemaTable(
                    OleDbSchemaGuid.Tables,
                    new object[] { null, null, null, "TABLE" });
                return schemaTable;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
            string filepath;//文件路径字段名
            int tablecount;//表总行数
            string dbname;//数据库路径,含文件名
            string tablename;//所要验证的表名
            string filelable;//验证文件的路径
            string mysql;//查询语句
            //string daxiao;
            //string tempfilename;            dbname = textBox1.Text;
            tablename = comboBox1.Text;
            filelable = textBox3.Text;
            if (System.IO.File.Exists(dbname))//判断用户选择<库路径>文件是否存在
            {
                if (filelable != "")//判断用户输入<文件路径>是否为空
                {
                    display_info("正在将数据读入缓存,请稍候......");
                    OleDbConnection myConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbname + ";");
                    myConn.Open();//打开连接字符串
                    if (!checkBox1.Checked)//是否为二次查找
                    {
                        mysql = "SELECT id,filecount,filepath FROM " + tablename;
                        //mysql = "SELECT id,filecount,filepath,daxiao FROM " + tablename;
                    }
                    else
                    {
                        mysql = "SELECT id,filecount,filepath FROM " + tablename + " where filecount=0";
                    }
                    OleDbDataAdapter thisAdapter = new OleDbDataAdapter(mysql, myConn);
                    OleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter);
                    DataSet thisDataSet = new DataSet();
                    thisAdapter.Fill(thisDataSet, "Test");//填充DataSet
                    display_info("正在遍历数据库记录,请稍候......");
                    tablecount = thisDataSet.Tables["Test"].Rows.Count;                    for (int i = 0; i < tablecount; i++)//遍历数据库
                    {
                        filepath = @filelable + thisDataSet.Tables["Test"].Rows[i]["filepath"];
                        //daxiao = @filelable + thisDataSet.Tables["Test"].Rows[i]["daxiao"];
                        //tempfilename = @daxiao + thisDataSet.Tables["Test"].Rows[i]["filepath"];
                        if (Convert.ToString(thisDataSet.Tables["Test"].Rows[i]["filepath"]) != "")//判断路径是否为空
                        {
                            if (System.IO.File.Exists(filepath))//判断文件是否存在
                            {
                                thisDataSet.Tables["Test"].Rows[i]["filecount"] = 1;
                                //Directory.CreateDirectory(daxiao);
                                //File.Move(filepath, tempfilename);
                            }
                            else
                            {
                                thisDataSet.Tables["Test"].Rows[i]["filecount"] = 0;
                            }
                        }
                        else
                        {
                            thisDataSet.Tables["Test"].Rows[i]["filecount"] = -1;
                        }
                        if (i % 1000 == 0)
                        {
                            display_info(i + "/" + tablecount);
                        }
                    }
                    display_info("正在回写数据,此处耗时较多,请耐心等待......");
                    thisAdapter.Update(thisDataSet, "Test");
                    display_info("验证文件数目");
                    myConn.Close();
                    MessageBox.Show("完成!", "ok");
                }
                else
                {
                    MessageBox.Show("请输入正确的文件路径!", "错误");
                }
            }
            else
            {
                MessageBox.Show("找不到数据库,请重新输入!", "错误");
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            //选择库文件
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Access Files(*.mdb)|*.mdb";
            openFileDialog1.Title = "选择一个mdb文件";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
                DataTable tablenamelist = GetSchemaTable(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + openFileDialog1.FileName + ";");
                comboBox1.Enabled = true;
                
                foreach (DataRow ds in tablenamelist.Rows)
                {
                    comboBox1.Items.Add(ds["TABLE_NAME"]);
                }
            }
        }
    }
}

解决方案 »

  1.   

    使用BackgroundWorker 类,把读数据代码放到DoWork事件处理函数中。
      

  2.   

    把button1_Click的代码放在新线程ReadDB内。ReadDB()
    {
       ...
    }button1_Click(...)
    {
       Thread t = new Thread(new ThreadStart(ReadDB));
       t.Start();
    }
      

  3.   

    我的做法是在程序里面加 Application.DoEvents();语句.
      

  4.   

    采用了Efcndi的方法
    但程序运行很多地方都提示“线程间操作无效: 从不是创建控件“Form1”的线程访问它”。
    不知如何解决。因为程序很快就要用了,我没时间去从头学一遍c#,只能麻烦各位高手了。
    haiwangstar pepsisoft 二位提的方法我不知如何进行,麻烦提示一下,谢谢。