VB.NET--NullReferenceException on secondary form load - vb.net

Background
I have a Winform application (VB.NET framework 4.0). Within the application are two forms: frmHome (startup form) and frmdePDF (secondary form with an Adobe PDF Reader COM control). frmHome grabs a PDF pathway from DFS, checks if the pathway actually exists, and if so, loads the file into my PDF control and shows frmdePDF. Now, this works 100% of the time on my local machine. However, the initial load on any other machine throws a NullReferenceException. Other events (e.g. my datagridview CellContentClick) that call the SAME code works fine on all machines. I understand (I hope correctly) that the issue is initializing that frmdePDF the first time.
Code
The code that loads the datagridview (dgDE) and pulls the PDF pathway:
Private Sub btnDELegalLOMCState_Click(sender As Object, e As EventArgs) Handles btnDELegalLOMCState.Click
loadStatus = "State"
Dim deState As String = Nothing
''Validate and set state
If Me.cbDEState.SelectedIndex = 0 Then
MessageBox.Show("Please select a state.")
Exit Sub
Else
deState = Me.cbDEState.Text
End If
''Populate dgDE/set data bindings
Try
Me.cbDERegion.ComboBox.SelectedIndex = -1
Me.dgDE.DataSource = Nothing
Me.SpSelectLOMAbyStateTableAdapter.Fill(Me.DevGISDataSet.spSelectLOMAbyState, 4, deState)
Me.dgDE.DataSource = Me.SpSelectLOMAbyStateBindingSource
Me.bnDE.BindingSource = Me.SpSelectLOMAbyStateBindingSource
Me.txtDEIssueDte.DataBindings.Clear()
Me.txtDECaseNum.DataBindings.Clear()
Me.txtDECommNum.DataBindings.Clear()
Me.txtDEIssueDte.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.SpSelectLOMAbyStateBindingSource, "IssueDte", True))
Me.txtDECaseNum.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.SpSelectLOMAbyStateBindingSource, "CaseNum", True))
Me.txtDECommNum.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.SpSelectLOMAbyStateBindingSource, "CommNum", True))
Catch ex As Exception
MessageBox.Show("Error loading LOMAs: " & ex.Message.ToString)
End Try
''Load PDF
If Me.dgDE.RowCount <> 0 Then
Dim i As Integer = Me.dgDE.CurrentRow.Index
Dim pathdePDF As String = Me.dgDE.Rows.Item(i).Cells(5).Value.ToString
LoaddePDF(pathdePDF)
End If
End Sub
The Public Sub that contains code to load the file and show frmdePDF:
Public Sub LoaddePDT(ByVal pathdePDF As String)
If System.IO.File.Exists(pathdePDF) Then
frmdePDF.pdfLOMC.LoadFile(pathdePDF)
frmdePDF.Show()
Else
MessageBox.Show("Image not available. Please check FEMA and CAMSIS.")
frmdePDF.Hide()
End If
End Sub
Other things I have tried
Creating a new instance of the form each time:
Public Sub LoadPDF_test(ByVal pathdePDF As String)
Dim openForms = Application.OpenForms.OfType(Of frmdePDF)()
While openForms.Any
openForms.First.Close()
End While
If System.IO.File.Exists(pathdePDF) Then
Dim newfrmdePDF As New frmdePDF
newfrmdePDF.pdfLOMC.LoadFile(pathdePDF)
newfrmdePDF.Show()
Else
MessageBox.Show("Image not available. Please check FEMA and CAMSIS.")
End If
End Sub
This code works on my machine 100%. It behaves the same way as the first load code on any other machine (throws the NullReferenceException).
Exception Text
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at SaveLOMC.frmHome.LoadPDF_test(String testPath) in C:\Data\Alycia\Development\LOMC\Projects\TFS_test\SaveLOMC\SaveLOMC\frmHome.vb:l ine 46
at SaveLOMC.frmHome.btnDENewLOMCState_Click(Object sender, EventArgs e) in C:\Data\Alycia\Development\LOMC\Projects\TFS_test\SaveLOMC\SaveLOMC\frmHome.vb:l ine 440
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
I have tried stepping through the debugger on my local machine. But again, I can't recreate the error on it so the objects all have assigned values.
I know this question has been asked endlessly. I have read through all of these (plus MORE!):
What is a NullReferenceException, and how do I fix it?
"Object reference not set to an instance of an object?"
Object reference not set to an instance of an object. vb.net looking up a string
visual basic: object reference not set to an instance of an object
Visual Basic - Object reference not set to an instance of an object
VB.Net How to set an object reference to an instance of an object?
object reference not set to an instance of an object. vb.net for some but not all code
Object reference not set to an instance of an object.
specific error on :"Object reference not set to an instance of an object."
Object reference not set to an instance of an object
"Object reference not set to an instance of an object" message
object reference not set to an instance of an object error showing
Additional information: my local machine is 64 bit, the other machines are 32 bit. The associated .dll files for the Adobe component are 32 bit. My Target CPU in the Application Properties is x86.
Any advice is appreciated, I wouldn't ask this broad question if I hadn't exhausted my options.
Solution
There was nothing wrong with the form load event--I was trying to manually set the location/bounds of the form on load. Set the bounds to a non-existent screen (my computer has three screens--everyone else has 2). Learning experience?

