Application.Run() 导致程序挂起 [英] Application.Run () Cause to Hang Program

查看:64
本文介绍了Application.Run() 导致程序挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有一些链接,我必须在网页浏览器中一一打开这些链接并在网页中输入一些数据.

I have some links in my program which I must open the links in Web Browser one by one and enter some data in Web page.

链接列表在 GUI 线程中提供.但是我需要对主线程之外的链接做一些工作,以避免滞后和挂起 GUI.我需要一个接一个地打开链接.我的意思是如果一个线程开始工作,下一个线程只有在线程完成工作时才会开始.

The links list is provided in GUI Thread. But I need to do some works with links outside of Main Thread to avoid of Lagging and Hanging the GUI. And I need to open links one after the other. I mean if a thread starts to working, the next thread will start only when the thread finish works.

关于链接列表的另一个重要的事情是,当线程被创建和工作时,链接列表正在更新.

Another important thing about Links list is that when threads are created and working, Links List is updating.

所以我创建了一个工作线程来管理另一个线程的启动.

So I created a worker thread which manages starting of another Threads.

我有两个 AutoResetEvents,一个用于检查列表是否为空 (postInQueue).如果列表为空,则等待链接添加到列表并调用 postInQueue.set().

I have two AutoResetEvents, one for checking the list that is empty or not (postInQueue). If the list is empty, it's wait until a link will added to list and calls postInQueue.set().

第二个是threadInProgress,当一个线程开始工作时,它会一直等到线程调用threadInProgress.set();

The second one is threadInProgress that when a thread is started working, it's waiting until the thread calls threadInProgress.set();

AutoResetEvent threadInProgress = new AutoResetEvent(False);
AutoResetEvent postInQueue = new AutoResetEvent (False);
List<String> links = new List<String>;
public MainForm(){
InitializeComponent();
    Thread threadManager = new Thread( () => 
    {
        while(true){
            if (postsQueue.Count == 0)
                postInQueue.WaitOne();
            Thread t2 = new Thread(() => {
                linkProcess(links[0]);
            };
            t2.SetApartmentState(ApartmentState.STA);
            t2.Start();
            threadInProgress.WaitOne(60000);
            links.RemoveAt(0);
        }
    });
    threadManager.start();
}
public void linkProcess(String link){
    WebBrowser webBrowser = new WebBrowser();
    webBrowser.DocumentCompleted += (s , e) => {
        //Enter some data in webBrowser
        Application.ExitThread();
        threadInProgress.set();
    };
    webBrowser.Navigate(link);
    Application.Run();
}

我必须在每个线程中调用 Application.Run()DocumentCompleted 事件将被调用.

I Must call Application.Run() in each thread to DocumentCompleted Event will be called.

此代码适用于两个或三个第一个链接,但下一个线程将卡在 Application.Run() 中,直到 threadInProgress.WaitOne(60000); 发送超时.

This code works well for two or three first links but next threads will be stuck in Application.Run() until threadInProgress.WaitOne(60000); send timeout.

前两个链接工作正常,但后来我意识到 CPU 使用率为 0%.当我点击 break all 时,我看到线程卡在 Application.Run() 中.

Two first links works correct but then I realized that CPU usage is 0%. When I click on break all, I see that the thread is stucking in Application.Run().

有什么问题?

在论坛中,有人建议我使用线程以外的进程...这怎么可能?会有帮助吗?

In a forum, someone advise me to use process except thread... How can this be possible? And would be helpful?

推荐答案

好的... 调用Application.Run()的时候没找到挂掉的原因.我搜索了互联网,发现的只是一些关于 Application.Run() 的问题,有不同的场景,但没有答案!

Ok... I coudn't find out the reason of hanging when calling Application.Run(). I searched internet and all I found was some questions about Application.Run() with different scenarios but without answer!

但至少我找到了一种在另一个线程中使用 WebBrowser.DocumentComplete 事件而不使用 Application.Run() 的方法!

But at least I found a way to use WebBrowser.DocumentComplete event in another thread without using Application.Run()!

我开始一个线程坚持 Application.Run() 运行直到 WebBrowser.DocumentComplete 事件将被调用并完成它的工作!

I start a Thread insist of Application.Run() which run until WebBrowser.DocumentComplete event will be call and finish it works!

Thread t1 = new Thread ( () => {

    AutoResetEvent keepMeAlive = new AutoResetEvent( False );

    webBrowser1.DocumentComplete += (s , e) => {
        //Do Some Stuff

        keepMeAlive.set();
    };

    webBrowser1.Navigate("www.google.com");

    Thread keepAlive = new Thread ( () =>{
        keepMeAlive.WaitOne();
    });
    keepAlive.Start();
});

这篇关于Application.Run() 导致程序挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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