[問題] 關於BackgroundWorker的行為中有大量檔 …
大家好
我在WPF的程式之中,有讀取大量的xaml檔案
所以我將讀取xaml的部份放在BackgroudWorker的DoWork
void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
// 預備儲存讀進來的string
List<string> ContentList = new List<string>();
// 準備被讀的檔案路徑的集合,每個檔案約200-300k, 每次約讀50-500個檔
List<string> fileList = e.Argument as List<string>;
foreach (string filePath in fileList)
{
string content = "";
FileInfo info = new FileInfo(filePath);
using (FileStream stream = info.OpenRead())
{
StreamReader reader = new StreamReader(stream);
content = reader.ReadToEnd();
}
ContentList.Add(content);
}
e.Result = ContentList;
}
本來這段程式碼交給BackgroundWorker的原因是想保持WPF視窗的互動性
但是現在DoWork在執行的時候
視窗也沒有回應,反應和沒有使用BackgroundWorker是差不多的
想請教各位板友應該怎麼處理
謝謝大家耐心看完
補充啓動BackgroundWorker那段
// 某個按鈕的觸發事件
private void ShowSelectedImage_Click(object sender, RoutedEventArgs e)
{
// SelectionListView是combobox
if (SelectionListView.SelectedItems.Count == 0)
{
MessageBox.Show("請勾選比對結果")
return;
}
// 先把狀態都清光光,這段可以忽略
compare1.Children.Clear();
compare2.Children.Clear();
imageCollection.Clear();
AllUnChecked(compare1ChkFields);
AllUnChecked(compare2ChkFields);
// 取得在ListView中勾選的時間
List<DateTime> cDateList = new List<DateTime>();
cDateList = new List<DateTime>();
foreach (DataRowView item in SelectionListView.SelectedItems)
{
cDateList.Add(Convert.ToDateTime(item["CDate"]));
}
foreach (DateTime cDate in cDateList)
{
// compare1
ShowImage image = new ShowImage("XamlDiagram\\iswis-map.xaml");
image.OverLap(domainSelected.GetMapFrameXaml());
image.SetText(cDate.ToString());
image.Info = goalImage.Info;
compare1.Children.Add(image);
imageCollection.Add(image);
// compare2
ShowImage image2 = new ShowImage("XamlDiagram\\iswis-map.xaml");
image2.OverLap(domainSelected.GetMapFrameXaml());
image2.SetText(cDate.ToString());
image2.Info = goalImage.Info;
compare2.Children.Add(image2);
imageCollection.Add(image2);
}
// 從這以下一大段是從介面上使用者選取的狀態去找出要餵給BackgroundWorker的
// 路徑集合
string historyModelName = sourceHistory.SelectedItem.ToString();
List<string> buttonContentList = new List<string>();
foreach (ShowImageButton button in compare1ChkFields.Children)
{
buttonContentList.Add(button.Content.ToString());
}
// 要餵給BackgroundWorker的檔案路徑集合
List<string> fileList = new List<string>();
foreach (DateTime cDate in cDateList)
{
foreach (string buttonContent in buttonContentList)
{
string fileName = Properties.Resources.HISTORY_XAML_PATH
+ "\\" + historyModelName + "\\" + cDate.ToString("yyyy") + "\\" +
cDate.ToString("HH") + "\\" + historyModelName + "_" +
cDate.ToString("yyyyMMddHHmm") + "_000_" + buttonContent + ".xaml";
fileList.Add(fileName);
}
}
// 這行我自己測試用
MessageBox.Show("開始讀取資料");
// 啓動BackgroundWorker
backgroundWorker.RunWorkerAsync(fileList);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.231.43.58
→
06/14 14:50, , 1F
06/14 14:50, 1F
→
06/14 14:53, , 2F
06/14 14:53, 2F
※ 編輯: kairy 來自: 118.231.43.58 (06/14 15:03)
→
06/14 22:53, , 3F
06/14 22:53, 3F
→
06/14 22:54, , 4F
06/14 22:54, 4F