Stop FTP file downloading - vb.net

This is my sample code run under thread to download file from ftp server. In that, if the user want to stop file download, i tried to abort the thread. If the control in the while loop, its hang up.
How to close the binaryreader and Stream, when reader in the middle of stream
Using response As FtpWebResponse = CType(ftp.GetResponse, FtpWebResponse)
Using input As Stream = response.GetResponseStream()
Using reader As New BinaryReader(input)
Try
Using writer As New BinaryWriter(File.Open(targetFI.FullName, FileMode.Create)) 'output)
Dim buffer(2048) As Byte '= New Byte(2048)
Dim count As Integer = reader.Read(buffer, 0, buffer.Length)
While count <> 0
writer.Write(buffer, 0, count)
count = reader.Read(buffer, 0, buffer.Length)
End While
writer.Close()
End Using
Catch ex As Exception
'catch error and delete file only partially downloaded
targetFI.Delete()
'Throw
ret = False
End Try
reader.Close()
End Using
input.Close()
End Using
response.Close()
End Using

You will need to add "polling" within your While loop to check if a certain condition (in your case, the user wishes to abort the download) is true. If the condition is true, you can exit from the while loop.
For example, you will presumably have a function that is called when the user wishes to stop the download (this is perhaps in response to a Button click on a user interface or some such mechanism).
Using a class level boolean variable (or property), you can simply set this variable to true in response to the user wishing to abort the download, then within your while loop that reads portions of the file from the FTP response stream, you check the value of this variable and if it's true, you simply exit from the while loop:
For example:
Somewhere at the class level, you declare:
Dim blnAbort as Boolean = False
When the user (for example) clicks a button to abort the download:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
blnAbort = True
End Sub
And in your main While Loop from the code in your question, you add:
While count <> 0
' Check the value of the blnAbort variable. If it is true, the user wants to abort, so we exit out of our while loop'
If blnAbort = True Then
Exit While
End If
writer.Write(buffer, 0, count)
count = reader.Read(buffer, 0, buffer.Length)
End While
This is the basic mechanism (polling) by which you should abort a long running process. Of course, you should always ensure that the relevant clean-up code is performed in the event of aborting (in your case, closing the reader and writer, which you are already doing). You may also need to make a call to Application.DoEvents in your while loop if this is being done in the context of a Windows Forms based application, and the user aborting is controlled by some king of GUI interaction. This will ensure that Windows messages generated by (for example) a button click are processed in a timely fashion.

Related

Check the file whether is it by another process

I have some ask to you to check and advice if my code is enough and correct. My application works on files sometimes its downloading multiple files by windows service and then by other windows service parser process those files are parsing. Unfortunately before process file has to be finished downloading before process parser should open the file and play with it therefore i prepared some code to check firstly whether the file is free - means not open by another process or some person, whatever. I use most cases streamreader to open the file and play with it but as i said i would like firstly check file if its ready.
This is my code, what do you think:
Public Shared Function FileInUse(ByVal sFile As String, ByVal log As String) As Boolean
Dim thisFileInUse As Boolean = True
Dim counter As Integer = 0
For i = 0 To 30
counter += 1
If System.IO.File.Exists(sFile) Then
Try
Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
thisFileInUse = False
Exit For
End Using
Catch
System.Threading.Thread.Sleep(10000) '10 sec
End Try
End If
Next
Return thisFileInUse
End Function

Shared Resource in Parallel.ForEach