Related

form.Invoke(delegate) Cannot access a disposed object. .net

Enviroment: vb.net visual studio 10 - win forms
The Error:
{"Cannot access a disposed object. Object name:'frmInfo'."}
StackTrace:
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at HorizonRealTime.UI.frmInfo.UpdateGUI(cHorizonColoredString strdata) in R:\csavb2010dlls\Test\TestSoloutions\StoreAutoStuff\HorizonRealTimeVB10\CommanderRealTime.UI\frmInfo.vb:line 93
at HorizonRealTime.UI.frmInfo.DoWork() in R:\csavb2010dlls\Test\TestSoloutions\StoreAutoStuff\HorizonRealTimeVB10\CommanderRealTime.UI\frmInfo.vb:line 75
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Simple Explanation:
I have 1 main form with many child mdi forms.
One form(frminfo) manages all the status/error messages that server processes generate.
These messages get placed on a
Imports System.Collections.Concurrent
Public oStatusMsgs As BlockingCollection(Of String) = New BlockingCollection(Of String) 'thread safe collection
The problem occurs very rarely(usually just on application startup) when it enters the if me.invokerequired { me.Invoke(delegate)} I get a object disposed error when it tries to invoke itself.
even in debug the object is not flagged as disposed after the error occurs.
if I wrap the me.invoke in a try/catch... it procedes to process the following messages just fine.
Output in textbox ends up looking like this:
---Refreshing Data---
#Items Data Refreshed#
#Deps Data Refreshed#
Real-Time Info - Error Displaying GUI Info - #Units Data Refreshed# - Cannot access a disposed object. Object name: 'frmInfo'.
#Retail Data Refreshed#
#Tax Data Refreshed#
---Exiting Data Refresh---
Whats funny is that this textbox is on frminfo that im copying the text out of.
Whats even stranger is. It is actually processing messages prior to getting this error. So the disposed error exception seems to be a false reading, and something else is actually occurring.
On frminfo is a low-priority thread that loops infinitely basically like this
Private sub DoStuff() 'this is kicked off from the frminfo.onload event as a new thread that is referenced by frminfo
dim strdata as string = nothing
do while active
If Me.frmparent.oStatusMsgs.Count > 0 Then
Me.frmparent.oStatusMsgs.TryTake(strdata, 300)
If Not strdata Is Nothing Then
Call me.UpdateGUI(strdata)
End If
strdata = Nothing
End If
thread.sleep(50)
loop
End sub
Private Sub UpdateGUI(ByVal strdata As string)
If Me.InvokeRequired Then
Try
Me.Invoke(New UpdateGUIDelegate(AddressOf UpdateGUI), New Object() {strdata})
Catch ex As Exception
Me.frmparent.oErrorMsgs.Add(Me.Text & " - Error Displaying GUI Info - " & strdata & " - " & ex.Message)
End Try
Else
'this is where it would append the strdata to some kind of textbox or other form level control
end if
end sub
On the main UI thread I do create addition references to this mdi child form.
The first is a collection I add every mdi child form to. I let the GC handle this entirely other then the add.(I do this to ensure my forms are not disposed of)
There is also a collection of forms that I use to sort the mdi childs. The
in some cases i do like
dim otmpinfofrm as form = frmcollection(i)
frmcollection(i) = nothing 'where this is where frminfo currently resides in the collection
frmcollection(i) = frmcollection(i+1)
I loop through the collection and at the end i put back in the reference to
frmcollection(lastindex) = otmpinfofrm
I dont think this sorting could impact the thread calling me.invoke to update a local control. Since Im just sorting references?
Any help would be appreciated regarding better use of the me.invoke to avoid this error from happening at all.

VB.NET Gives error sometimes with custom fonts

