作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<分区>
Possible Duplicate:
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
如何纠正这个异常:
跨线程操作无效:从创建它的线程以外的线程访问控件“pgImportProcess(ProgressBar 控件)”。
代码:
表格:
private void btnImport_Click(object sender, EventArgs e)
{
if (CheckDataValidation() == false) return;
if (MessageBox.Show("Do you want to import this file?", "Import", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
Cursor.Current = Cursors.WaitCursor;
//_blacklist.Process(pgImportProcess);
Thread thread = new Thread(new ThreadStart(delegate { _blacklist.Process(pgImportProcess); }));
thread.Start();
if (!thread.IsAlive) thread.Abort();
//ThreadStart process = delegate
// {
// _blacklist.Process(pgImportProcess);
// };
//Thread threadProcess = new Thread(process);
//threadProcess.Start();
//if(!threadProcess.IsAlive) threadProcess.Abort();
}
类:
public void Process(ProgressBar process)
{
int same = 0, added = 0, updated = 0;
OracleConnection connection = (OracleConnection)DbConnection.Instance();
OracleTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
process.Step = 1;
process.Minimum = 1;
process.Maximum = _recordNumber;
while (_csv.ReadNextRecord())
{
if (_csv[0] == null | _csv[0] == "") break;
process.PerformStep();
using (OracleCommand cmd = new OracleCommand(_sql, connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 600;
cmd.BindByName = true;
switch (_fieldCount)
{
case SdnName :
ImportBlacklistName(cmd);
break;
case SdnAddress:
ImportBlacklistAddress(cmd);
break;
case SdnAlt :
ImportBlacklistAlt(cmd);
break;
}
try
{
cmd.ExecuteNonQuery();
switch (cmd.Parameters["message_out"].Value.ToString())
{
case "Added":
added += 1;
break;
case "Same":
same += 1;
break;
case "Updated":
updated += 1;
break;
}
}
catch (Exception error)
{
transaction.Rollback();
MessageBox.Show(error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
transaction.Commit();
MessageBox.Show(String.Format("Total Record : {0} ; Added : {1} ; Updated : {2} ; Same : {3} !!!!",_recordNumber,added,updated,same), "Import successsfull", MessageBoxButtons.OK, MessageBoxIcon.Information);
_recordNumber = 0;
process.Value = 1;
_csv.Dispose();
}
它在 Process.step = 1
时捕获异常如何解决这个问题?提前致谢
我是一名优秀的程序员,十分优秀!