程序运行一段时间老是会死掉。我找不到远因。麻烦各位指点下小弟。。谢谢。代码如下:
Thread tt = null;
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
tt = new Thread(new ThreadStart(import));
this.dgvNewHouse.Enabled = false;
this.toolStrip1.Enabled = false;
this.tsPages.Enabled = false;
this.grp1.Enabled = false;
this.panel3.Visible = true;
this.progressBar1.Enabled = true;
this.richTextBox1.Enabled = true;
this.button1.Enabled = true;
this.panel3.Enabled = true;
tt.Start();
}
int errCount = 0;
private void import() {
int countNum = 1;
ImportExportToExcel iete = new ImportExportToExcel();
string fileName = ""; if (this.openFileDialog1.FileName.Trim() != "")
{
fileName = this.openFileDialog1.FileName;
}
else
{
return;
}
try
{
DataSet ds = iete.ImportFromExcel(fileName);
this.progressBar1.Maximum = ds.Tables[0].Rows.Count;
this.progressBar1.Value = 0;
this.richTextBox1.Text = "";
errCount = 0;
Thread.CurrentThread.Join(50);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
try
{
Thread.Sleep(50);
this.progressBar1.Value += 1;
ConsolveHouseBean bean = new ConsolveHouseBean();
try
{
bean.CArea1 = float.Parse(ds.Tables[0].Rows[i]["安置面积"].ToString());
}
catch
{ }
bean.CArea2 = float.Parse(ds.Tables[0].Rows[i]["另购面积"].ToString());
bean.CConsolveTime = ds.Tables[0].Rows[i]["安置时间"].ToString();
bean.CCountArea = float.Parse(ds.Tables[0].Rows[i]["总面积"].ToString());
int ccpid = 0;
DataSet dsNewPro = proBll.findAllPro(new ConsolveProject());
for (int j = 0; j < dsNewPro.Tables[0].Rows.Count; j++)
{
if (ds.Tables[0].Rows[i]["安置项目"].ToString() == dsNewPro.Tables[0].Rows[j]["cPName"].ToString())
{
ccpid = Int32.Parse(dsNewPro.Tables[0].Rows[j]["cPId"].ToString());
}
}
bean.CCPId = ccpid;
bean.CHouseNumber = ds.Tables[0].Rows[i]["房间号码"].ToString();
bean.CMember = ds.Tables[0].Rows[i]["成员名称"].ToString();
bean.CMemberCount = Int32.Parse(ds.Tables[0].Rows[i]["成员数"].ToString());
bean.CNote = ds.Tables[0].Rows[i]["备注"].ToString();
bean.CUserIDCard = ds.Tables[0].Rows[i]["户主身份证"].ToString();
bean.CUserName = ds.Tables[0].Rows[i]["户主名称"].ToString();
nhBll.add(bean);
this.richTextBox1.AppendText("成功插入第" + (i + 1) + "条数据. \r\n");
this.richTextBox1.ScrollToCaret();
countNum++;
}
catch {
this.richTextBox1.AppendText("准备插入第" + (i + 1) + "条数据失败,可能是数据的格式不完整导致. \r\n");
this.richTextBox1.ScrollToCaret();
errCount++;
}
}
this.richTextBox1.AppendText("导入完成");
this.button1.Text = "完成";
}
catch(Exception eee)
{
MessageBox.Show(eee.Message.ToString());
MessageBox.Show("数据异常,请检查导入文件的数据");
return;
}
}
Thread tt = null;
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
tt = new Thread(new ThreadStart(import));
this.dgvNewHouse.Enabled = false;
this.toolStrip1.Enabled = false;
this.tsPages.Enabled = false;
this.grp1.Enabled = false;
this.panel3.Visible = true;
this.progressBar1.Enabled = true;
this.richTextBox1.Enabled = true;
this.button1.Enabled = true;
this.panel3.Enabled = true;
tt.Start();
}
int errCount = 0;
private void import() {
int countNum = 1;
ImportExportToExcel iete = new ImportExportToExcel();
string fileName = ""; if (this.openFileDialog1.FileName.Trim() != "")
{
fileName = this.openFileDialog1.FileName;
}
else
{
return;
}
try
{
DataSet ds = iete.ImportFromExcel(fileName);
this.progressBar1.Maximum = ds.Tables[0].Rows.Count;
this.progressBar1.Value = 0;
this.richTextBox1.Text = "";
errCount = 0;
Thread.CurrentThread.Join(50);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
try
{
Thread.Sleep(50);
this.progressBar1.Value += 1;
ConsolveHouseBean bean = new ConsolveHouseBean();
try
{
bean.CArea1 = float.Parse(ds.Tables[0].Rows[i]["安置面积"].ToString());
}
catch
{ }
bean.CArea2 = float.Parse(ds.Tables[0].Rows[i]["另购面积"].ToString());
bean.CConsolveTime = ds.Tables[0].Rows[i]["安置时间"].ToString();
bean.CCountArea = float.Parse(ds.Tables[0].Rows[i]["总面积"].ToString());
int ccpid = 0;
DataSet dsNewPro = proBll.findAllPro(new ConsolveProject());
for (int j = 0; j < dsNewPro.Tables[0].Rows.Count; j++)
{
if (ds.Tables[0].Rows[i]["安置项目"].ToString() == dsNewPro.Tables[0].Rows[j]["cPName"].ToString())
{
ccpid = Int32.Parse(dsNewPro.Tables[0].Rows[j]["cPId"].ToString());
}
}
bean.CCPId = ccpid;
bean.CHouseNumber = ds.Tables[0].Rows[i]["房间号码"].ToString();
bean.CMember = ds.Tables[0].Rows[i]["成员名称"].ToString();
bean.CMemberCount = Int32.Parse(ds.Tables[0].Rows[i]["成员数"].ToString());
bean.CNote = ds.Tables[0].Rows[i]["备注"].ToString();
bean.CUserIDCard = ds.Tables[0].Rows[i]["户主身份证"].ToString();
bean.CUserName = ds.Tables[0].Rows[i]["户主名称"].ToString();
nhBll.add(bean);
this.richTextBox1.AppendText("成功插入第" + (i + 1) + "条数据. \r\n");
this.richTextBox1.ScrollToCaret();
countNum++;
}
catch {
this.richTextBox1.AppendText("准备插入第" + (i + 1) + "条数据失败,可能是数据的格式不完整导致. \r\n");
this.richTextBox1.ScrollToCaret();
errCount++;
}
}
this.richTextBox1.AppendText("导入完成");
this.button1.Text = "完成";
}
catch(Exception eee)
{
MessageBox.Show(eee.Message.ToString());
MessageBox.Show("数据异常,请检查导入文件的数据");
return;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货