How do I control access to a shared resource in a Parallel.ForEach loop? I am trying to download multiple files in parallel, and I want to capture information about downloads that fail, so that the user can re-attempt the download later. However, I am worried that if more than one download fails at the same time, the application will throw an exception because one thread will attempt to access the file while it is being written to by another.
In the code below, I would like to know how to control access to the file at RepeateRequestPath. A RequestSet is a list of strings that represent IDs of the resource I am trying to download.
Dim DownloadCnt As Integer = 0
Dim ParallelOpts As New ParallelOptions()
ParallelOpts.MaxDegreeOfParallelism = 4
Parallel.ForEach(RequestSets, ParallelOpts, Sub(RequestSet)
Try
DownloadCnt += 1
Dim XmlUrl As String = String.Format("{0}{1}{2}", "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=", String.Join(",", RequestSet), "&retmode=xml&rettype=abstract")
DownloadFile(XmlUrl, String.Format("{0}\TempXML{1}.xml", XMLCacheDir, DownloadCnt))
Catch ex As WebException
Using Response As WebResponse = ex.Response
Dim statCode As Integer = CInt(DirectCast(Response, HttpWebResponse).StatusCode)
MessageBox.Show(String.Format("Failed to retrieve XML due to HTTP error {0}. Please hit the 'Retrieve XML' button to re-run retrieval after the current set is complete.", statCode))
If Not File.Exists(RepeatRequestPath) Then
File.WriteAllLines(RepeatRequestPath, RequestSet)
Else
File.AppendAllLines(RepeatRequestPath, RequestSet)
End If
End Using
End Try
End Sub)
The usual way to protect a shared resource in VB.NET is to use SyncLock.
So, you would create a lock object before the Parallel.ForEach() loop:
Dim lock = New Object
and then you would use that inside the loop:
SyncLock lock
File.AppendAllLines(RepeatRequestPath, RequestSet)
End SyncLock
Also note that you can use AppendAllLines() even if the file doesn't exist yet, so you don't have to check for that.
You need to use a semaphore to control access to a shared resource. You want only one thread to access the error file at one time, so initialize the semaphore to only allow 1 thread in. Calling _pool.WaitOne should seize the semaphore, and then release it once it finishes creating/writing to the file.
Private Shared _pool As Semaphore
_pool = = New Semaphore(0, 1)
Dim DownloadCnt As Integer = 0
Dim ParallelOpts As New ParallelOptions()
ParallelOpts.MaxDegreeOfParallelism = 4
Parallel.ForEach(RequestSets, ParallelOpts, Sub(RequestSet)
Try
DownloadCnt += 1
Dim XmlUrl As String = String.Format("{0}{1}{2}", "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=", String.Join(",", RequestSet), "&retmode=xml&rettype=abstract")
DownloadFile(XmlUrl, String.Format("{0}\TempXML{1}.xml", XMLCacheDir, DownloadCnt))
Catch ex As WebException
Using Response As WebResponse = ex.Response
Dim statCode As Integer = CInt(DirectCast(Response, HttpWebResponse).StatusCode)
MessageBox.Show(String.Format("Failed to retrieve XML due to HTTP error {0}. Please hit the 'Retrieve XML' button to re-run retrieval after the current set is complete.", statCode))
_pool.WaitOne()
Try
If Not File.Exists(RepeatRequestPath) Then
File.WriteAllLines(RepeatRequestPath, RequestSet)
Else
File.AppendAllLines(RepeatRequestPath, RequestSet)
End If
Catch ex as Exception
'Do some error handling here.
Finally
_pool.Release()
End Try
End Using
End Try
End Sub)
svick's solution is almost right. However if you need to protect access to a shared variable you also need to declare your lock object as shared at class level.
This works correctly:
Friend Class SomeClass
Private Shared _lock As New Object
Private Shared sharedInt As Integer = 0
Sub Main()
SyncLock _lock
sharedInt += 1
End SyncLock
End Sub
End Class
If you use a non-shared lock object, synclock will protect the variable only from multiple accessing threads within the same instance, not across instances.

The asynchronous operation has already completed

