-
May 23rd, 2022, 04:34 PM
#1
Thread Starter
Addicted Member
Cannot Maximize form after it has been Minimized
After I minimize the main form, the form will stay minimized no matter what I do. I need to right click the program icon in the task bar and close it to restart the program. After some troubleshooting and research in this forum I found the code that is causing the problem. It is shown below in blue. If I comment out this code the problem is gone.
When I change the focus away from this form, like clicking on a browser, this code works as expected and finds all the objects I need in ManagementObjectSearcher. But, when the form is minimized, it will stay minimized unless I close the program. This should be an easy fix, but I don't know what it is. The only thing that comes to mind is to use a BackGroundWorker. Any ideas? Thanks for reading.
Code:
Private Sub MainForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Try
'SOME CODE HERE ......
Dim DummyString08 As String = ""
Dim searcher As New ManagementObjectSearcher(
"root\cimv2",
"SELECT * FROM Win32_PnPEntity")
For Each queryObj As ManagementObject In searcher.Get()
If Len(queryObj("Name")) > 10 Then
DummyString08 = Trim(LSet(queryObj("Name"), 14))
If LSet(DummyString08, 10) = "MyBoard" Then
lbxPairedBoards.Items.Add(DummyString08)
End If
End If
Next
Catch err As ManagementException
Dim el As New ErrorLogger
el.WriteToErrorLog(err.Message, err.StackTrace, "Error")
End Try
End Sub
-
May 23rd, 2022, 07:56 PM
#2
Re: Cannot Maximize form after it has been Minimized
Does the code get executed as expected when the form is minimised? You need to find out what is actually happening.
-
May 23rd, 2022, 08:04 PM
#3
Thread Starter
Addicted Member
Re: Cannot Maximize form after it has been Minimized
Originally Posted by jmcilhinney
Does the code get executed as expected when the form is minimised? You need to find out what is actually happening.
This code writes certain devices detected by the computer in a ListBox, so when I minimize the window I can't see if it worked. If I change the focus away from the program and go back to it, this code works. This code takes some time, probably a couple of seconds, to detect all the devices. I think that this delay might be the cause of the problem.
-
May 23rd, 2022, 09:58 PM
#4
Re: Cannot Maximize form after it has been Minimized
Originally Posted by VB-MCU-User
This code writes certain devices detected by the computer in a ListBox, so when I minimize the window I can't see if it worked.
Then it's a good thing that developers have additional tools available to them that regular users do not, the most important being the debugger built into VS.
Originally Posted by VB-MCU-User
This code takes some time, probably a couple of seconds, to detect all the devices. I think that this delay might be the cause of the problem.
Then you probably shouldn't be doing it on the UI thread regardless. You could use Async/Await or, if you feel more comfortable, you could use a BackgroundWorker. If you do the latter, you would gather the data in the DoWork event handler and then either update the UI incrementally as you are but in the ProgressChanged event handler, or gather all the data first and then update the UI in the RunWorkerCompleted event handler. You would also want to check the IsBusy property first and only kick off the process if its not already in progress.
-
May 24th, 2022, 05:52 PM
#5
Thread Starter
Addicted Member
Re: Cannot Maximize form after it has been Minimized
Originally Posted by jmcilhinney
Then it's a good thing that developers have additional tools available to them that regular users do not, the most important being the debugger built into VS.
Yes, I know. I'm learning.
Originally Posted by jmcilhinney
Then you probably shouldn't be doing it on the UI thread regardless. You could use Async/Await or, if you feel more comfortable, you could use a BackgroundWorker. If you do the latter, you would gather the data in the DoWork event handler and then either update the UI incrementally as you are but in the ProgressChanged event handler, or gather all the data first and then update the UI in the RunWorkerCompleted event handler. You would also want to check the IsBusy property first and only kick off the process if its not already in progress.
Thanks, I used the BackgroundWorker and it worked. I used the DoWork event handler to update the UI incrementally. Below is my modified code. I used the INVOKE method to write to a ListBox because the ListBox was created in the UI thread. Is this correct?
Code:
Private Sub MainForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Try
'SOME CODE HERE ......
BackgroundWorker1.RunWorkerAsync()
Catch err As ManagementException
Dim el As New ErrorLogger
el.WriteToErrorLog(err.Message, err.StackTrace, "Error")
End Try
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim DummyString08 As String = ""
Dim searcher As New ManagementObjectSearcher(
"root\cimv2",
"SELECT * FROM Win32_PnPEntity")
For Each queryObj As ManagementObject In searcher.Get()
If Len(queryObj("Name")) > 10 Then
DummyString08 = Trim(LSet(queryObj("Name"), 14))
If LSet(DummyString08, 10) = "MyBoard" Then
'lbxPairedBoards.Items.Add(DummyString08)
lbxPairedBoards.Invoke(Sub()
lbxPairedBoards.Items.Add(DummyString08)
End Sub)
End If
End If
Next
End Sub
-
May 24th, 2022, 08:15 PM
#6
Re: Cannot Maximize form after it has been Minimized
It's not incorrect but the whole point of the BackgroundWorker is that you can call methods and handle events like normal and not have to invoke any delegates directly. If only I told you exactly what to do in my previous post:
update the UI incrementally as you are but in the ProgressChanged event handler
Did you bother to investigate that at all? I'm guessing not. Follow the CodeBank link in my signature below and check out my thread on Using The BackgroundWorker for some explanation and examples.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|