当我运行我的代码并单击执行按钮时,它会在运行时挂起(特别是当我运行更多文件时)我无法再单击我的主表单,它需要先完成执行才能与我的主要交互形成。我研究过我需要多线程。我将如何插入多线程或后台工作程序的代码,以便我仍然可以与我的表单进行交互。
下面是执行按钮,当我点击它时开始执行我的scipts并在未完成时冻结我的表格。
private void Execute_Click(object sender, EventArgs e)
{
try
{
//Execution Pane
timer1.Start();
progressBar1.Maximum = checkedListBox1.CheckedItems.Count;
richTextBox2.Text = "";
string file_name = "\\test1.sql";
file_name = textBox1.Text + file_name;
{
string line;
line = null;
foreach (Object item in checkedListBox1.CheckedItems)
{
string sql_name = "\\test1.sql";
sql_name = textBox1.Text + sql_name;
StreamWriter SaveFile = new StreamWriter(sql_name);
SaveFile.WriteLine(":r \"" + textBox1.Text + "\\" + item + "\"");
SaveFile.Close();
line += item + "\n";
StreamReader sql = new StreamReader(sql_name);
//richTextBox1.Text = sr.ReadToEnd();
sql.Close();
sql.Dispose();
//Create a batchfile for execution of consolidated script
string execute = "\\Script_Runner.bat";
execute = textBox1.Text + execute;
StreamWriter SaveFile2 = new StreamWriter(execute);
SaveFile2.WriteLine("sqlcmd -S localhost -E -i " + textBox1.Text + "\\" + "test1.sql");
//Environment.NewLine + "pause");
SaveFile2.Close();
//running the batchfile
string ScriptRun = "\\Script_Runner.bat";
ScriptRun = textBox1.Text + ScriptRun;
Process.Start(ScriptRun);
progressBar1.PerformStep();
this.Refresh();
}
richTextBox2.Text = line;
richTextBox2.Text += "\nData Patch Completed.";
transfer = label3.Text;
NotificationForm NF = new NotificationForm();
NF.Show();
}
}
catch (Exception ex)
{
string filePath = @"C:\Error.txt";
filePath = textBox1.Text + filePath;
using (StreamWriter writer = new StreamWriter(filePath, false))
{
writer.WriteLine("Message :" + ex.Message + Environment.NewLine + "StackTrace :" + ex.StackTrace +
"" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
}
}
答案 0 :(得分:2)
将BackgroundWorker拖放到表单中,在Execute_Click事件中添加以下代码。
selector
将Do_Work事件添加到您的Backgroundworker。
$('#table').on('mousedown', 'div', function(e) {
console.log('Do some stuff here.');
});
答案 1 :(得分:1)
private void Execute_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() =>
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(DoSomething));
}
else
{
DoSomething();
}
});
}
private void DoSomething()
{
try
{
//Execution Pane
timer1.Start();
progressBar1.Maximum = checkedListBox1.CheckedItems.Count;
richTextBox2.Text = "";
var file_name = "\\test1.sql";
file_name = textBox1.Text + file_name;
{
string line;
line = null;
foreach (var item in checkedListBox1.CheckedItems)
{
var sql_name = "\\test1.sql";
sql_name = textBox1.Text + sql_name;
var SaveFile = new StreamWriter(sql_name);
SaveFile.WriteLine(":r \"" + textBox1.Text + "\\" + item + "\"");
SaveFile.Close();
line += item + "\n";
var sql = new StreamReader(sql_name);
//richTextBox1.Text = sr.ReadToEnd();
sql.Close();
sql.Dispose();
//Create a batchfile for execution of consolidated script
var execute = "\\Script_Runner.bat";
execute = textBox1.Text + execute;
var SaveFile2 = new StreamWriter(execute);
SaveFile2.WriteLine("sqlcmd -S localhost -E -i " + textBox1.Text + "\\" + "test1.sql");
//Environment.NewLine + "pause");
SaveFile2.Close();
//running the batchfile
var ScriptRun = "\\Script_Runner.bat";
ScriptRun = textBox1.Text + ScriptRun;
Process.Start(ScriptRun);
progressBar1.PerformStep();
Refresh();
}
richTextBox2.Text = line;
richTextBox2.Text += "\nData Patch Completed.";
var transfer = label3.Text;
//NotificationForm NF = new NotificationForm();
//NF.Show();
}
}
catch (Exception ex)
{
var filePath = @"C:\Error.txt";
filePath = textBox1.Text + filePath;
using (var writer = new StreamWriter(filePath, false))
{
writer.WriteLine("Message :" + ex.Message + Environment.NewLine + "StackTrace :" + ex.StackTrace +
"" + Environment.NewLine + "Date :" + DateTime.Now);
writer.WriteLine(Environment.NewLine +
"-----------------------------------------------------------------------------" +
Environment.NewLine);
}
}
}