简体   繁体   English

锁定的浏览器表单

[英]Locked webBrowser form

How do I solve a crash problem with webBrowser?如何解决 webBrowser 的崩溃问题?

This code:这段代码:

 private void button1_Click(object sender, EventArgs e)
        {

           BackgroundWorker backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
            backgroundWorker.RunWorkerAsync();
            _progresso = new Progresso();
            _progresso.ShowDialog();
        }

 private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        _progresso.Close();
        _impressaoBoleto.webBrowser.Navigate(_arquivo);
        _impressaoBoleto.ShowDialog();


    }

it generates several images and presents it in the webBrowser, however when I have more than 100 images it locks the application and raises the process of the machine.它生成多个图像并将其显示在 webBrowser 中,但是当我有超过 100 个图像时,它会锁定应用程序并提升机器的进程。

How to solve this?如何解决这个问题?

You are eating the systems resources.你正在吃系统资源。 (You can see that if you start you task manager, go to details, and add the "Handles" column. Then watch it grow as your program runs) (你可以看到,如果你启动你的任务管理器,进入细节,并添加“句柄”列。然后看着它随着你的程序运行而增长)

There are two things you can consider:您可以考虑两件事:

The IDisposable Interface IDisposable 接口

Always dispose of objects that contain the IDisposable interface.始终处理包含 IDisposable 接口的对象。

For example, try to change that code:例如,尝试更改该代码:

 _progresso = new Progresso();
 _progresso.ShowDialog();

to:到:

using (_progresso = new Progresso())
{
 _progresso.ShowDialog();
}

Instance creation实例创建

Also, the Backgroundworker needs to be disposed properly or instantiated only once.此外,Backgroundworker 需要正确处理或仅实例化一次。

You are excessively using resources when you let it run that way.当您让它以这种方式运行时,您就过度使用了资源。 Try using one instance instead of creating new instances and registering to their events evertime you click.尝试使用一个实例,而不是每次单击时创建新实例并注册到其事件。

You could, for example, remove that lines:例如,您可以删除该行:

// BackgroundWorker backgroundWorker = new BackgroundWorker();
// _progresso = new Progresso();

and put it them into your constructor.并将它们放入您的构造函数中。

Also, when your class is not implementing IDisposable, implement it.此外,当您的类没有实现 IDisposable 时,请实现它。 Then dispose them as well, by implementing the disposable interface..然后通过实现一次性接口来处理它们。

    protected override void Dispose(bool disposing);

If helpful, please accept the answer..如果有帮助,请采纳答案。。

Greetings, Mike问候,迈克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM