C# 在STA和事件驱动环境中使用SmartThread
嗨,我有一个用例,我应该在表单中使用C Web浏览器并从中获取一些数据。然而,正如我们所知,C浏览器在STA下运行,因此我创建了一个具有单线程体系结构的SmartThread实例 在MVC应用程序的某些事件中,我应该调用web浏览器并获得结果。当浏览器在那一刻被实例化时,我可以调用任何JavaScript方法,但是当我在MVC中等待事件并在事件发生时调用时,SmartThread似乎不会调用webbrowser 这是我下面的代码,请帮助我C# 在STA和事件驱动环境中使用SmartThread,c#,winforms,webbrowser-control,threadpool,sta,C#,Winforms,Webbrowser Control,Threadpool,Sta,嗨,我有一个用例,我应该在表单中使用C Web浏览器并从中获取一些数据。然而,正如我们所知,C浏览器在STA下运行,因此我创建了一个具有单线程体系结构的SmartThread实例 在MVC应用程序的某些事件中,我应该调用web浏览器并获得结果。当浏览器在那一刻被实例化时,我可以调用任何JavaScript方法,但是当我在MVC中等待事件并在事件发生时调用时,SmartThread似乎不会调用webbrowser 这是我下面的代码,请帮助我 public class FormBrowser {
public class FormBrowser
{
public static Form1 form1 = null;
public static SmartThreadPool smartThreadPool;
public static void initialise()
{
if (smartThreadPool == null)
{
STPStartInfo stpStartInfo = new STPStartInfo();
stpStartInfo.StartSuspended = true;
stpStartInfo.ApartmentState = ApartmentState.STA;
stpStartInfo.MaxWorkerThreads = 1;
smartThreadPool = new SmartThreadPool(stpStartInfo);
var ab = smartThreadPool.QueueWorkItem(new WorkItemCallback(initForm), "line");
smartThreadPool.Start();
//smartThreadPool.Shutdown();
}
}
public static object initForm(object msg)
{
var id = Thread.CurrentThread.ManagedThreadId;
form1 = new Form1();
System.Windows.Forms.Application.Run(form1);
return null;
}
public static void redraw()
{
var id = Thread.CurrentThread.ManagedThreadId;
smartThreadPool.QueueWorkItem(new WorkItemCallback(rd), "line");
}
public static object rd(object msg)
{
var id = Thread.CurrentThread.ManagedThreadId;
form1.webBrowser1.Document.InvokeScript("startDrawing");
return null;
}
}
调用initialise方法时,SmartThread将创建一个线程池,然后创建一个包含Web浏览器的表单实例。接下来,当客户端发生任何事件时,比如他请求最新的数据,我必须调用方法redraw,该方法应该在内部调用rd,但这永远不会发生
请帮助我修复此问题,如果我缺少什么,请告诉我。以下代码对我有效
public class FormBrowser
{
public static Form1 form1 = null;
public static SmartThreadPool smartThreadPool;
public static SynchronizationContext ctx;
public static string html;
public static void initialise()
{
if (smartThreadPool == null)
{
STPStartInfo stpStartInfo = new STPStartInfo();
stpStartInfo.StartSuspended = false;
stpStartInfo.ApartmentState = ApartmentState.STA;
stpStartInfo.MaxWorkerThreads = 1;
smartThreadPool = new SmartThreadPool(stpStartInfo);
var ab = smartThreadPool.QueueWorkItem(new WorkItemCallback(initForm), "line");
smartThreadPool.Start();
//smartThreadPool.Shutdown();
}
}
public static object initForm(object msg)
{
var id = Thread.CurrentThread.ManagedThreadId;
form1 = new Form1();
ctx = WindowsFormsSynchronizationContext.Current;
System.Windows.Forms.Application.Run(form1);
return null;
}
public static string redraw(string chartType)
{
ctx.Send(rd, chartType);
return html;
}
public static void rd(object chartType)
{
var id = Thread.CurrentThread.ManagedThreadId;
html = (string)form1.webBrowser1.Document.InvokeScript("drawChart", new object[] { chartType });
}
}
如果与SynchronizationContext一起使用的SmartThreadPool工作良好。但是,仅使用SynchronizationContext或SmartThreadPool无法解决此问题。您必须在initForm中设置ApartmentState.STA;看,是的,这是真的,但因为我使用的是智能线程池,所以在初始化表单时它会设置它。非常感谢。