Hi
I have a module that handles threading with user32.dll Api.
The problem comes when each window calls this pSetForegroundWindow subrrutine to set the Foreground thread. The windows is displayed active but is not refreshed or painted, stays unresponsive. Why is that Can someone help
Would I have to declare a system.threading instance to substitute this module functionality And not use user32.dll in vb.net How can I do that I have an idea how threading works but this is the first time working with it
See Code Below
************************************* module********************************************
Option Strict Off
Option Explicit On
Imports System.Runtime.InteropServices
Module WinApiFunctions
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)
Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Boolean) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Integer, ByVal lParam As Integer) As Integer
Declare Function GetForegroundWindow Lib "user32" () As Integer
Declare Function GetParent Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function GetWindow Lib "user32" (ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"(ByVal hwnd As Integer, ByVal nIndex As Integer) As Integer
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA"(ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer
Declare Function IsIconic Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef lParam As String) As Integer
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef lParam As Integer) As Integer
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
' Constants used with APIs
'
Public Const SW_SHOW As Short = 5
Public Const SW_RESTORE As Short = 9
Public Const GW_OWNER As Short = 4
Public Const GWL_HWNDPARENT As Short = (-8)
Public Const GWL_EXSTYLE As Short = (-20)
Public Const WS_EX_TOOLWINDOW As Short = &H80s
Public Const WS_EX_APPWINDOW As Integer = &H40000
Public Sub pSetForegroundWindow(ByVal hwnd As Integer)
Dim lForeThreadID As Integer
Dim lThisThreadID As Integer
Dim lReturn As Integer
'
' Make a window, specified by its handle (hwnd)
' the foreground window.
'
' If it is already the foreground window, exit.
'
Try
If hwnd <> GetForegroundWindow() Then
'
' Get the threads for this window and the foreground window.
'
lForeThreadID = GetWindowThreadProcessId(GetForegroundWindow, 0)
lThisThreadID = GetWindowThreadProcessId(hwnd, 0)
'
' By sharing input state, threads share their concept of
' the active window.
'
If lForeThreadID <> lThisThreadID Then
' Attach the foreground thread to this window.
Call AttachThreadInput(lForeThreadID, lThisThreadID, True)
' Make this window the foreground window.
Dim blnBforeground As Boolean
blnBforeground = False
***Loop to obtain the foreground window
Do While Not (blnBforeground)
lReturn = SetForegroundWindow(hwnd)
If lReturn <> 0 Then
blnBforeground = True
Else
blnBforeground = False
End If
Loop
*******Loop to obtain the foreground window
' Detach the foreground window's thread from this window.
'It stops here for some reason....
Call AttachThreadInput(lForeThreadID, lThisThreadID, False)
Else
lReturn = SetForegroundWindow(hwnd)
End If
'
' Restore this window to its normal size.
'
If IsIconic(hwnd) = 0 Then
Call ShowWindow(hwnd, SW_RESTORE)
ElseIf Not IsIconic(hwnd) = 0 Then
Call ShowWindow(hwnd, SW_SHOW)
End If
End If
Catch e As Exception
Console.WriteLine("****Error Description: " & e.Message)
End Try
End Sub
End Module
**********************************End of Module *************************************