I have a problem, that VB.NET throws an error runtime with my custom fonts, but not always. Sometimes it works, sometimes not. here's my code:
Dim path As String
Private Sub MMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
path = "C:/MMCTools/"
Try
Dim privateFonts As New System.Drawing.Text.PrivateFontCollection()
privateFonts.AddFontFile(path & "font.ttf")
Dim font20 As New System.Drawing.Font(privateFonts.Families(0), 20)
Dim font14 As New System.Drawing.Font(privateFonts.Families(0), 14)
Label1.Font = New Font(font20, FontStyle.Regular)
Label2.Font = New Font(font14, FontStyle.Regular)
Catch
MsgBox("Hiba")
End Try
If My.Computer.FileSystem.FileExists(path & "username.mcusr") Then
PictureBox1.ImageLocation = _
"http://mestermc.hu/3dskin/3d.php?a=0&w=0&wt=0&abg=0&abd=0&ajg=0&ajd=0&ratio=13format=png&displayHairs=true& headOnly=false&login=" _
& (ReadALine(path & "username.mcusr", GetNumberOfLines(path & "username.mcusr"), 0))
Else
MCUsr.Show()
Me.Close()
End If
End Sub
The error message:
************** Error contents **************
System.ArgumentException: Invalid parameter.
at: System.Drawing.FontFamily.GetName(Int32 language)
at: System.Drawing.FontFamily.get_Name()
at: System.Windows.Forms.Internal.WindowsFont.FromFont(Font font, WindowsFontQuality fontQuality)
at: System.Windows.Forms.Internal.WindowsGraphicsCacheManager.GetWindowsFont(Font font, WindowsFontQuality fontQuality)
at: System.Windows.Forms.TextRenderer.MeasureText(String text, Font font, Size proposedSize, TextFormatFlags flags)
at: System.Windows.Forms.Layout.LayoutUtils.MeasureTextCache.GetUnconstrainedSize (String text, Font font, TextFormatFlags flags)
at: System.Windows.Forms.Layout.LayoutUtils.MeasureTextCache.TextRequiresWordBrea k(String text, Font font, Size size, TextFormatFlags flags)
at: System.Windows.Forms.Label.CreateTextFormatFlags(Size constrainingSize)
at: System.Windows.Forms.Label.CreateTextFormatFlags()
at: System.Windows.Forms.Label.OnPaint(PaintEventArgs e)
at: System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at: System.Windows.Forms.Control.WmPaint(Message& m)
at: System.Windows.Forms.Control.WndProc(Message& m)
at: System.Windows.Forms.Label.WndProc(Message& m)
at: System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at: System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at: System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam
This happens when fast switching forms.
Just came across the error myself and figured it out. You are defining the private font collection in the load event and it goes out of scope thereby making it eligible for garbage collection. So depending on how fast it gets collected it may or may not still be around when needed.
I'll tell you how you do it. Don't actually import the fonts into your program but import it on to your computer.
Just access the:
Control Panel -> Appearance and Personalization -> Fonts
And drag the fonts into the folder. Next time, Visual Studio will load the font. This is for windows. If you are using another computer, search how to load fonts onto computer.
I've got your mistake here!!!! It's very easy to solve.
Path most be written with "\" not "/" that's all of it ^^
path = "C:\MMCTools\"

Updating Variable in Multithreading in VB.NET

