-
Feb 10th, 2021, 03:32 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Form2 is opened repeatedly
I have a problem with following code:
Code:
Dim hWnd As Integer = FindWindowNullClassName(0, "form_number2")
If hWnd = 1 Then
Else
Form2.Show()
End If
Form2 is repeatedly opened every n-th seconds even window name is detected i.e. software is still running. What causes that behavior?
Please dont forget to add good reputation if my advices were useful for you.
How? Under this post there is "RATE THIS POST" button. Click on it.
-
Feb 10th, 2021, 04:37 PM
#2
Re: Form2 is opened repeatedly
What is the code in FindWindowNullClassName? Is this a custom method?
-
Feb 10th, 2021, 05:07 PM
#3
Re: Form2 is opened repeatedly
1 seems like an improbable value for an hWnd, if that really is an hWnd. If the return from the method isn't exactly 1, then you'd certainly drop into the else.
My usual boring signature: Nothing
-
Feb 10th, 2021, 06:38 PM
#4
Re: Form2 is opened repeatedly
I believe he tries to use an implementation of the FindWindow user32.dll to open a form on the same app.
Something like:
Code:
Dim nWnd As IntPtr
Dim zeroIntPtr As New IntPtr(0)
Dim Wnd_name as String
Wnd_name= "form_number2"
nWnd = FindWindow(Nothing, Wnd_name)
If nWnd.Equals(zeroIntPtr) Then
'No Form
Form2.Show()
Else
'Form Found
End If
Personally I would not use Windows API when there are similar .net core implementations or when I don't get a better result in something.
So I would be doing something like:
Code:
Dim ifrmcount As Integer = 0
Dim forms As FormCollection = Application.OpenForms
For Each cont As Control In forms
If TypeOf cont Is Form Then
If cont.Name = "form_number2" Then
ifrmcount = ifrmcount + 1
Exit For
End If
End If
Next
If ifrmcount <= 0 Then
Form2.Show()
Exit Sub
End If
Last edited by sapator; Feb 13th, 2021 at 04:53 PM.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Feb 11th, 2021, 12:38 AM
#5
Re: Form2 is opened repeatedly
I think hWnd will be 0 if no window found, else <> 0...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 13th, 2021, 12:19 PM
#6
Thread Starter
Hyperactive Member
Re: Form2 is opened repeatedly
Yeah, I am using this:
Code:
Public Declare Auto Function FindWindowNullClassName Lib "user32.dll" Alias "FindWindow" (ByVal lpClassName As Integer, ByVal lpWindownName As String) As Integer
@sapator
it does not work either.
Please dont forget to add good reputation if my advices were useful for you.
How? Under this post there is "RATE THIS POST" button. Click on it.
-
Feb 13th, 2021, 01:12 PM
#7
Re: Form2 is opened repeatedly
That is why I suggested the other way to do so.
Anyhow, this will work fine:
Code:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
ByVal childAfter As IntPtr, _
ByVal lclassName As String, _
ByVal windowTitle As String) As IntPtr
End Function
''''
Dim i As IntPtr
i = FindWindowEx(IntPtr.Zero, IntPtr.Zero, vbNullString, "form_number2") 'The .text of the form
If i.Equals(0) Then
' no form
End If
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Feb 13th, 2021, 01:25 PM
#8
Re: Form2 is opened repeatedly
Why use the API at all if Form2 is part of the same app? Application.OpenForms is the easier method...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 13th, 2021, 01:33 PM
#9
Re: Form2 is opened repeatedly
Code:
Dim isOpen as Boolean = False
For Each f As Form In Application.OpenForms.ToArray
If TypeOf f Is Form2 Then
If f.Text = "your Form2 title" Then
isOpen = True
End If
End If
Next
If isOpen = False Then
Form2.Show()
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 13th, 2021, 02:40 PM
#10
Re: Form2 is opened repeatedly
That's what I also suggested at post #4 but if he wants to do it with API then that's fine by me.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Feb 13th, 2021, 04:16 PM
#11
Re: Form2 is opened repeatedly
Originally Posted by sapator
That's what I also suggested at post #4 but if he wants to do it with API then that's fine by me.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 4th, 2021, 03:18 PM
#12
Thread Starter
Hyperactive Member
Re: Form2 is opened repeatedly
Both forms are independent.
I am interested why my prior code does not work:
Code:
Public Declare Auto Function FindWindowNullClassName Lib "user32.dll" Alias "FindWindow" (ByVal lpClassName As Integer, ByVal lpWindownName As String) As Integer
Dim hWnd As Integer = FindWindowNullClassName(0, TextBox1.Text)
If hWnd = 1 And Process.GetProcessesByName("ExpressionEvaluatorGUI").Length > 0 Then
Else
MsgBox("Process NOT found!", MsgBoxStyle.Critical)
End If
albeit it is rewritten from videotutorial "how to check if a process is running in visual basic net 2010" where it works fine via window name.
textbox1.text value is "ExpressionEvaluatorGUInazov" - exactly rewritten window name from another project. Of course EEGUInazov is running all the time...
process name is also correct.
Please dont forget to add good reputation if my advices were useful for you.
How? Under this post there is "RATE THIS POST" button. Click on it.
-
Mar 4th, 2021, 03:53 PM
#13
Re: Form2 is opened repeatedly
Originally Posted by VB.NET Developer
Both forms are independent.
I am interested why my prior code does not work:
....
Post #3 and Post #5?
What is the purpose of comparing hWnd to 1? The chances of it being 1 is probably several billion to one.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Mar 5th, 2021, 09:48 AM
#14
Thread Starter
Hyperactive Member
Re: Form2 is opened repeatedly
My prior code was incomplete - there was "counter", but I prior thought that its not neccessary since the videoguy also checked how many processess are detected. But now it works flawlessly!
Code:
Public Declare Auto Function FindWindowNullClassName Lib "user32.dll" Alias "FindWindow" (ByVal lpClassName As Integer, ByVal lpwindowName As String) As Integer
Dim counter As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hWnd As Integer = FindWindowNullClassName(0, TextBox1.Text)
If hWnd > 0 And Process.GetProcessesByName("ExpressionEvaluatorGUI").Length > 0 Then
MsgBox("Process found")
Else
MsgBox("Process NOT FOUND!", MsgBoxStyle.Critical)
End If
End Sub
Please dont forget to add good reputation if my advices were useful for you.
How? Under this post there is "RATE THIS POST" button. Click on it.
-
Mar 5th, 2021, 11:26 AM
#15
Re: [RESOLVED] Form2 is opened repeatedly
Yes, of course the introduction of a new variable that isn't actually used in your code fixed it...
...and not the fact that you also changed the logic of your If statement just like was suggested in several earlier posts...
Keep on keeping on.
Good luck.
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
|