|
-
Apr 14th, 2005, 04:21 PM
#1
Thread Starter
Fanatic Member
[resolved] .Invoke, Threads, and OnPaint method
When I create a delegate sub and then point it at another sub that has the same signature, then use the .Invoke method, does it create a new thread for runing that sub?
Basically, my paint code is long and involved. It uses a for...next loop to loop through a collection of 'elements' and draw each of the visible ones individually.
I was wondering if there was an easy way to put that in it's own thread so the ui doesn't lock up (just slow down) when there's a huge number of 'elements'
I was also wondering if creting a delegat sub was the correct way of doing that.
This is all being performed in the OnPaint method of the class I'm working on and it needs to pass arguments to the thread.
Any ideas?
Last edited by agent; Apr 28th, 2005 at 04:24 PM.
Reason: resolved
-
Apr 27th, 2005, 03:19 PM
#2
Junior Member
Re: .Invoke, Threads, and OnPaint method
If the GUI locks up, it's better to do the calculations on a seperate thread.
.NET has easy mechanisms for this. You will have to take care of the synchronization between the GUI thread and the 'worker' thread. Indead,
passing arguments to the worker thread is not always an easy thing to do, and you'll need synchronization for this (most of the time I use the AutoResetEvent class for this)
A simple example for getting started:
VB Code:
Imports System
Imports System.Threading
' Simple threading scenario: Start a Shared method running
' on a second thread.
Public Class ThreadExample
' The ThreadProc method is called when the thread starts.
' It loops ten times, writing to the console and yielding
' the rest of its time slice each time, and then ends.
Public Shared Sub ThreadProc()
Dim i As Integer
For i = 0 To 9
Console.WriteLine("ThreadProc: {0}", i)
' Yield the rest of the time slice.
Thread.Sleep(0)
Next
End Sub
Public Shared Sub Main()
Console.WriteLine("Main thread: Start a second thread.")
' The constructor for the Thread class requires a ThreadStart
' delegate. The Visual Basic AddressOf operator creates this
' delegate for you.
Dim t As New Thread(AddressOf ThreadProc)
' Start ThreadProc. On a uniprocessor, the thread does not get
' any processor time until the main thread yields. Uncomment
' the Thread.Sleep that follows t.Start() to see the difference.
t.Start()
'Thread.Sleep(0)
Dim i As Integer
For i = 1 To 4
Console.WriteLine("Main thread: Do some work.")
Thread.Sleep(0)
Next
Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.")
t.Join()
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.")
Console.ReadLine()
End Sub
End Class
You might also use the ThreadPool class
VB Code:
Imports System
Imports System.Threading
Public Class Example
Public Shared Sub Main()
' Queue the task.
ThreadPool.QueueUserWorkItem( _
New WaitCallback(AddressOf ThreadProc) _
)
' Note that you do not have to create the WaitCallback delegate
' explicitly in Visual Basic. The following line also queues
' the task:
'ThreadPool.QueueUserWorkItem(AddressOf ThreadProc)
Console.WriteLine("Main thread does some work, then sleeps.")
' If you comment out the Sleep, the main thread exits before
' the thread pool task runs. The thread pool uses background
' threads, which do not keep the application running. (This
' is a simple example of a race condition.)
Thread.Sleep(1000)
Console.WriteLine("Main thread exits.")
End Sub
' This thread procedure performs the task.
Shared Sub ThreadProc(stateInfo As Object)
' No state object was passed to QueueUserWorkItem, so
' stateInfo is null.
Console.WriteLine("Hello from the thread pool.")
End Sub
End Class
Notes : here you can use the stateInfo argument to pass your own data.
-
Apr 28th, 2005, 01:34 AM
#3
Thread Starter
Fanatic Member
Re: .Invoke, Threads, and OnPaint method
So QueueUserWorkItem is the only way to pass parameters?
-
Apr 28th, 2005, 05:13 AM
#4
Junior Member
Re: .Invoke, Threads, and OnPaint method
I did not say that, it's just an easy solution.
Another option is using synchronization (Mutex, AutoResetEvent...) and shared variables to pass the arguments between the two threads.
-
Apr 28th, 2005, 04:23 PM
#5
Thread Starter
Fanatic Member
Re: .Invoke, Threads, and OnPaint method
Thanks for you info. It helped a lot.
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
|