WebBrowser control uses too much CPU and RAM - vb.net

In my application I have only a webbrowser and a few timers, but if the webbrowser is loading, it uses 50% of the CPU and afer 5 minutes around of 120k RAM. What can I do?
My code:
Public Sub work()
WebBrowser1.Navigate("Site")
tim.Start()
End Sub
Public Sub work2()
WebBrowser1.Document.GetElementFromPoint(point).InvokeMember("click")
tim2.Start()
tim.Stop()
End Sub
Dim point As New Point(800, 30)
Dim WithEvents tim As New Timer
Dim WithEvents tim2 As New Timer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tim.Interval = 6000
tim2.Interval = 6000
work()
End Sub
Private Sub tim_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tim.Tick
work2()
End Sub
Private Sub tim2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tim2.Tick
work()
End Sub

Long, Time ago since this was asked, but as i write this i have been working on my own project to create a YouTube app from Vb.net and, for a few weeks i have been stuck on trying to figure out why the IE Wrapper Web Control was taking so much of my CPU when Processing and Playing Videos, and from a lack of finding any good help with my problem that solved anything, i eventually researched enough and found a solution to my problem which were the use of Feature Registry Entries that i added for my program to check and add as needed on startup of the program, and the two i found which were a lot of help were both FEATURE_BROWSER_EMULATION and FEATURE_GPU_RENDERING, By Default i believe and could be wrong, the vb.net wrapper uses IE7 but if you change it to the latest version for your program in there it will make it more secure and improve performance, than you can turn on the gpu rendering with a DWORD subkey of 1 with the name of your program and it will make the gpu handle some of the workload also greatly improving the speed. using these made the webcontrol go from utilizing around 70-80% CPU during videos down to low of 3% and high of 20% CPU utilization, good for future reference, also learn more about the registry features here https://msdn.microsoft.com/en-us/library/ee330733(v=vs.85).aspx

120k sounds very reasonable if there is some heavy web page loaded in the control. E.g. if you save the main page of microsoft.com from within IE in a mht format (complete archive), it is almost 1MB large. The processor usage will most probably depend on what the click handler does - is there any heavy script attached to the element?

Related

What would be a proper way to end a .Net app?