I've wrote a program which on startup loads the computer list from Active Directory. This takes about 10 seconds. If the user has started the program with a specific host as parameter, it should be usable immediately.
So to don't interrupt the user I want to load the computer list in a different thread. The problem is that it writes to a variable (the computer list) which is also used in the main thread.
You may think, I could simply use a temporary variable and when its done overwrite the main variable. But I have to keep existing data of the main variable.
'hosts list
Private Shared hosts As New SortedDictionary(Of String, HostEntry)
'Get all computers in Active Directory
'Will run in a extra thread
Private Delegate Sub GetADcomputersDelegate()
Private Sub GetADcomputers()
If Me.InvokeRequired Then
Me.Invoke(New GetADcomputersDelegate(AddressOf GetADcomputers), Nothing)
Else
lblStatusAD.Text = "Getting Computers..."
Try
Dim search As New DirectorySearcher(ActiveDirectory.Domain.GetCurrentDomain().GetDirectoryEntry(), "(objectClass=computer)")
For Each host As SearchResult In search.FindAll()
'AddHost creates a new HostEntry object and adds it to my "global" hosts variable
'It also checks if a host is already present in the list and only updates it.
AddHost(host.GetDirectoryEntry().Properties("cn").Value.ToLower(), host.GetDirectoryEntry().Properties("description").Value)
Next
Catch ex As Exception
Debug.WriteLine("GetADcomputers() Exception: " & ex.Message)
End Try
ThreadPool.SetMaxThreads(hosts.Count, hosts.Count)
Dim ah As String = activehost
'Fill my ListBox with the computers
lstHosts.DataSource = New BindingSource(hosts, Nothing)
'Select the computer that was selected before
UseHost(ah)
lblStatusAD.Text = ""
End If
End Sub
So when GetADcomputers() runs in its own thread, the main thread is also blocked. I guess because auf the hosts variable.
So what could I change to make the thread do it's work and after that apply the updated computer list without losing data of entries in old hosts list? And all this in a fast and efficient way.
That code is very wrong. If you call that method on a secondary thread then it immediately marshals a call back to the UI thread and does EVERYTHING on the UI thread. What you should be doing is executing all the background work on the secondary thread and then marshalling to the UI thread ONLY to update the UI.
Get rid of that If...Else block and just make the entire body of the method what's current ly in the Else block. Next, identify all the lines that specifically interact with the UI and remove each of those to their own method. You then add If...Else blocks to each of those methods so that only the code that actually touches the UI is executed on the UI thread.
Here's a start:
Private Sub GetADcomputers()
UpdateStatusADLabel("Getting Computers...")
Try
Dim search As New DirectorySearcher(ActiveDirectory.Domain.GetCurrentDomain().GetDirectoryEntry(), "(objectClass=computer)")
For Each host As SearchResult In search.FindAll()
'AddHost creates a new HostEntry object and adds it to my "global" hosts variable
'It also checks if a host is already present in the list and only updates it.
AddHost(host.GetDirectoryEntry().Properties("cn").Value.ToLower(), host.GetDirectoryEntry().Properties("description").Value)
Next
Catch ex As Exception
Debug.WriteLine("GetADcomputers() Exception: " & ex.Message)
End Try
ThreadPool.SetMaxThreads(hosts.Count, hosts.Count)
Dim ah As String = activehost
'Fill my ListBox with the computers
lstHosts.DataSource = New BindingSource(hosts, Nothing)
'Select the computer that was selected before
UseHost(ah)
lblStatusAD.Text = ""
End Sub
Private Sub UpdateStatusADLabel(text As String)
If lblStatusAD.InvokeRequired Then
lblStatusAD.Invoke(New Action(Of String)(AddressOf UpdateStatusADLabel), text)
Else
lblStatusAD.Text = text
End If
End Sub

Handling an exception thrown from inside a dll

