11 Replies - 885 Views - Last Post: 11 April 2013 - 09:03 AM Rate Topic: -----

#1 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 968
  • Joined: 09-May 09

Background worker issue

Posted 31 March 2013 - 02:33 PM

Hi Guys

I new to threading, im attempting to improve the performance of making multiple web requests by initiating multiple threads to do the fetching.

The code below works but not as expected. It fetches the posters but seems to be fetching old posters too.

If I look at the save location I can see the posters being added but I also see previously added posters being overwritten.

Can anyone guide me to a resolution

 'LOAD ALL DIRECTORIES INTO ARRAY
        Dirs = IO.Directory.GetDirectories(parentFolder)

        Dim p As Integer = 0
        While p < +Dirs.Count - 1

            'REGISTER KEY
            Dim api = New WatTmdb.V3.Tmdb("638a9eb1f9682879f74313fdf68876768")
            Dim sizes = api.GetConfiguration()

            'EXTRACT NAME
            Dim temp As String = Dirs(p)
            Dim tempName() As String = temp.Split("\")
            Dim tempMovName = tempName(tempName.Count - 1)

            Dim movName = tempMovName.Replace(".", " ")

            'GET MOVIE ID
            Dim basicMovie = api.SearchMovie(movName, 1)
            Dim movieID = basicMovie.results(0).id

            'SAVE LOCATION
            Dim pPath = "C:\Users\Kevin\Documents\Visual Studio 2012\Projects\Movie Contents\Posters"

            'CALL REQUEST THREAD
            Dim request As New WebRequest
            request.makeWebRequest(pPath, movName, movieID)

            p = p + 1
        End While




'WEB REQUEST CLASS
Option Explicit On
Option Strict On
Imports System.Threading



Public Class WebRequest
    Dim WorkerThread As Thread                  'Create the worker thread
  
    Private movieName As String = Nothing
    Private posterLocation As String = Nothing
    Private movie As String = Nothing

    Public Sub makeWebRequest(ByVal posterPath As String, ByVal movName As String, ByVal movID As String)

        'SET VARIABLES 
        movieName = movName
        posterLocation = posterPath
        movie = movID

        'START NEW BACKGROUND WORKER THREAD
        WorkerThread = New Thread(AddressOf t_GetMoviePoster)
        WorkerThread.IsBackground = True
        WorkerThread.Start()

    End Sub

    Private Sub t_GetMoviePoster()


        'REGISTER KEY
        Dim api = New WatTmdb.V3.Tmdb("638a9eb1f9682879f74313987987987", "en")
        Dim sizes = api.GetConfiguration()

        Dim webClient1 As New System.Net.WebClient

        Try

            'DOWNLOAD POSTER
            Dim k As Integer = sizes.images.poster_sizes.Count - 4
            While k >= 0

                Try
                    'QUERY BASIC MOVIE INFO AND EXTRACT MOVIE ID
                    Dim basicMovie = api.SearchMovie(movieName, 1)
                    Dim movieID = basicMovie.results(0).id

                    'GET DETAILED INFORMATION
                    Dim MovieInfo = api.GetMovieInfo(movieID, "en")

                    'BUILD URL AND DOWNLOAD POSTER 
                    Dim poster_Url = "http://cf2.imgobject.com/t/p/" + sizes.images.poster_sizes(k) + MovieInfo.poster_path
                    webClient1.DownloadFile(poster_Url, posterLocation + "\" + movieName + ".jpg")
                    webClient1.Dispose()

                Catch ex As Exception

                End Try
            End While

        Catch ex As Exception

        End Try

        'END THREAD
        Thread.CurrentThread.Abort()
    End Sub


End Class

thanks in advance for any help :)/>/>


This post has been edited by deery5000: 31 March 2013 - 02:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Background worker issue

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,466
  • Joined: 29-May 08

Re: Background worker issue

Posted 31 March 2013 - 04:21 PM

TIP: Don't treat file paths as strings.
Why?
  • Paths have a restricted set of allowable characters in them.
  • The underlying system determines what those are, including the directory separator.


Don't use
Dim newPath = path1 + "\" + path2
' or
Dim newPath = path1 & "\" & path2


use
Dim newPath = Path.Combine(path1,path2)



Your code is really confusing.

What is this doing?
           'EXTRACT NAME
           Dim temp As String = Dirs(p)
           Dim tempName() As String = temp.Split("\")
           Dim tempMovName = tempName(tempName.Count - 1)

           Dim movName = tempMovName.Replace(".", " ")


Why you replace . with a apace? Why is are period in the directory name?

This post has been edited by AdamSpeight2008: 31 March 2013 - 04:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 968
  • Joined: 09-May 09

Re: Background worker issue

Posted 31 March 2013 - 04:32 PM

Thanks for the tip with path.combine

the reason I have the replace . with " " is, movies that are download often contain .

ie harry.potter

any idea on how I would be best to increase the performance of the webrequests?
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,466
  • Joined: 29-May 08

Re: Background worker issue

Posted 31 March 2013 - 04:44 PM

Previously downloaded ones are being overridden because that what your download section of code is telling it to do that.
Was This Post Helpful? 0
  • +
  • -

#5 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 968
  • Joined: 09-May 09

Re: Background worker issue

Posted 31 March 2013 - 04:49 PM

please elaborate

because I am passing the details to the thread and that will download the poster, I then update the counter the pass in the next movies details

please point out the error.

thanks
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,466
  • Joined: 29-May 08

Re: Background worker issue

Posted 31 March 2013 - 04:59 PM

Explain the steps involved here.
'BUILD URL AND DOWNLOAD POSTER
Dim poster_Url = "http://cf2.imgobject.com/t/p/" + sizes.images.poster_sizes(k) + MovieInfo.poster_path

webClient1.DownloadFile(poster_Url, posterLocation + "\" + movieName + ".jpg")

webClient1.Dispose()


Was This Post Helpful? 0
  • +
  • -

#7 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 968
  • Joined: 09-May 09

Re: Background worker issue

Posted 01 April 2013 - 03:25 PM

BUILDS URL, this is where is image is located
Dim poster_Url = "http://cf2.imgobject.com/t/p/" + sizes.images.poster_sizes(k) + MovieInfo.poster_path 



Download the image and save in the correct directory
webClient1.DownloadFile(poster_Url, posterLocation + "\" + movieName + ".jpg") 



'close the connection
webClient1.Dispose()


Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,466
  • Joined: 29-May 08

Re: Background worker issue

Posted 01 April 2013 - 03:36 PM

And also overwriting the previously downloaded file.
Was This Post Helpful? 0
  • +
  • -

#9 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 968
  • Joined: 09-May 09

Re: Background worker issue

Posted 01 April 2013 - 04:00 PM

I don't think your right


each time I call the request the url changes, downloading a different image

is there something I need to do, please provide help

kevin
Was This Post Helpful? 0
  • +
  • -

#10 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 968
  • Joined: 09-May 09

Re: Background worker issue

Posted 10 April 2013 - 09:11 AM

Anyone?
Was This Post Helpful? 0
  • +
  • -

#11 IronRazer  Icon User is online

  • D.I.C Addict

Reputation: 281
  • View blog
  • Posts: 890
  • Joined: 01-February 13

Re: Background worker issue

Posted 10 April 2013 - 09:35 AM

Hi,
Maybe i am missing something but, from what i can see you are not checking to see if the poster already exists in the (pPath) before calling for it to be downloaded. You can check to see if a file or directory exists using the (File.Exists) or Directory.Exists) methods.
File.Exists
Directory.Exists

This post has been edited by IronRazer: 10 April 2013 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#12 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 968
  • Joined: 09-May 09

Re: Background worker issue

Posted 11 April 2013 - 09:03 AM

Thanks for the reply

I typically do this but I knocked up this code to test threading, I think this could help but it shouldn't be doing this all all.

I passing the correct details. Is it possible that im not terminating the thread correctly and the thread is downloading the material over and over again?

This post has been edited by deery5000: 11 April 2013 - 09:14 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1