user32.dll Threads Issue from Upgrading from Vb6 to vb.net
SMang
Fitometer
Hi
I have am still stuck in the same problem. The forms shows but do not refresh or paints, why
Good question. I thought the module that I posted was causing the problem , so I commented the module and it continues behaving the same way.
Can you help me or suggestion
I am loosing my patience…
-Sharon< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
priyankak
ok.
If in this case I am not processing messages how can i set the foreground I just removed the do while but still the form shows and hangs.
-S
Caroline_C#
As I already said, "When it hangs, break in with the debugger to see what it is doing - why it is not processing messages." The UI thread will be in some function different from GetMessage. Figure out why that function is stuck.
Kaiser
Hi:
Running the debugger and looking at the threads window, there are three threads running a timer(set to normal), a window(set to normal) and an unknown thread running and it is set as the highest priority but does not give me any detail or information from were it is coming from. Is it normal to see this unknow thread
Apparently it is a pain event problem..still do not know how it is realated to these module..
-Sharon
YOU can close the thread...
cpsubrian
I already explained where your problem is. This loop
' Make this window the foreground window.
Dim blnBforeground As Boolean
blnBforeground = False
Do While Not (blnBforeground)
lReturn = SetForegroundWindow(hwnd)
If lReturn <> 0 Then
blnBforeground = True
Else
blnBforeground = False
End If
Loop
has two bugs.
1. If SetForegroundWindow fails you go into an infinite loop.
2. You are not processing messages in this loop. If the window you are trying to set focus to shares your input queue, it will wait for you to release foreground before it can take it. But you're not processing messages so the foreground change cannot complete.
Get rid of the loop. If SetForegroundWindow fails then accept that you cannot change the foreground window for whatever reason. If you keep retrying you will just keep failing.
Mike Droney
Hi:
How can i establish the message between windows Of what I have worked in this app I understand that I am not passing any messages that I know of between the windows. This function is called only whe it creates a form or calls an existing one to set it as the front most window no matter what applications are running.
Should I be checking for a GetQueueStatus or GetInputState Function to see what is causing the app to hang
-S
Bandit97
http://msdn.microsoft.com/library/default.asp url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/iswindowvisible.asp
Ernie100
"By sharing input state, threads share their concept of the active window."
They share much more than just the input state. Indeed, what you did was attach the two threads and then made the attacher hang! (not processing messages) This hangs both threads. I discussed this in detail in my PDC talk "Five things every win32 developer should know" - this is issue #5 as I recall.
itaihor
Can SOME ONE HELP !!!
me reference is from this website:Still having problems. It calls the current hwnd to bring to front and set it as foreground but the windows hangs. Their is no response from the form or any other that calls this subroutine.
When i call this subroutine it does not throw any error, that is what bugs me. Can SOME ONE HELP it has been over a week and I have a DEADLINE!!! that is over due.
Option Strict Off
Option Explicit On
Imports System.Runtime.InteropServices
Module WinApiFunctions
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)
Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Boolean) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Integer, ByVal lParam As Integer) As Integer
Declare Function GetForegroundWindow Lib "user32" () As Integer
Declare Function GetParent Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function GetWindow Lib "user32" (ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"(ByVal hwnd As Integer, ByVal nIndex As Integer) As Integer
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA"(ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer
Declare Function IsIconic Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function CloseWindow Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef lParam As String) As Integer
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef lParam As Integer) As Integer
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Integer) As Integer
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
' Constants used with APIs
'
Public Const SW_SHOW As Short = 5
Public Const SW_SHOWNA = 8
Public Const SW_RESTORE As Short = 9
Public Const GW_OWNER As Short = 4
Private Const WM_CLOSE = &H10
Public Const GWL_HWNDPARENT As Short = (-8)
Public Const GWL_EXSTYLE As Short = (-20)
Public Const WS_EX_TOOLWINDOW As Short = &H80s
Public Const WS_EX_APPWINDOW As Integer = &H40000
Public Sub pSetForegroundWindow(ByVal hwnd As Integer)
Dim lForeThreadID As Integer
Dim lThisThreadID As Integer
Dim lReturn As Integer
'
' Make a window, specified by its handle (hwnd)
' the foreground window.
'
' If it is already the foreground window, exit.
'
Try
If hwnd <> GetForegroundWindow() Then
'
' Get the threads for this window and the foreground window.
'
lForeThreadID = GetWindowThreadProcessId(GetForegroundWindow, 0)
lThisThreadID = GetWindowThreadProcessId(hwnd, 0)
'
' By sharing input state, threads share their concept of
' the active window.
'
If lForeThreadID <> lThisThreadID Then
' Attach the foreground thread to this window.
'Call AttachThreadInput(lForeThreadID, lThisThreadID, True)
' Make this window the foreground window.
Dim blnBforeground As Boolean
blnBforeground = False
Do While Not (blnBforeground)
lReturn = SetForegroundWindow(hwnd)
If lReturn <> 0 Then
blnBforeground = True
Else
blnBforeground = False
End If
Loop
' Detach the foreground window's thread from this window.
End If
'
' Restore this window to its normal size.
If IsIconic(hwnd) = 0 Then
Call BringWindowToTop(hwnd)
ElseIf IsIconic(hwnd) <> 0 Then
Call ShowWindow(hwnd, SW_SHOW)
End If
End If
'Sleep(5000)
'If IsHungAppWindow(hwnd) Then
' CloseWindow(hwnd)
'End If
Catch e As System.SystemException
Console.WriteLine("****Error Description: " & e.Message)
Catch e As System.ApplicationException
Console.WriteLine("****Error Description: " & e.Message)
Catch e As SEHException
Console.WriteLine("****Error Description: " & e.Message)
Catch e As Exception
Console.WriteLine("****Error Description: " & e.Message)
End Try
End Sub
Private Sub cmdClose_Click()
'Add code here to close falcon view
'Call SendMessage(, WM_CLOSE, 0, 0) 'This call will send the message to
'close the txt file
End Sub
End Module
hrishi1976
Tristian
Azhar_kazi
I moved your question to the Windows Forms forum, because that's where the people who really know the ins-and-outs of Windows forms hang out.
Best regards
Johan Stenberg
Lovelu
I ran debugger and it runs and does not go to any exception. The form displays and shows not responding in the form caption.
-s