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

看板C_Sharp (C#)作者 (仁武田信玄)時間10年前 (2015/04/07 13:28), 編輯推噓3(302)
留言5則, 3人參與, 最新討論串1/1
最近在寫一個讓程式自動更新的功能 實際上就只是進行檔案的複製貼上而已 但是放在單一執行緒下可以正常運作 多執行緒下就會直接讓程式結束 不知道是哪裡出問題 請大家幫我看看 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://www.ptt.cc/bbs/C_Sharp/M.1428384508.A.293.html

04/07 14:49, , 1F
直接讓程式結束是什麼意思 感覺你用錯東西了
04/07 14:49, 1F

04/07 14:55, , 2F
主執行緒沒等BW跑完就先跑到Dispose了嗎
04/07 14:55, 2F

04/07 15:03, , 3F
爸爸叫兒子買東西,卻沒有等他回來,就把門關起來。
04/07 15:03, 3F

04/07 15:18, , 4F
如果是這樣那就根本不需要用多執行緒阿XD
04/07 15:18, 4F

04/07 16:00, , 5F
好爸爸
04/07 16:00, 5F
文章代碼(AID): #1L8shyAJ (C_Sharp)
文章代碼(AID): #1L8shyAJ (C_Sharp)