Results 1 to 4 of 4

Thread: [02/03] Multithread Class

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    47

    [02/03] Multithread Class

    Hey,

    Is there anyway to get a whole class to run on a seperate thread? Right now I have declared a class in my General Declaration. Now in the class I have functions which start other private threads in that class.

    General Declaration:

    VB Code:
    1. Private objDatabase as new clsdatabase

    When I try (in general declaration):

    VB Code:
    1. Private objDatabase As New Thread(AddressOf clsdatabase)

    I get a type problem. I think it only works like that for methods. But how would I get it to work for a class?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,461

    Re: [02/03] Multithread Class

    You're not really getting the concept of threading. Each thread is a sequence of executed commands. An instance of a class is an object, while its methods are sequences of commands. Objects just sit there in memory that is accessible to all threads. They do not "run". You can have several methods of the same object all executing simultaneously in diffreent threads. What is often done is to use an instance of a class as the data source and entry point for a thread. That gives you an easy way to pass data to your thread's entry method, but the class instance is not "running" in either thread. It's just sitting there and a thread or threads are executing its methods.
    VB Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim tc As New ThreadCapsule
    5.  
    6.         tc.Data = "Hello World"
    7.  
    8.         Dim t As New Threading.Thread(AddressOf tc.EntryPoint)
    9.  
    10.         t.Start()
    11.     End Sub
    12. End Class
    13.  
    14. Public Class ThreadCapsule
    15.  
    16.     Public Data As String
    17.  
    18.     Public Sub EntryPoint()
    19.         'Use Data here.
    20.     End Sub
    21.  
    22. End Class

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    47

    Re: [02/03] Multithread Class

    I have written a class that handles all the database reading/writting. When my program starts I open one connection to a sql database. Whenever this program needs something from the database it calls the database class and does whatever it needs to do. Now, some of the methods/functions in this database class take a while to execute, thus causing the application to freeze until the db work is completed.

    To fix this problem I made the existing methods/functions in the class private and added new methods that initialized each method on a diffrent thread. This works fine and all but someone told me that there is an easier way to do this. Thats why I came and posted the question here because I couldnt find anything on the net.

    Now see I figured that I could simply tell Windows to execute everything in one object on another thread whenever needed. But I guess that isnt possible.

    But I also wasnt aware that you could launch a method in class B from class A in a seperate thread. I think that will help me clean the code a little.

    Thanks again for the help!

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,461

    Re: [02/03] Multithread Class

    It may be that that someone was talking about some of the improvements in .NET 2.0. The BackgroundWorker may have been useful in your situation. Also, the SqlCommand class supports asynchronous methods, like BeginExecuteReader, which do not block like their synchronous equivalents do, allowing execution to continue in the main thread while the data access occurs in the background. You may also like to follow the Articles -> Advanced .NET link in my signature. The sections on Asynchronous Programming and Managed Threading are worthwhile reading for anyone. Note that they are from the .NET 2.0 documentation though so you'd always need to check that the technique described could be allied to 1.x as well.

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