Question Network request timeout if it takes too long?

Johno

New member
Joined
Feb 18, 2010
Messages
3
Programming Experience
Beginner
Hi everyone,

I apologise now if I come across as a newbie but its because I am :) I've been using VB for exactly a week today and for that past 4 hours I've been pulling my hair out! My problem is that our software can only run on one PC on a site, if two instances are running they cause issues. So instead of checking each PC individually I created a program with VB to do this (and other tasks) for me. The problem with the network search is that if I cannot access a remote PC (usually access is denied) then it hangs for a long time before moving on to the next. Can I introduce a timeout feature where the program tries to access the c drive but if it does this for more than 5 seconds and gets nowhere then it moves on?

The code looks like this (red part being the part it hangs on)

If IO.Directory.Exists(testcpath) Then
If IO.File.Exists(netpath) Or IO.File.Exists(netpath2) Then
ListBox2.Items.Add(ListBox1.Items(y))
End If
Else : End If

'testcpath' is \\pcname\c$ and the other two paths look for the software I'd need to remove if found. So basically, if I have access to every c drive it works fine and completes within a few minutes. If I don't have access to even a small amount then it takes an absolute age.

I've searched forums and google for the past 4 hours and come up with nothing that would help. Or maybe I've just overlooked something :(

Thanks in advance!
Johno
 
Hi David,

thats what I was after. I've added the following which has worked great:

Public Class FileExistsHelper
Public Shared Function FileExists( _
ByVal file As String, _
ByVal timeOut As Integer) As Boolean
m_File = file
m_Exists = False
m_Thread = New System.Threading.Thread(AddressOf CallFileExists)
m_Thread.Start()
m_Thread.Join(timeOut)
m_Thread.Abort()
Return m_Exists
End Function

Private Shared Sub CallFileExists()
m_Exists = My.Computer.FileSystem.FileExists(m_File)
End Sub
Private Shared m_Thread As System.Threading.Thread
Private Shared m_File As String = String.Empty
Private Shared m_Exists As Boolean = False
End Class


and then in the section that had been causing me issues:

Dim b As Boolean = FileExistsHelper.FileExists(netpath, 2000)

This has worked for me and the search now takes less than a minute as opposed to the several it had been taking previously
 
Back
Top