[問題] 關於多執行緒的問題

作者: yj0803 (台中居正廣)   2015-04-07 13:28:25
最近在寫一個讓程式自動更新的功能
實際上就只是進行檔案的複製貼上而已
但是放在單一執行緒下可以正常運作
多執行緒下就會直接讓程式結束
不知道是哪裡出問題
請大家幫我看看
CODE 如下
backgroundWorker1.RunWorkerAsync();
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
DirectoryCopy(g_strCopyLocal, g_strCopyBackup, true);
DirectoryCopy(g_strCopyServer, g_strCopyLocal, true);
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
try
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, true);
}
catch
{
}
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
if (subdir.Name != "UpGrade" || subdir.Name != "CheakVersion")
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
}
請大家能給我一些方向
作者: TameFoxx (foxx)   2015-04-07 14:49:00
直接讓程式結束是什麼意思 感覺你用錯東西了主執行緒沒等BW跑完就先跑到Dispose了嗎
作者: johnpage (johnpage)   2015-04-07 15:03:00
爸爸叫兒子買東西,卻沒有等他回來,就把門關起來。
作者: TameFoxx (foxx)   2015-04-07 15:18:00
如果是這樣那就根本不需要用多執行緒阿XD
作者: GoalBased (Artificail Intelligence)   2015-04-07 16:00:00
好爸爸

Links booklink

Contact Us: admin [ a t ] ucptt.com