I have a VB.net app with a DataGridView displaying a list of jobs, retrieved from a SQL query into a DataTable, then a DataView and finally a BindingSource for the DataGridView.DataSource. I have recently added SqlNotificationRequest functionality to the setup so user A is immediately updated when user B performs an action on a job in the list.
I used this MSDN article (http://msdn.microsoft.com/en-US/library/3ht3391b(v=vs.80).aspx) as the basis for my development and it works ok. The problem comes when the user wants to change the parameters of the SQL query which displays the jobs, for example the date displayed. Currently I am creating a new SqlCommand with new Notification but after the user has changed the date a few times (say 30), but no data changes, when the notification timeout occurs I receive the above error when the callback handler tried to EndExecuteReader. My callback handler is below:
Private Sub OnSalesReaderComplete(ByVal asynResult As IAsyncResult)
' You may not interact with the form and its contents
' from a different thread, and this callback procedure
' is all but guaranteed to be running from a different thread
' than the form. Therefore you cannot simply call code that
' updates the UI.
' Instead, you must call the procedure from the form's thread.
' This code will use recursion to switch from the thread pool
' to the UI thread.
If Me.InvokeRequired Then
myStatus.addHistory("OnSalesReaderComplete - Background", "Sub")
Dim switchThreads As New AsyncCallback(AddressOf Me.OnSalesReaderComplete)
Dim args() As Object = {asynResult}
Me.BeginInvoke(switchThreads, args)
Exit Sub
End If
' At this point, this code will run on the UI thread.
Try
myStatus.addHistory("OnSalesReaderComplete - UI", "Sub")
Dim sourceText, rSalesId As String
waitInProgressSales = False
Trace.WriteLine(String.Format("Sales:asynResult.IsCompleted1: {0}", asynResult.IsCompleted.ToString), "SqlNotificationRequest")
Trace.WriteLine(String.Format("Sales:asynResult.CompletedSynchronously: {0}", asynResult.CompletedSynchronously.ToString), "SqlNotificationRequest")
Dim reader As SqlDataReader = DirectCast(asynResult.AsyncState, SqlCommand).EndExecuteReader(asynResult)
Trace.WriteLine(String.Format("Sales:asynResult.IsCompleted2: {0}", asynResult.IsCompleted.ToString), "SqlNotificationRequest")
Do While reader.Read
' Empty queue of messages.
' Application logic could parse
' the queue data to determine why things.
'For i As Integer = 0 To reader.FieldCount - 1
' 'Debug.WriteLine(reader(i).ToString())
' Console.WriteLine(reader(i).ToString)
'Next
Dim bytesQN As SqlBytes = reader.GetSqlBytes(reader.GetOrdinal("message_body"))
Dim rdrXml As XmlReader = New XmlTextReader(bytesQN.Stream)
Do While rdrXml.Read
Select Case rdrXml.NodeType
Case XmlNodeType.Element
Select Case rdrXml.LocalName
Case "QueryNotification"
sourceText = rdrXml.GetAttribute("source")
Case "Message"
rSalesId = rdrXml.ReadElementContentAsString
End Select
End Select
Loop
Loop
reader.Close()
' The user can decide to request
' a new notification by
' checking the check box on the form.
' However, if the user has requested to
' exit, we need to do that instead.
If exitRequestedSales Then
'Me.Close()
commandSales.Notification = Nothing
Else
Select Case sourceText.ToLower
Case "data"
Trace.WriteLine(String.Format("SalesId: {0}, data notification", rSalesId), "SqlNotificationRequest")
Call GetSalesData(True, action.REATTACH)
Case "timeout"
'check timeout is for this user and relates to current wait thread
Select Case salesId = rSalesId
Case True
Trace.WriteLine(String.Format("SalesId: {0}, timeout - current", rSalesId), "SqlNotificationRequest")
Call GetSalesData(True, False)
Case False
Trace.WriteLine(String.Format("SalesId: {0}, timeout - old", rSalesId), "SqlNotificationRequest")
Me.ListenSales()
End Select
End Select
End If
Catch ex As Exception
Call errorHandling(ex, "OnSalesReaderComplete", "Sub")
End Try
End Sub
The problem seems to be where I am only refreshing the data and renewing the notification request (using GetSalesData) when either the notification is due to an update ("data") or the timeout is for the current request. On other notifications, the application calls Me.ListenSales which creates a SQL command "WAITFOR (RECEIVE * FROM [QueueMessage])" and starts the callback listener as per the MSDN article. If I remove the Me.ListenSales line, once a notification is received which is not due to data or the timeout of the current query, the application stops listening for notifications.
I have also posted the same question on MSDN forums (http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/0f7a636d-0c9b-4b39-b341-6becf13873dc) as I have tried other resolutions without success so require some advice on whether this is possible and if so how.

vb.net: listbox.items.add() throws exception in same class

I'm not even sure I understand this situation enough to come up with a proper title. I come from a modest understanding of VB6 and having to climb a steep learning curve for VB 2010.
I am trying to create a multi-client server program that will communicate with my Enterprise iPhone app. I found a relatively simple example to build upon here: http://www.strokenine.com/blog/?p=218. I have been able to modify the code enough to make it work with my app, but with one glitch: I can't get access to the controls on the form to add items, even though the method is invoked within the form's class. (I tried this on the original code too, and it does the same thing. I don't know how the author managed to get it to work.)
Here's the code segment in question:
Public Class Server 'The form with the controls is on/in this class.
Dim clients As New Hashtable 'new database (hashtable) to hold the clients
Sub recieved(ByVal msg As String, ByVal client As ConnectedClient)
Dim message() As String = msg.Split("|") 'make an array with elements of the message recieved
Select Case message(0) 'process by the first element in the array
Case "CHAT" 'if it's CHAT
TextBox3.Text &= client.name & " says: " & " " & message(1) & vbNewLine 'add the message to the chatbox
sendallbutone(message(1), client.name) 'this will update all clients with the new message
' and it will not send the message to the client it recieved it from :)
Case "LOGIN" 'A client has connected
clients.Add(client, client.name) 'add the client to our database (a hashtable)
ListBox1.Items.Add(client.name) 'add the client to the listbox to display the new user
End Select
End Sub
Under Case "LOGIN" the code tries to add the login ID to the listbox. It throws an exception: "A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll" The listbox (all controls, for that matter) is in the same class, Server.vb and Server.vb [Design].
The data comes in from another class that is created whenever a client logs on, which raises the event that switches back to the Server class:
Public Class ConnectedClient
Public Event gotmessage(ByVal message As String, ByVal client As ConnectedClient) 'this is raised when we get a message from the client
Public Event disconnected(ByVal client As ConnectedClient) 'this is raised when we get the client disconnects
Sub read(ByVal ar As IAsyncResult) 'this will process all messages being recieved
Try
Dim sr As New StreamReader(cli.GetStream) 'initialize a new streamreader which will read from the client's stream
Dim msg As String = sr.ReadLine() 'create a new variable which will be used to hold the message being read
RaiseEvent gotmessage(msg, Me) 'tell the server a message has been recieved. Me is passed as an argument which represents
' the current client which it has recieved the message from to perform any client specific
' tasks if needed
cli.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing) 'continue reading from the stream
Catch ex As Exception
Try 'if an error occurs in the reading purpose, we will try to read again to see if we still can read
Dim sr As New StreamReader(cli.GetStream) 'initialize a new streamreader which will read from the client's stream
Dim msg As String = sr.ReadLine() 'create a new variable which will be used to hold the message being read
RaiseEvent gotmessage(msg, Me) 'tell the server a message has been recieved. Me is passed as an argument which represents
' the current client which it has recieved the message from to perform any client specific
' tasks if needed
cli.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing) 'continue reading from the stream
Catch ' IF WE STILL CANNOT READ
RaiseEvent disconnected(Me) 'WE CAN ASSUME THE CLIENT HAS DISCONNECTED
End Try
End Try
End Sub
I hope I am making sense with all this. It all seems to bounce back and forth, it seems so convoluted.
I've tried using Me.listbox1 and Server.listbox1 and several other similar structures, but to no avail.
I'm reading a lot about Invoke and Delegates, but would that be necessary if the method and the control are in the same class? Or do I have a fundamental misperception of what a class is?
Many thanks for any help I can get.
Private Delegate Sub UpdateListDelegate(byval itemName as string)
Private Sub UpdateList(byval itemName as string)
If Me.InvokeRequired Then
Me.Invoke(New UpdateListDelegate(AddressOf UpdateList), itemName)
Else
' UpdateList
' add list add code
ListBox1.Items.Add(itemName)
End If
End Sub
Add above, then replace:
ListBox1.Items.Add(client.name)
to
UpdateList(client.name)
Does it work? check the syntax, may have typo as I type it.

