我在c#中使用Windows窗體,當我點擊我的按鈕時,我的方法一堆正在操作word文件和pdf文件。但是這個表格在東西發生時會掛起(平均時間是5分鐘,看起來真的很糟糕)。我正在考慮使用多線程來解決這個問題,我想分配更多的處理能力和內存。有沒有人對我有好的提示。所有幫助是極大的讚賞c#形式掛mutithread修復
這是在線程的做法,但它不工作
Thread oThread = new Thread(AwesomeMethod (wfile,a));
oThread.Start(this);
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
編輯1:
我想使用backgorundworker,但它不是叫我的方法。
private void createToolStripMenuItem_Click(object sender, EventArgs e)
{
ofd.Filter = ".docx |*.DOCX";
if (ofd.ShowDialog() == DialogResult.OK)
{
pictureBox2.Visible = true;
worker2 = new BackgroundWorker();
worker2.WorkerReportsProgress = true;
worker2.WorkerSupportsCancellation = true;
worker2.DoWork += (obj, ea) => smallworker();
worker2.RunWorkerAsync();
// Thread demoThread = null;
// demoThread = new Thread(new ThreadStart(smallworker()));
// demoThread.Start();
//smallworker();
// var process = System.Diagnostics.Process.Start("WINWORD.exe");
//process.WaitForExit();
button2.Enabled = true;
}
}
private void smallworker()
{
var fileName = ofd.FileName;
tabPage8.Text = ofd.SafeFileName;
// file.CopyTo(ofd.FileName);
string pdfname = ofd.SafeFileName.Replace(".docx", " PPO .pdf");
string pdfpath = spdf + "/ " + pdfname;
string DOCXpath = spdf + "/ " + ofd.SafeFileName;
try
{
System.IO.File.Copy(fileName, DOCXpath);
wordtopdf(DOCXpath, pdfpath);
}
catch
{
MessageBox.Show("File Already in the folder");
}
perform_search(ofd.FileName);
dt_test();
_TODELETE_.Add(DOCXpath);
_TODELETE_.Add(pdfpath);
pictureBox2.Visible = false ;
}
您使用的是什麼版本的.net框架? –
我正在使用.NET框架4.5 –
此外,上述代碼產量的錯誤是:CS1503 \t參數1:無法從'void'轉換爲'System.Threading.ThreadStart –