//給ProgressBar最大執行時間
progressBar1.Maximum = 10 * 1000;
string strMessage = string.Empty;
//取得、控制執行緒,目前狀態
Thread t = new Thread(database);
t.Start();
//以ProgressBar最長時間,跑迴圈,影響ProgressBar進度
for (int i=0;i<10001;i++)
{
progressBar1.Value = i;
if (progressBar1.Maximum == progressBar1.Value && !t.Join(10 * 1000))
{
strMessage = "完成";
break;
}
//執行緒是否執行超過指定時間
if (!t.Join(10 * 1000))
{
strMessage = "Done";
//強制結束執行緒
t.Abort();
break;
}
}
if (progressBar1.Maximum == progressBar1.Value && !t.Join(10 * 1000))
{
strMessage = "完成";
}
MessageBox.Show(strMessage);
//給Thread的Function
public void database()
{
OracleConnection conn = new OracleConnection("Data Source=DB;User Id=abc;Password=abcd;");
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"INSERT INTO Tablename VALUES('123','1234','abc')";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}