Results 1 to 6 of 6

Thread: [RESOLVED] Controling Threads

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    120

    Resolved [RESOLVED] Controling Threads

    Code:
    Imports System.Threading
    Imports System.IO
    Public Class Form1
        Public iPath As String
        Public cnt As Integer
        Shared locker As Object = New Object()
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Startdownload.Click
            Dim sr As New StreamReader(TextBox1.Text)
            Dim strLine As String
            strLine = sr.ReadLine()
                Do Until strLine Is Nothing
                    cnt = cnt + 1
                    iPath = strLine
                    strLine = sr.ReadLine()
                    StartThread()
    
                Loop
            sr.Close()
        End Sub
    
    
        Private Sub DownloadFile()
             Dim url As String
            Dim localFileName As String
         
            On Error Resume Next
    
                My.Computer.Network.DownloadFile _
                    (url, _
                    localFileName)
            Next i
    
        End Sub
    
    
        Private Sub StartThread()
            Dim nthread As Thread
            nthread = New Thread(AddressOf DownloadFile)
            nthread.Start()
    
        End Sub
    End Class
    I am downloading 500 file from internet but it freeze my form utile it's completed so i have tried Thread but it starts all the Thread at once and i don't get all the files.... how can i control the Thread execution? i want that until one file is not completed dont go for other file or loop. i have tried SyncLock but really don't know how to use SyncLock plz help

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Controling Threads

    In situations like this where I need to let the thread finish before starting another I typically will have an event that is fired by the thread upon completion and I will then execute the next thread after that event fires and so on.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Controling Threads

    You don't really need more than one thread. If you intend to only download one file at a time then just start one thread and then download them all in that one thread. Can you let us know where and how you're getting the list of files to download?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    120

    Re: Controling Threads

    If you intend to only download one file at a time then just start one thread and then download them all in that one thread.
    can you show me how can i do this?

    Can you let us know where and how you're getting the list of files to download?
    URL links are in on text file i just read lines 1 by 1 and download to local HDD

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Controling Threads

    vb.net Code:
    1. For Each remotePath As String In IO.File.ReadAllLines("file path here")
    2.     'Download from remotePath here.
    3. Next remotePath
    Just put that in the DoWork event handler of a BackgroundWorker. There's a link to BGW example code in my signature.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    120

    Re: Controling Threads

    thanx jmcilhinney

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width