9f4136a119aa096b3b4faf1c33a7e08e

This horrible code I inherited uses a GOTO. Anyone care to fix it?

Private Function fIsAppRunning() As Boolean
            'Looks to see if Lotus Notes is open

            Dim lngH As Integer
            Dim lngX, lngTmp As Integer
            Const WM_USER As Short = 1024
            On Error GoTo fIsAppRunning_Err
            fIsAppRunning = False

            lngH = FindWindow("NOTES", vbNullString)

            If lngH <> 0 Then
                SendMessage(lngH, WM_USER + 18, 0, 0)
                lngX = IsIconic(lngH)
                If lngX <> 0 Then
                    lngTmp = ShowWindow(lngH, 1)
                End If

                lngTmp = SetForegroundWindow(lngH)

                fIsAppRunning = True
            End If

fIsAppRunning_Exit:
            Exit Function

fIsAppRunning_Err:
            fIsAppRunning = False
            Resume fIsAppRunning_Exit
        End Function

Refactorings

No refactoring yet !

22e33503870d8e20493c4dd6b2f9767f

rikkus

September 25, 2008, September 25, 2008 16:56, permalink

1 rating. Login to rate!

This kind of error handling is idiomatic in VB.

9f4136a119aa096b3b4faf1c33a7e08e

hcase

September 25, 2008, September 25, 2008 23:33, permalink

No rating. Login to rate!

My own refactoring since noone seems interested.

Private Function IsAppRunning() As Boolean
	'Looks to see if Lotus Notes is open

	Dim lngH As Integer
	Dim lngX, lngTmp As Integer
	Const WM_USER As Short = 1024
	IsAppRunning = False
	Try
	  lngH = FindWindow("NOTES", vbNullString)
	  If lngH <> 0 Then
	    SendMessage(lngH, WM_USER + 18, 0, 0)
	    lngX = IsIconic(lngH)
	    If lngX <> 0 Then
	      lngTmp = ShowWindow(lngH, 1)
	    End If
	    lngTmp = SetForegroundWindow(lngH)
	    IsAppRunning = True
          End If
	Catch x As Error
		IsAppRunning = False
	End Try
End Function
503dc458e66746c5ac681e7057d209dd

McDole

September 26, 2008, September 26, 2008 16:24, permalink

No rating. Login to rate!

*Removed*

55502f40dc8b7c769880b10874abc9d0

https://me.yahoo.com/a/gRMed8kNn9bseqntTCs6dXkdMWMkQus-

February 16, 2011, February 16, 2011 12:52, permalink

No rating. Login to rate!

You don't need to set IsAppRunning, just use return true or false. It makes a bit more sense for someone reading it. I believe you also want to catch the system.exception as well - http://msdn.microsoft.com/en-us/library/system.exception.aspx. Hope this helps a little bit.

Wade

Private Function IsAppRunning() As Boolean
	'Looks to see if Lotus Notes is open

	Dim lngH As Integer
	Dim lngX, lngTmp As Integer
	Const WM_USER As Short = 1024

	Try
	  lngH = FindWindow("NOTES", vbNullString)
	  If lngH <> 0 Then
	    SendMessage(lngH, WM_USER + 18, 0, 0)
	    lngX = IsIconic(lngH)
	    If lngX <> 0 Then
	      lngTmp = ShowWindow(lngH, 1)
	    End If
	    lngTmp = SetForegroundWindow(lngH)
	    return True
          End If
	Catch x As Exception
	  return False
	End Try
End Function
0bcc811da9e7fe1a462d74e0ab402768

lamy

April 17, 2011, April 17, 2011 23:07, permalink

No rating. Login to rate!

you can also do this without directly using Windows API

Private Function IsAppRunning() As Boolean
        For Each p As Process In Process.GetProcesses
            If p.MainWindowTitle = "NOTES" Then
		Return True
            End If
        Next

        Return false
    End Function

Your refactoring





Format Copy from initial code

or Cancel