Parallel.Foreach に少し問題があります。抽象クラスといくつかの派生クラスがあります。それらの 1 つは、ActiveX 要素 (webbrowser) を呼び出します。このオブジェクトをスレッドセーフにしたいのですが、うまくいきません:
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);
}
}