Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,228 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,298 people online right now. Registration is fast and FREE... Join Now!




streamwriter error

2 Pages V  1 2 >  
Reply to this topicStart new topic

streamwriter error

Damage
10 Oct, 2008 - 04:19 AM
Post #1

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 738



Thanked: 7 times
Dream Kudos: 75
My Contributions
I'm using this
CODE

Private Sub downloadFile(ByVal url As String, ByVal fileName As String)
        Dim webRequest As HttpWebRequest
        Dim webResponse As HttpWebResponse
        Dim imagepath As String = "E:\Anime\" & fileName
        Dim writeStream As New IO.FileStream(imagepath, IO.FileMode.Create)

        webRequest = HttpWebRequest.Create(url)
        webResponse = webRequest.GetResponse()

        Dim fileSize As Integer
        fileSize = webResponse.ContentLength
        Dim nRead As Integer
        nRead = 0
        Dim buffer(4096) As Byte
        Dim blockSize As Integer
        Dim tempStream As New MemoryStream

        Dim updateProgressBarDelegateSafe As New updateProgressBarDelegate(AddressOf updateProgressBar)
        Invoke(updateProgressBarDelegateSafe, 0)

        Do
            Dim readBytes(4095) As Byte
            blockSize = writeStream.Read(buffer, 0, 4096)
            Dim bytesread As Integer = webResponse.GetResponseStream.Read(readBytes, 0, 4096)
            nRead += bytesread
            If bytesread = 0 Then Exit Do
            writeStream.Write(readBytes, 0, bytesread)
            Dim percent As Short = (nRead * 100) / fileSize
            lblProgress.Text = percent.ToString
            Invoke(updateProgressBarDelegateSafe, percent)
            lblProgress.Text = percent.ToString
        Loop
       ' MsgBox("Done")
        webResponse.Close()
        writeStream.Close()
    End Sub


to download images from a website. Only problem is it doesn't work so great. It creates the file but sometimes I can't open the image(drawing failed) and it's soooooooooooooooooooooo slow. Is there a better way of doing this?
User is offlineProfile CardPM
+Quote Post

magicmonkey
RE: Streamwriter Error
10 Oct, 2008 - 07:32 AM
Post #2

D.I.C Regular
***

Joined: 12 Sep, 2008
Posts: 413



Thanked: 68 times
My Contributions
What version of .NET are you using? The later versions you can us the webclient to download files async...

vb

Dim wc As New Net.WebClient
AddHandler wc.DownloadProgressChanged, AddressOf wc_DownloadProgressChanged
AddHandler wc.DownloadFileCompleted, AddressOf wc_DownloadFileCompleted
wc.DownloadFileAsync(New Uri("http://"), localFile)


To speed up your code, you could either cache more bytes before you write them out to the file or if the files are small enough cache all the bytes then write out the file after the download is complete. Also don't update the UI as often, maybe every 25k or 100k.

Right now every loop you are downloading 4k, writing 4k to a file, calculating progress, and updating the progress 3 times, twice to the same lable and once using a delegate to the progress bar. If you are running this in a background thread all UI updates should be done via a delegate, if you are not running this in a background thread I am not sure why you are just using the delegate for the progress bar update. Clean this up a bit and don't update the ui every iteration, nor would i write out to the file every iteration.

And what is this doing?

vb

blockSize = writeStream.Read(buffer, 0, 4096)


You are reading from the stream you are writing your downloaded file too every iteration? And you never use BlockSize.
User is offlineProfile CardPM
+Quote Post

Damage
RE: Streamwriter Error
10 Oct, 2008 - 01:17 PM
Post #3

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 738



Thanked: 7 times
Dream Kudos: 75
My Contributions
sorry, using vs 2005 pro, .net 2.0 i think tongue.gif

as always great info, icon_up.gif

but I've never used the async file download. How exactly does it work? I've got a list of filenames stored in a text document, would i just loop through the list calling the method and passing the arguments and thats all? I don't need to specify buffer sizes and everything like i was doing originally just this:

CODE

    Dim wc As New Net.WebClient  
    wc.DownloadFileAsync(New Uri("http://"), localFile)  

is that right?

This post has been edited by Damage: 10 Oct, 2008 - 01:53 PM
User is offlineProfile CardPM
+Quote Post

Damage
RE: Streamwriter Error
10 Oct, 2008 - 02:11 PM
Post #4

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 738



Thanked: 7 times
Dream Kudos: 75
My Contributions
I'm still not doing it right. I'm trying to download a 21k gif and i get to 20k fine and then nothing.I get a message saying it's completed but it's not, so i'm not sure what i'm doing wrong

This post has been edited by Damage: 10 Oct, 2008 - 02:20 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Streamwriter Error
10 Oct, 2008 - 05:18 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,926



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Post the latest version of your code so we can see what you are doing.

User is offlineProfile CardPM
+Quote Post

Damage
RE: Streamwriter Error
10 Oct, 2008 - 06:07 PM
Post #6

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 738



Thanked: 7 times
Dream Kudos: 75
My Contributions
i'm using this now
CODE

        
        Dim wc As New Net.WebClient
         AddHandler wc.DownloadFileCompleted, AddressOf wc_DownloadFileCompleted
        wc.DownloadFileAsync(New Uri("http://img.4chan.org/b/res/90218658.html"), "1223660956.gif")



CODE

Public Sub wc_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
        MsgBox("Completed")

    End Sub


but i haven't had any success using either method
User is offlineProfile CardPM
+Quote Post

magicmonkey
RE: Streamwriter Error
11 Oct, 2008 - 07:05 AM
Post #7

D.I.C Regular
***

Joined: 12 Sep, 2008
Posts: 413



Thanked: 68 times
My Contributions
Is "http://img.4chan.org/b/res/90218658.html" an image or a HTML page?
User is offlineProfile CardPM
+Quote Post

Damage
RE: Streamwriter Error
11 Oct, 2008 - 01:35 PM
Post #8

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 738



Thanked: 7 times
Dream Kudos: 75
My Contributions
it's was an HTML it's been 404'd now.

basically what i've got going is I'm making like a scraper and i enter in the html, it extracts all the images links, cleans them up, writes them to a text document and from there i loop through each one and pass it to this method to download the image. Well thats the way i want it to work but at the moment its too slow and doesn't seem to finish properly, so i get an image written to my drive but when i open it theres nothing there.
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Streamwriter Error
11 Oct, 2008 - 02:54 PM
Post #9

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
Try using DownloadFile instead of DownloadFileAsync
User is offlineProfile CardPM
+Quote Post

Damage
RE: Streamwriter Error
11 Oct, 2008 - 03:10 PM
Post #10

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 738



Thanked: 7 times
Dream Kudos: 75
My Contributions
CODE

Dim wc As New Net.WebClient
      
        wc.DownloadFile(New Uri("http://orz.4chan.org/wg/6.html"), "1223753402.jpg")

        '   wc.Dispose()
        MsgBox("done")


nope no good, is there something insanely obvious that i'm not doing?
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Streamwriter Error
11 Oct, 2008 - 03:20 PM
Post #11

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
Your code looks fine to me.

But you should definitely use the DownloadFileAsync. It does not block the thread. Where DownloadFile does block the thread, and holds the entire application up.
User is offlineProfile CardPM
+Quote Post

Damage
RE: Streamwriter Error
11 Oct, 2008 - 03:31 PM
Post #12

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 738



Thanked: 7 times
Dream Kudos: 75
My Contributions
smile.gif its kinda of a moot point seeing as how it's not working properly tongue.gif


User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 03:12AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month