Don't get any data from socket when data should be there, and no exceptions raised, connection is open. Using DataAvailable to wait for data

I have a problem reading data from an RFID-reader. I connect to the reader by tcp and wait for DataAvailable to be true, then reading the data until I got a end of data character. Then I just go back and waiting for a new DataAvailable. This is done in a own thread for the function.
There seems to be some kind of timeout, If I don't got any data in a couple of minutes, then it just sits there in the do/loop waiting for DataAvailable. I hold the card to the RFID reader, it beeps, but there is no data available. I don't get any exceptions, and the information says that the clientsocket is still connected. Are there anything more I can check?
If I put the card to the reader in a minute-interval it seems as this will never occur. So 2-3 minutes idle:ing seems to do this.
Here are my code to read data from the socket, I have taken away some irrelevant code:
Sub test(ByVal ip As String, ByVal port As Integer)
' this sub is meant to run forever
Try
Dim clientSocket As New System.Net.Sockets.TcpClient()
clientSocket.Connect(ip, port)
Using serverStream As NetworkStream = clientSocket.GetStream()
Do 'loop forever or until error occur
'Every new dataentry starts here
Dim inStream(0) As Byte
Dim returndata As String = ""
Do 'loop forever
Do Until serverStream.DataAvailable 'loop until data exists to read
If clientSocket.Connected = False Then
'this will never happen.
'but if there are more than 5 minutes between data then
'it never got data again as if no data was sent.
Exit Sub
End If
Application.DoEvents()
Loop
'there is data to read, read first byte and
serverStream.Read(inStream, 0, 1)
If inStream(0) = 13 Then
'got end of data
'exit loop if reading chr 13.
returndata &= System.Text.Encoding.ASCII.GetString(inStream)
Exit Do
End If
Loop
GotData(returndata)
Loop
End Using
Catch ex As Exception
' handle error
Finally
'close connection if open
End Try
End Sub
I found out that socket.Connected only reports the status for when the last command was runned on the connection.
So in the Do Until serverStream.DataAvailable loop I used this trick to check if the connection was closed instead:
Dim test As Boolean = clientSocket.Client.Poll(10, System.Net.Sockets.SelectMode.SelectRead)
If test = True And serverStream.DataAvailable = False Then
'restart connection
End If
So now finally got control over what is happening and know that its because of the client connection is closed that I dont got any data.
So, then I figured, now that I know that the connection is closed, how do I prevent it? That was easier, just send data to the tcpip-server every 10 second and it will hold it open.
The result that works for me (this is not the production code, just an example of the solution):
Dim s As Date = Now
Do Until serverStream.DataAvailable
Dim r As Boolean = clientSocket.Client.Poll(10, System.Net.Sockets.SelectMode.SelectRead)
If DateDiff(DateInterval.Second, s, Now) > 10 Then
Dim dta(0) As Byte
dta(0) = 0
clientSocket.Client.Send(dta)
s = Now
End If
If r = True And serverStream.DataAvailable = False Then
'restart sub
Exit Sub
End If
loop
So now it doesnt even close and need to restart every x minutes.
My problems are solved and Im a happy coder. ;)