如何在表单加载vb.net之前加快表单的加载时间,以便循环运行进程 [英] how do I speed up the load time of a form I'm looping through running process before the form loads vb.net

查看:69
本文介绍了如何在表单加载vb.net之前加快表单的加载时间,以便循环运行进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如上所述,我的表单在循环进程时加载缓慢。我正在捕捉所有访问被拒绝(5)例外,我认为这是减缓事情的原因。我甚至使用继续(我之前从未使用过)来查看是否会加快速度。我知道它有帮助。这是我的代码:

As stated, my form loads slow while looping through processes. I'm catching all the "access denied(5) exceptions" and I think that's what is slowing things down. I'm even using a "continue for" (which I've never used before) to see if that would speed it up. I know it helps though. Here's my code:

    Try
        strName = Process.GetProcessesByName(p.ProcessName)(0).MainModule.FileName.ToString()
    Catch
        Continue For
    End Try


    exeIcon = Icon.ExtractAssociatedIcon(strName)

    If strName.ToLower <> "idle" Or strName.ToLower <> "system" Then

        imgList.Images.Add(exeIcon)

        blist.Items.Add(imgList.Images(i))
        blist.Items.Add(strName)
        blist.Items.Add(p.Id)
        i += 1

    End If

Next





有谁知道我可以在哪里优化这段代码?有没有更快的方法来处理异常?也许有办法阻止更多的例外?谢谢你的回复。



Does anyone see where I can optimize this code? Is there a faster way to handle the exceptions? Maybe there's a way to block more of the exceptions? Thanks for looking.

推荐答案

循环过程可能很慢,而且你已经做好了准备异常,这又会导致延迟。



从你的代码中你不清楚你正在循环什么(For is is missing)但是如果你要使用 GetProcesses [ ^ ]在进入循环之前获取所有进程并避免不断搜索进程。



另一个(小)增强可能是使用使用 [ ^ ]阻止,例如 blist



另一件事是因为你不能制作这闪电般快,为什么不为用户显示启动屏幕并告诉进度。您可以让后台工作程序搜索进程,UI将显示始终发生的情况。这不会消除持续时间,但当您看到程序没有挂起时,它会提供更好的用户体验。
Looping through the processes may be slow and as you have done you have to be prepared for exceptions, which again cause delay.

From your code it's not clear what you're looping (the For is missing) but what if you would use GetProcesses[^] to get all the processes before entering the loop and to avoid constantly searching for the processes.

Another(small) enhancement could be the usage of a With[^] block, for example for blist.

Another thing is that since you can't make this lightning fast, why not show a 'splash' screen for the user and tell about the progress. You could have a background worker searching for the processes and the UI would show what happens all the time. This doesn't take away the duration but it gives a better user experience when you see that the program isn't hung.


@Mika嗯,我稍微更改了我的代码某种原因,它现在更快。到目前为止,这对我来说没有意义。也许以后会来找我。这是改变之后的代码。 ListView窗口只需2秒即可加载,我可以使用它。



@Mika Well, I changed my code just a bit and for some reason, it's faster now. So far, it doesn't make sense to me. Maybe it will come to me later. Here's the code after I changed it. The ListView windows only takes 2 seconds to load and I can live with that.

blist.Clear() 'clear listbox. In case listbox window has been closed then reopened

imgList.ImageSize = New Size(30, 30)

For Each p As Process In Process.GetProcesses

    Try

        strName = Process.GetProcessesByName(p.ProcessName)(0).MainModule.FileName.ToString()

        If strName.ToLower <> "idle" Or strName.ToLower <> "system" Then

            exeIcon = Icon.ExtractAssociatedIcon(strName)

            imgList.Images.Add(exeIcon)

            blist.Items.Add(imgList.Images(i))
            blist.Items.Add(strName)
            blist.Items.Add(p.Id)
            i += 1
        End If

        'MsgBox(p.Id)
    Catch
        Continue For
    End Try
Next





我将所有代码移到try块中。它现在有效。也许有人会告诉我如何:)感谢Mika的帮助



I moved all my code inside of the "try" block. It works now. Maybe someone will tell me how :) Thanks for the help Mika


这篇关于如何在表单加载vb.net之前加快表单的加载时间,以便循环运行进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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