Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Threading and Classes

Threading and Classes

Scheduled Pinned Locked Moved Visual Basic
businessperformancehelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheComputerMan
    wrote on last edited by
    #1

    I have searched everywhere and cannot find a definitive answer on this. I want to use a class to be created in a different thread to collect an image (or images) from the web. This is to run as part of a service. The thread is created thus:

    Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
        Dim cBR As New BackgroundRetrieveProcess(m\_strURL, m\_strImageName)
        Dim t As System.Threading.Thread
        t = New System.Threading.Thread(AddressOf cBR.GetImageFile)
        t.Start()
    End Sub
    

    The outline of the class is thus:

    Public Class BackgroundRetrieveProcess
    Private m_strUrl As String = ""
    Private m_strPlotName As String = ""

    Public Sub New(ByVal URLAddress As String, ByVal PlotImageName As String)
        m\_strUrl = URLAddress
        m\_strPlotName = PlotImageName
    End Sub
    
    ''' <summary>
    ''' This is the bit that does the retrieval of the image from the web
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub GetImageFile()
        Try
    
            'do the business here
    
        Catch ex As Exception
    
            'bog out here if there is a problem
    
        End Try
    End Sub
    

    End Class

    When the class has does the business how does it terminate? I have created a class, but there seems to be no way of destroying it when it has collected the image. Is it going to stay around in memory? Obviously I don't want loads of these lying around in memory, but also I don't want to stop the main thread to wait for the class to finish (some of these can take a while to download). Basically each class needs to get the image and then cease existence. I can't find a way to end the class - but do I need to? It is also possible that one may not have finished before the next one is started up (it is on a timer). Am I going to run into a problem here? Thanks for any assistance.

    L 1 Reply Last reply
    0
    • T TheComputerMan

      I have searched everywhere and cannot find a definitive answer on this. I want to use a class to be created in a different thread to collect an image (or images) from the web. This is to run as part of a service. The thread is created thus:

      Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
          Dim cBR As New BackgroundRetrieveProcess(m\_strURL, m\_strImageName)
          Dim t As System.Threading.Thread
          t = New System.Threading.Thread(AddressOf cBR.GetImageFile)
          t.Start()
      End Sub
      

      The outline of the class is thus:

      Public Class BackgroundRetrieveProcess
      Private m_strUrl As String = ""
      Private m_strPlotName As String = ""

      Public Sub New(ByVal URLAddress As String, ByVal PlotImageName As String)
          m\_strUrl = URLAddress
          m\_strPlotName = PlotImageName
      End Sub
      
      ''' <summary>
      ''' This is the bit that does the retrieval of the image from the web
      ''' </summary>
      ''' <remarks></remarks>
      Public Sub GetImageFile()
          Try
      
              'do the business here
      
          Catch ex As Exception
      
              'bog out here if there is a problem
      
          End Try
      End Sub
      

      End Class

      When the class has does the business how does it terminate? I have created a class, but there seems to be no way of destroying it when it has collected the image. Is it going to stay around in memory? Obviously I don't want loads of these lying around in memory, but also I don't want to stop the main thread to wait for the class to finish (some of these can take a while to download). Basically each class needs to get the image and then cease existence. I can't find a way to end the class - but do I need to? It is also possible that one may not have finished before the next one is started up (it is on a timer). Am I going to run into a problem here? Thanks for any assistance.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, you shouldn't worry about having classes around; classes are just code (and some static data). It is when you create one or more instances of a class, that objects get created; these may or may not hold lots of data, up to the point of being relevant. However, as soon as the last reference to such object has disappeared (when the object is no longer considered "alive"), the garbage collector can (and will) destroy it the next time the GC runs. If you insist on worrying about it, you could: - pass the parameters (URL and imageName) to the GetFile() method, so it does not need any class members; that allows you to make it static/shared, and hence you no longer need to instantiate that class; - move the GetFile method to the BackgroundRetrieveProcess class; there is no real need to have it in a separate class! :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read formatted code with indentation, so please use PRE tags for code snippets.


      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


      T 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, you shouldn't worry about having classes around; classes are just code (and some static data). It is when you create one or more instances of a class, that objects get created; these may or may not hold lots of data, up to the point of being relevant. However, as soon as the last reference to such object has disappeared (when the object is no longer considered "alive"), the garbage collector can (and will) destroy it the next time the GC runs. If you insist on worrying about it, you could: - pass the parameters (URL and imageName) to the GetFile() method, so it does not need any class members; that allows you to make it static/shared, and hence you no longer need to instantiate that class; - move the GetFile method to the BackgroundRetrieveProcess class; there is no real need to have it in a separate class! :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        T Offline
        T Offline
        TheComputerMan
        wrote on last edited by
        #3

        Thanks Luc :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups