|
-
Jan 27th, 2009, 01:28 AM
#1
Thread Starter
Junior Member
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.
-
Jan 27th, 2009, 02:11 AM
#2
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.
-
Jan 27th, 2009, 10:42 AM
#3
Thread Starter
Junior Member
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().
-
Jan 27th, 2009, 05:13 PM
#4
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.
-
Jan 29th, 2009, 01:58 AM
#5
Thread Starter
Junior Member
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.
-
Jan 29th, 2009, 02:04 AM
#6
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.
-
Jan 29th, 2009, 09:34 AM
#7
Thread Starter
Junior Member
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.
-
Jan 29th, 2009, 03:21 PM
#8
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|