我有一個Parallel.Foreach小問題: 我有一個抽象類和一些派生類。其中一個叫ActiveX元素(網頁瀏覽器)。我想使這個對象線程安全的,但它不會工作:Parallel.ForEach和ActiveX
Parallel.ForEach(stringarray, currentfile =>
{
// When we have something, set the thread to STA
// So we can call a WebBrowser
if (currentfile.Contains("something"))
Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
// Here is the part where the WebBrowser is called
// But it fails and the debugger says that
// Thread.CurrentThread.ApartmentState is MTA, but the condition above
// is true
obj track = IService.Create(currentfile);
if (track != null)
{
lock(my_list)
my_list.Add(track);
}
}
那麼是否有機會處理這種情況? 或者我必須用ActiveX提取零件並自行處理它們? – Link