我有一段代碼循環訪問數組,其中數組元素存儲文件名。每循環,代碼加載一個XML文檔。當我直接調用數組元素時,我收到一個異常,說索引超出了數組的邊界,但是如果我將數組元素存儲到單獨的變量中,代碼編譯就好了。Dispatcher.BeginInvoke數組循環中的異常
我只是不明白爲什麼有差異,爲什麼一個工作,而不是其他。
編譯OK
for(int i =0; i < MyArray.Count(); i++)
{
string myString = MyArray[i].Split(',')[0];
Dispatcher.BeginInvoke(new Action(() =>
{
string xmlPath = _PATH + + myString;
var document = XDocument.Load(xmlPath, LoadOptions.SetLineInfo);
}));
}
}
異常:索引數組的邊界之外
for(int i =0; i < MyArray.Count(); i++)
{
Dispatcher.BeginInvoke(new Action(() =>
{
string xmlPath = _PATH + + MyArray[i].Split(',')[0];
var document = XDocument.Load(xmlPath, LoadOptions.SetLineInfo);
}));
}
}
你可以嘗試寫入控制檯的所有'我'值嗎? –
你能證明你的'MyArray'是怎麼樣的 –