作者yj0803 (仁武田信玄)
看板C_Sharp
標題[問題] 關於多執行緒的問題
時間Tue Apr 7 13:28:25 2015
最近在寫一個讓程式自動更新的功能
實際上就只是進行檔案的複製貼上而已
但是放在單一執行緒下可以正常運作
多執行緒下就會直接讓程式結束
不知道是哪裡出問題
請大家幫我看看
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);
}
}
}
}
請大家能給我一些方向
--
Sent from my Misaka 10032
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.71.170.97
※ 文章網址: https://webptt.com/m.aspx?n=bbs/C_Sharp/M.1428384508.A.293.html
1F:推 TameFoxx: 直接讓程式結束是什麼意思 感覺你用錯東西了 04/07 14:49
2F:→ TameFoxx: 主執行緒沒等BW跑完就先跑到Dispose了嗎 04/07 14:55
3F:推 johnpage: 爸爸叫兒子買東西,卻沒有等他回來,就把門關起來。 04/07 15:03
4F:推 TameFoxx: 如果是這樣那就根本不需要用多執行緒阿XD 04/07 15:18
5F:→ GoalBased: 好爸爸 04/07 16:00