I have an app I'm working on and I've given the user a button with the option to close the app at any time - but I'm not sure if this is the best way to go about it so I'm asking for some feedback just to double check.
Here's what I have:
Private Sub CloseButton_Click(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles CloseButton.Click
End
End Sub
For all I know this could be a really bad way to achieve this. Please be gentle!
Firstly, you need to close/dispose all the objects you have been instantiating.
Then you can close your application. And to do so, it is better to use Application.Exit Method.
According to MSDN, It should informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.
Private Sub CloseButton_Click(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles CloseButton.Click
Application.Exit
End Sub

PictureBox image quality is low

I was using the following code to enter an image url at TextBox1 and display it in PictureBox1.Everything worked.
But suddenly the displayed image quality is lower than the original link. I've tried PictureBox1.Load(TextBox1.Text) and changing SizeMode still the same result.
Local images are displayed normally.
Imports System.Net
Public Class Form1
Dim client As WebClient
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
client = New WebClient()
Dim ImageInBytes() As Byte = client.DownloadData(TextBox1.Text)
Dim ImageStream As New IO.MemoryStream(ImageInBytes)
PictureBox1.Image = New System.Drawing.Bitmap(ImageStream)
End Sub
End Class
I'm using
Microsoft Visual Studio 2010 Version 10.0.30319.1 RTMRel,
Microsoft .NET Framework Version 4.0.30319 RTMRel
How can I get the image to display without the quality loss?
My guess is that the website you are linking to provides a low quality image when hotlinking to it. This is common practice so that users visit the actual site. And it doesn't use so much bandwidth.
If this is the case then there is nothing you can do about it.
Actually it was problem with my internet connection,there is too much packetloss which affects the image quality.
I tested the code with another connection and it worked.

Getting Current Song Data From Windows Media Player 12

I have been searching all over for an answer to this, and I'm figuring there is no way to do it, but I thought I'd ask.
I'm looking to make a VB.net app that gets and displays the artist and title of a song playing in Windows Media Player. I'm not looking to embed the player in my app, just pull data from the already existing window.
Most of the stuff I find on how to do this is old or related to embedding WMP. I'm looking to do it in with MWP12. I've also seen references to using AxWindowsMediaPlayer.currentMedia Property, but that doesn't seem to work anymore. I used to be able to pull the data from the Registry, but it seems Microsoft took that away.
I've tried sooooo much other code, but this is currently what I have. I'm at my wits end.
Imports WMPLib
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim wmp As WindowsMediaPlayer = New WindowsMediaPlayer
Dim playlist As IWMPPlaylist = wmp.currentMedia.???????(????).Item(?)
Label1.Text = playlist.getItemInfo("Title")
End Sub
End Class
No idea where to go here, but thanks for any help.

Visual Basic 2008 - New Form on a different thread

My “form1” is just a simple page with buttons that launch different forms which do all the work, the "form1" code for the first four buttons is below.
What I want is for each form to run in a separate thread.
Public Class Main
Private Sub btnDownLoadStockPrices_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownLoadStockPrices.Click
LoadStocksFromDownloadSite.Visible = True
End Sub
Private Sub btnLoadOptionsIntoDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadOptionsIntoDatabase.Click
LoadOptionsIntoDatabase.Visible = True
End Sub
Private Sub btnVerifyDatabases_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerifyDatabases.Click
VerifyDatabase.Visible = True
End Sub
Private Sub btnAnalyzeStock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyzeStock.Click
AnalyzeSingleStock.visible = True
End Sub
End Class
I’ve found plenty of code examples to have different buttons on a single form run a subroutine in a separate thread, but for some reason I can’t seem to apply it to an entire form.
I think it’s something simple, like I need to tie each new thread as a handle to each forms “load” routine, but I just can’t get it to work. I don’t care about “synchronizing” threads at all, as each form is really a completely different functional program.
Any help would be much appriciated!
This isn't very common; generally it's best to limit all UI stuff to a single thread. But if you're convinced that you need each form to run on a separate thread, you must take into account the Windows API event handling model. The [over]-simplified version is that each form must have its own message loop to remove event messages from the queue and process them, so if you want to open a form on a new thread, you need to create that message pump.
The easiest way to do that is using the Application.Run method, and let the .NET Framework handle creating that message pump for you. For example:
Dim frm As Form1 = New Form1()
Application.Run(frm)
From looking at the code shown in your question, I can't discern any possible reason why those forms would need to run on separate threads. You can call the Show method of multiple forms so that they will be displayed on the screen at the same time. They won't block each other as long as you don't use the ShowDialog method, which displays each as a modal dialog. This is the way so many applications display multiple toolbox windows and other kinds of forms on the screen at the same time.
If you need to do some type of processor-intensive calculation, you still don't need to run each on a separate thread. Spin up a background thread (the BackgroundWorker class makes this very simple) and update the appropriate form's UI using the Invoke method.
You can certainly do this on Win32 but I don't know how well this maps over to .net.
The essential issue is that window handles have thread affinity. So you really need all interaction with them to happen in that thread. Essentially this means that you create all the window handles associated with that form in its thread. You also need to run a message loop in the thread.
The reason that people usually run all the UI out of the main thread and handle long-running actions in separate threads is that it is easier that way. You should ask yourself again why you want to do it this non-standard way.
I suspect you are not quite seeing the full picture. The need for threads in a desktop app principally arises when you have long running actions. Usually you want to keep your UI responsive and providing feedback for the long running action. Doing so leads to threads.
However, in your proposed solution you now have a multitude of extra threads and complexity, and you are still faced with the original problem! A long running action on one of your forms will hang it unless you perform that action in a separate thread, and once again we reach the standard solution to the problem.

vb.net more performance for moving objects

I have the mission to make a small game for a school project. Pictures boxes, moved by a timer for walking enemies.If there are around 5 or 6 moving picture boxes at the form, my application get troubles and lags. After I kill some enemies (remove them from the Controls Collection of the Form/Panel) It come back smooth.
I think the loop of the enemy movement is too complicated but I don't know how to make that simpler.
Private Sub TimerEnemyMovement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerEnemyMovement.Tick
For Each Enemy As Control In PanelBackground.Controls
If Enemy.Name.Substring(0, 5) = "Enemy" Then
_enemy.MoveEnemy(Enemy, 2)
End If
Next
End Sub
I also thought about Multithreading but not sure this would solve the problem and there is also the problem that I can't access the Controls of my mainform.
You see, I don't have much knowledge about vb.net
Any ideas how to fix that lag?
Try this:
Private Sub TimerEnemyMovement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerEnemyMovement.Tick
SuspendLayout()
For Each Enemy As Control In PanelBackground.Controls
If Enemy.Name.Substring(0, 5) = "Enemy" Then
_enemy.MoveEnemy(Enemy, 2)
End If
Next
ResumeLayout()
End Sub
VB.NET WinForm apps aren't the ideal setup to create games or moving objects. The Painting of the form uses to much performance as you have found out.
Try moving to a VB.NET WPF (Windows Presentation Framework) application it handles graphics a lot better.
Just a couple of suggestions, that may improve speed
a) Rather than going through through all the controls, how about storing them in an array / list
b) old game trick is to draw the scene in memory and then copy it to screen .. so why not have a memory bitmap (or graphic) draw the "enemies" to this and then copy to entire bmp to screen once all is done
Do all the enemies need to move at the same time? Do some move faster?
if there are 6 enemies, you could move 1,3,5 on the first tick, then 2.4.6 on the next tick etc.
?
I've been using XNA for creating a simple game, it was really cool. Though VB.NET is not officially supported, you can make it work. It's optimized for scenery and large number of animated objects.