I'm working on a project for school in which a dll is loaded.
The dll that is loaded is a bridge between my program and the Twincat System Manager which forms the bridge between the computer and a PLC via the local network.
I need to read Variables trough this whole chain from the plc to my program.
This is the way I do this:
Public Function adsReadReal(ByVal Variabelenaam As String) As Single
Dim ds = New TwinCAT.Ads.AdsStream(4 * 8) ' new data stream
Dim br = New System.IO.BinaryReader(ds) 'new binary
Dim hVar = New Integer
Try
ConState(1)
tcclient = New TcAdsClient
ConState(2)
tcclient.Connect(Form1.amsAdress, 801) 'connects the tcclient to the PLC
hVar = tcclient.CreateVariableHandle(Variabelenaam) 'creats a handle for the variable
tcclient.Read(hVar, ds) 'read it
ConState(5)
Return br.ReadSingle() 'convert it from binary to readable for vb
Catch ex As Exception
ConState(0)
PrintEx(ex) 'print out the exception
finally
tcclient.Dispose() 'make the object stop being used to prevent a lingering connection
End Try
Return False
End Function
Now the program loads a dll called TwinCAT.ADS.dll at the start of the connection module. If the Twincat system manager is running the program ends normally, but when it is not it crashes and gives me this error:
System.DllNotFoundException was unhandled
Message="Kan DLL tcadsdll.dll niet laden: Kan opgegeven module niet vinden. (Uitzondering van HRESULT: 0x8007007E)"
Source="TwinCAT.Ads"
TypeName=""
StackTrace:
bij TwinCAT.Ads.Internal.TcAdsDllWrapper.TcAdsDll.AdsAmsUnRegisterRouterNotification()
bij TwinCAT.Ads.Internal.TcAdsDllWrapper.AmsUnRegisterRouterNotification(Boolean
throwAdsException)
bij TwinCAT.Ads.Internal.TcLocalSystem.Dispose(Boolean disposing)
bij TwinCAT.Ads.Internal.TcLocalSystem.Finalize()
which is roughly translated to:
Cannot load DLL tcadsdll.dll: Cannot find given module. (Exception at
HRESULT: 0x8007007E)
This is not a dll that I have imported, so it must be from the TwinCAT.ADS.dll
How can I prevent the program from throwing this error at me and instead close the program peacefully? I have tried to catch all the exceptions of every dll related operation possible.
Also the source is on Bitbucket. I will make it public on request.
Some links on the official but quite unhandy Beckhoff site:
http://infosys.beckhoff.com/espanol.php?content=../content/1034/tcquickstart/html/tcquickstart_samplevisualbasicnet.htm&id=10449
Edit:
Apparently using tcclient.dispose() causes the error since the finnaly statement was use instead of just after the try block
Edit: This currently catches the exception but it does not handle it.
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
AddHandler currentDomain.UnhandledException, AddressOf MyHandler
Dim tick As Byte = 0
Sub MyHandler(sender As Object, args As UnhandledExceptionEventArgs)
Dim ex As Exception = DirectCast(args.ExceptionObject, Exception)
MsgBox("exception tick" & Str(tick))
tick = tick + 1
PrintEx(ex)
End Sub
Edit:
The exception isn't caught properly because in vs2008 a couple of errors occurs but the tick appears after I press F5 (continue)
When the program is run directly, I only see 1 Tick. Then windows gives an error.
Did you try an unhandled exception handler?
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
AddHandler currentDomain.UnhandledException, AddressOf MyHandler
Sub MyHandler(sender As Object, args As UnhandledExceptionEventArgs)
Dim e As Exception = DirectCast(args.ExceptionObject, Exception)
Console.WriteLine("MyHandler caught : " + e.Message)
End Sub

IndexOutOfRangeException in VB.NET

Picked up a legacy VB.NET project originally written for version 1.1 of the .NET framework. I'm running Vista with .NET 3.5. I have cleared out all the original error and the project will build; it just wont run.
As far as I can tell, it is trying to run 'LoginForm' but putting breakpoints in doesn't work because the error is thrown before the breakpoints are reached, regardless of where in the file they are placed.
Really can't work out what to do! Any help appreciated.
StackTrace:
System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="FirstLine"
StackTrace:
at FirstLine.LoginForm.main()
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
EDIT: Terribly sorry, didn't appreciate the code would be of much use because the issue is more that I can't get to it. However, here's the main function:
Shared Sub main()
Dim p As Process() = Process.GetProcessesByName("FirstLine")
If p.Length = 1 Then
'START COPYMINDER
'Dim expirydate As Date = CDate("01/01/1970")
'Dim expiry As Integer
'Try
' GetCopyMinderExpiryDate(expiry)
' If Not expiry = 0 And Not expiry = 1 Then
' expirydate = expirydate.AddSeconds(expiry)
' Dim diff As Integer = DateDiff(DateInterval.Day, Date.Now, expirydate)
' If diff >= 0 And diff 0 Then
' DisplayError((ret_code))
' End
'End If
'Dim did As String
'GetCopyMinderDeveloperID(did)
'If did "IT" Then
' MessageBox.Show("Invalid Developer ID " & did & ". Firstline will now shutdown", "Firstline", MessageBoxButtons.OK, MessageBoxIcon.Error)
' End
'End If
'END COPYMINDER
Dim lf As New LoginForm
If LoginSettings.setting("loginShowErrorOnLine") = "TRUE" Then
lf.ShowDialog()
Else
Try
lf.ShowDialog()
Catch ex As Exception
MsgBox(ex.Message)
Config.UnlockByUser(Config.currentUser.username)
Config.currentUser.UserLoggedOff()
End Try
End If
Else
Dim prc As Process = p(0)
SwitchToThisWindow(prc.MainWindowHandle, True)
End If
End Sub
Thanks for your responses so far. It's encouraging to see such a helpful community!
Dim prc As Process = p(0) is your problem as it is in the else statements where the the array length can be anything but 1 (0 for instance).
when the length is 0 it will give you the IndexOutOfRange when you try to access the first element.
Try turning on debug build; this will give you the possibility to add breakpoints and will also give you some row numbers at the stack trace.
Your problem is that you are not handling the case where p.Length = 0. This occurs if there is no process with the name "FirstLine".
Have you also renamed your process / application ?