Results 1 to 8 of 8

Thread: How Do I Multi-Thread?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    23

    How Do I Multi-Thread?

    I have a GUI that calls a module's Sub Main(), which contains a System.Threading.Thread.Sleep. While the thread is sleeping, the GUI is unresponsive. Is there a way to have the module sleep, but keep the GUI responsive? Any help would be greatly appreciated.

    UPDATE:
    I found some code on the internet for multi-threading, and I used it in my program. However, there is a problem. The module is supposed to appendText to a richTextBox in my GUI, but it's not. Any ideas why?

    This is the code I used

    Code:
    Dim oThread As System.Threading.Thread
            oThread = New Thread(AddressOf Console.Main)
            oThread.Start()
    Any help would be greatly appreciated.
    Last edited by 5te4lthX; Jan 27th, 2009 at 01:34 AM.

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

    Re: How Do I Multi-Thread?

    First up, why are you calling a Main method? Main methods should be used as the entry point for an application but you shouldn't really be calling a Main method directly.

    As for the question, you have two main options:

    1. Use a BackgroundWorker and do your work in its DoWork event handler. You'd then call ReportProgress method and update the UI in the ProgressChanged event handler.

    2. Create a new thread explicitly and do your work in its entry method. To update the UI you create a delegate and invoke it to marshal a method call to the UI thread.

    There are links in the Multi-threading section of my signature that demonstrate each of those options.
    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Re: How Do I Multi-Thread?

    Thanks JM, those tutorials are very clear and in-depth.

    I don't know the conventions of VB, so could someone tell me how I should change Sub Main()?

    I called Main() of my module because the module was originally a Console Application. I simply copy/pasted the module into my new GUI project, which would display the contents of Console.WriteLine() into a richtextbox.AppendText().

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

    Re: How Do I Multi-Thread?

    If you have a module in your WinForms app then, as always, you should give the method a name that indicates what it does. That said, if you want to display text in a RichTextBox then why call Console.WriteLine at all? It sounds like you've copied a method in it's entirety when what you should have done is reimplement the functionality it provides in a way that suits your current project.
    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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Re: How Do I Multi-Thread?

    I'm having trouble having a thread from a module access a control in a form.

    I can make the form create a local thread that can affect the control, but not if the thread calls a method from a module.

    This is getting very frustrating.

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

    Re: How Do I Multi-Thread?

    I don't really know exactly what you just said but I think your issue comes down to one thing: you cannot, in general, access control directly on any thread other than the UI thread. There's a link in my signature that deals with this issue. Basically you need to have a reference to a control that is owned by the same thread as the one you want to access so that you can invoke a delegate on that thread. Preferably it would actually be the control you want to access but that's not actually necessary. Basically, if you want to access a control in a module on a secondary thread then you must pass a reference to the control into the module too.
    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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Re: How Do I Multi-Thread?

    Thanks again, and yes, that's exactly what I asked.

    My problem is this: I call my method writeLine(Byval str As String), which contains txtConsole.InvokeRequired, from a thread not containing txtConsole, but the InvokeRequired's state is false.

    I created a new thread like this:
    Code:
    Dim oThread As System.Threading.Thread
            oThread = New Thread(AddressOf Console.Main)
            oThread.Start(txtConsole)
    I didn't use the following code because an error came up saying that I didn't have a BackgroundWorker1. It said exactly this: BackgroundWorker1 is not a valid member of GUI.
    Code:
    Me.BackgroundWorker1.RunWorkerAsync()
    When I execute the following code, the debugger displays: "WriteLine:", when it should say: "WriteLine: Worker Thread" (I think)
    Code:
    Debug.WriteLine(Threading.Thread.CurrentThread.Name, "WriteLine")
    Also, I had to make my WriteLine method public, because my module cannot access private methods located in the GUI.frm.

    Thanks for all the help.

    EDIT: I forgot to pass the module a reference to the control. I'll do this now and tell you if it works.
    EDIT: I have multiple classes that each need to writeLine. Does each class need a reference?
    EDIT: When I try to pass the module a reference, it says NullReferenceException. I pass the reference to the module in GUI_Load. Does this mean that txtConsole is uninitialized when I pass the reference?
    Last edited by 5te4lthX; Jan 29th, 2009 at 09:50 AM.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Re: How Do I Multi-Thread?

    I think I know my mistake. My method signiture for Console.Main() was Console.Main(ByVal txt As RichTextBox) when it should have been Console.Main(ByRef txt As RichTextBox).

    I'll update my code when I get home and I'll tell you how it goes.

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