我需要遍历TreeView中的所有数据库,然后选择TreeNode作为要还原的数据库名称.如果我只选择1,2或3个数据库来恢复,我的代码工作正常.但如果我选择了很多数据库,它就会被挂起.
请检查我的代码并帮助我..
foreach (TreeNode tn in trvList.Nodes)
{
if (tn.Checked == true)
{
strFileName = GetFileTorestor(strRealdb);//Check File Directory to restore example : C:data\tn.text.
if (strFileName != "")
{
RestoreDatabase(res, tn);
}
else
{
strErorLog += "\r\n -" + tn.Text;
blIsEror = true;
MessageBox.Show("Database " + tn.Text + " has not found!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
/// <summary>
/// Restore database according to treenode selected.
/// </summary>
/// <param name="res"></param>
/// <param name="tn"></param>
private void RestoreDatabase(Restore res,TreeNode tn)
{
res.Database = tn.Text;
res.Action = RestoreActionType.Database;
res.Devices.AddDevice(strFileName, DeviceType.File);
res.ReplaceDatabase = true;
this.pgrRestore.Value = 0;
this.pgrRestore.Maximum = 100;
//res.Complete += new ServerMessageEventHandler(sqlRestore_Complete);
res.PercentCompleteNotification = 5;
res.PercentComplete += new PercentCompleteEventHandler(sqlRestore_PercentComplete);
res.SqlRestore(DBHelper.Server);
stLog += "\r\n " + tn.Text + " from file " + strFileName;//Record database name already restore to log text
txtLog.Text = stLog;
txtLog.Refresh();
}
Run Code Online (Sandbox Code Playgroud)
如果你的意思是卡住那个UI冻结了一段时间那么那是因为你在作为UI线程的主线程上击败了你的工作.由于线程忙于完成工作,因此无法进行UI交互.您应该看一下在BackgroundWorker后台执行您的工作并向UI报告进度.