计时器和线程出现问题 [英] Trouble with timers and threads

查看:151
本文介绍了计时器和线程出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个示例学习C#编码器,他不是很高级,这就是为什么无论互联网上的信息量如何,这个问题都让我很头疼.

I'm a learn-by-example C# coder who isn't very advanced, which is why this problem is completely stumping me regardless of the amount of information on the internet.

我实质上是在创建一个程序,该程序在计时器上反复轮询网站以获取一些信息.在此过程中,将创建一个WebBrowser控件以导航到该信息(身份验证所需).该程序在启动时运行这一系列事件,然后使用设置为每10分钟一次的System.Timers.Timer(当然较少调试)来执行相同的一系列事件,但是当我的Timer.Elapsed事件触发该过程时,我得到了a:

I'm essentially creating a program that is, on a timer, repeatedly polling a website to get some information. During this process, a WebBrowser control is created to navigate to the information (needed for authentication). The program runs this series of events at startup, then using a System.Timers.Timer set to every 10 minutes (less for debugging of course) to do that same series of events yet when my Timer.Elapsed event triggers that process, I get a:

ThreadStateException ,其描述为 ActiveX控件'8856f961-340a-11d0-a96b-00c04fd705a2',无法实例化,因为当前线程不在单线程单元中.

这是我程序的精简版.

private void Form1_Load(object sender, EventArgs e)
        {
            GetDataFromWebBrowser();
            Set_Auto_Refresh_Timer();
        }

private void Set_Auto_Refresh_Timer()
        {
            System.Timers.Timer TimerRefresh = new System.Timers.Timer(10000);
            TimerRefresh.Elapsed += new System.Timers.ElapsedEventHandler(TimerRefresh_Elapsed);
            TimerRefresh.AutoReset = true;
            TimerRefresh.Start();
        }    

private void TimerRefresh_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            GetDataFromWebBrowser();
        }

private void GetDataFromWebBrowser()
        {
            WebBrowser wb = new WebBrowser();  <--This is where the error is thrown.

            ...get web data...

        }

我想我那里有足够的代码来描绘图片.如您所见,当要创建另一个WebBrowser时,它将引发错误.

I think I got enough code in there to paint the picture. As you can see, when it gets to creating another WebBrowser, it throws the error.

我真的很沮丧,我只是开始在Threading上刮擦表面,这可能就是为什么我很沮丧的原因.

I'm really stumped and I'm just starting to scrape the surface on Threading which is probably why I'm so stumped.

//对我来说解决方案/ 最后,我将WebBrowser的创建移出了该方法,并使其静态化以仅重用WebBrowser控件.我也将System.Timers.Timer交换为System.Threading.Timer.似乎是要解决此问题.

//Solution for me/ I ended up moving the WebBrowser creation out of the method as well as making it static to just reuse the WebBrowser control. I also swapped my System.Timers.Timer to System.Threading.Timer. Seemed to fix the problem.

推荐答案

用于WebBrowser的MSDN文档指出:

WebBrowser类只能在设置为单线程单元(STA)模式的线程中使用.要使用此类,请确保您的Main方法已标记为[STAThread]属性.

此外,如果要定期与UI控件进行交互,请将System.Timers.Timer更改为System.Windows.Forms.Timer.或者,设置您的 SynchronizingObject 属性c0>到父控件,以强制您的计时器在正确的线程上调用调用.所有WinForms控件只能从同一,唯一的UI线程访问.

Also, change your System.Timers.Timer to a System.Windows.Forms.Timer if you want to interact with UI controls in regular intervals. Alternatively, set the SynchronizingObject property of your System.Timers.Timer to a parent control to force your timer to invoke calls on the right thread. All WinForms controls can only be accessed from the same, one and only UI thread.

.NET的BCL中存在三种类型的计时器,每种计时器的行为都非常不同.检查此MSDN文章以进行比较:

There are three types of timers in .NET's BCL, each of them acting very differently. Check this MSDN article for a comparison: Comparing the Timer Classes in the .NET Framework Class Library (web archive) or this brief comparison table.

这篇关于计时器和线程出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