473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How do I know when a thread has finished?

Hello I wish to execute some code when and only when a thread has completed
like so

thrRestore = New Thread(AddressOf RestoreDb)
thrRestore.IsBackground = True
thrRestore.Start()

??? I only want the next bit to run when thrRestore is complete how do i
know
while thrRestore.isalive

end while 'this doesn't work, it just loops around forever.

If BlnDatabaseChanged Then
Call ProjectNameSpace.mdiClient.LoadActiveItems(False)
End If

Thanx in advance
Robert
Nov 21 '05 #1
7 1252
"Robert Smith" <Ro*********@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
Hello I wish to execute some code when and only when a thread has completed like so

thrRestore = New Thread(AddressOf RestoreDb)
thrRestore.IsBackground = True
thrRestore.Start()

??? I only want the next bit to run when thrRestore is complete how do i know
while thrRestore.isalive

end while 'this doesn't work, it just loops around forever.

If BlnDatabaseChanged Then
Call ProjectNameSpace.mdiClient.LoadActiveItems(False)
End If

Thanx in advance
Robert


Do you want thrRestore.join() ?
Why would you want to start a new thread & then not use the current thread
to actually process anything ?

--
Jonathan Bailey.
Nov 21 '05 #2
Robert,

Raise an event at the end of the thread and catch that in your main thread.
Have a look or try this sample (I changed it a little bit in this text so
there can be errors).

\\\needs on form 1 one button and a textbox
Private WithEvents frm1 As Form2
Private Delegate Sub Frm1Handler(ByVal message As String)
Private MyThread As System.Threading.Thread
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
AddHandler timer1.Tick, AddressOf mytimer1
TextBox1.Text = "0"
timer1.Enabled = True
timer1.Interval = 400
Dim timer2 As New System.Windows.Forms.Timer
End Sub
Private Sub mytimer1(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox1.Text = (CInt(TextBox1.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
frm1 = New Form2
frm1.itstop = Me.Top
frm1.itsleft = Me.Left + 200
AddHandler frm1.ready, AddressOf Frm1Ready
frm1.Text = "Extra thread"
MyThread = New System.Threading.Thread(AddressOf frm1.Show)
MyThread.Start()
End Sub
Private Sub Frm1Ready(ByVal message As String)
Me.BeginInvoke(New Frm1Handler(AddressOf Frm1HandlerSub), New
Object() {message})
End Sub
Private Sub Frm1HandlerSub(ByVal message As String)
TextBox2.Text = message
frm1.Close()
MyThread.Abort()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
MyThread.Abort()
End Sub
///
\\\Needs a form2 with one textbox
Friend Event ready(ByVal message As String)
Friend itstop As Integer
Friend itsleft As Integer
Private Sub Form2_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
Me.Left = itsleft
Me.Top = itstop
Me.BringToFront()
Dim timenext As DateTime = Now.Add(TimeSpan.FromSeconds(10))
Do While timenext > Now
TextBox1.Text = Now.TimeOfDay.ToString
Application.DoEvents() 'to show the time
Threading.Thread.Sleep(50)
Me.Opacity -= 0.004
Loop
RaiseEvent ready(Now.TimeOfDay.ToString)
End Sub
Private Sub Form2_Closing(ByVal sender As Object, ByVal _
e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
e.Cancel = True
End Sub
///
I hope this helps a little bit?

Cor

"Robert Smith" <Ro*********@discussions.microsoft.com>
Hello I wish to execute some code when and only when a thread has
completed
like so

thrRestore = New Thread(AddressOf RestoreDb)
thrRestore.IsBackground = True
thrRestore.Start()

??? I only want the next bit to run when thrRestore is complete how do
i
know
while thrRestore.isalive

end while 'this doesn't work, it just loops around forever.

If BlnDatabaseChanged Then
Call ProjectNameSpace.mdiClient.LoadActiveItems(False)
End If

Thanx in advance
Robert

Nov 21 '05 #3
Hi Jb,
I don't want to process the process the next bit in the same thread
because I get errors saying cannot alter controls that have been created in
another thread, other places where this code is called outside of a thread do
not get this error.

"jb" wrote:
"Robert Smith" <Ro*********@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
Hello I wish to execute some code when and only when a thread has

completed
like so

thrRestore = New Thread(AddressOf RestoreDb)
thrRestore.IsBackground = True
thrRestore.Start()

??? I only want the next bit to run when thrRestore is complete how do

i
know
while thrRestore.isalive

end while 'this doesn't work, it just loops around forever.

If BlnDatabaseChanged Then
Call ProjectNameSpace.mdiClient.LoadActiveItems(False)
End If

Thanx in advance
Robert


Do you want thrRestore.join() ?
Why would you want to start a new thread & then not use the current thread
to actually process anything ?

--
Jonathan Bailey.

Nov 21 '05 #4
JD
From MSDN

"Controls in Windows Forms are bound to a specific thread and are not thread
safe. Therefore, if you are calling a control's method from a different
thread, you must use one of the control's invoke methods to marshal the call
to the proper thread. "
"Robert Smith" <Ro*********@discussions.microsoft.com> wrote in message
news:67**********************************@microsof t.com...
Hi Jb,
I don't want to process the process the next bit in the same thread because I get errors saying cannot alter controls that have been created in another thread, other places where this code is called outside of a thread do not get this error.

"jb" wrote:
"Robert Smith" <Ro*********@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
Hello I wish to execute some code when and only when a thread has

completed
like so

thrRestore = New Thread(AddressOf RestoreDb)
thrRestore.IsBackground = True
thrRestore.Start()

??? I only want the next bit to run when thrRestore is complete how do
i
know
while thrRestore.isalive

end while 'this doesn't work, it just loops around
forever.
If BlnDatabaseChanged Then
Call ProjectNameSpace.mdiClient.LoadActiveItems(False)
End If

Thanx in advance
Robert


Do you want thrRestore.join() ?
Why would you want to start a new thread & then not use the current

thread to actually process anything ?

--
Jonathan Bailey.

Nov 21 '05 #5
Cor,
Noce example, thanks for putting it on this ng.

I changed some small things and added comments. Works great!
Whats the difference between using BeginInvoke and just calling the method,
frm2HandlerSub, directly????

============FORM1 BELOW
'Project needs two forms
'Form 1 needs two textboxes and a button.
'Form2 needs one text box
'Raises an event at the end of the thread and catchs that in the main
thread.
'The Delegate class is not a delegate type; it is used to derive delegate
types, like frm2Handler.
Private Delegate Sub frm2Handler(ByVal message As String)
Private WithEvents frm2 As Form2
Private MyThread As System.Threading.Thread
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
AddHandler timer1.Tick, AddressOf mytimer1
TextBox1.Text = "0"
TextBox2.Text = ""
timer1.Enabled = True
timer1.Interval = 400
Dim timer2 As New System.Windows.Forms.Timer
End Sub
Private Sub mytimer1(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox1.Text = (CInt(TextBox1.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "0"
TextBox2.Text = ""
frm2 = New Form2
frm2.itstop = Me.Top
frm2.itsleft = Me.Left + 200
AddHandler frm2.ready, AddressOf frm2Ready
frm2.Text = "Extra thread"
MyThread = New System.Threading.Thread(AddressOf frm2.Show)
MyThread.Start()
End Sub
Private Sub frm2Ready(ByVal message As String)
'The frm2 Ready event returns the time of day in message
'The delegate ,frm2Handler,is called asynchronously, and this method returns
immediately.
'You can call this method from any thread, even the thread that owns the
control's handle.
'frm2Handler is a type so New frm2Handler(AddressOf frm2HandlerSub) runs the
type's constructor
'and return an instance of the type
'Second BeginInvoke argument is an array of objects to pass as arguments to
the given method.
'This array has one member, "message"
Me.BeginInvoke(New frm2Handler(AddressOf frm2HandlerSub), New Object()
{message})
End Sub
Private Sub frm2HandlerSub(ByVal message As String)
TextBox2.Text = message
frm2.Close()
MyThread.Abort()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
MyThread.Abort()
End Sub
End Class
============FORM2 BELOW
Friend Event ready(ByVal message As String)
Friend itstop As Integer
Friend itsleft As Integer
Private Sub Form2_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
Me.Left = itsleft
Me.Top = itstop
Me.BringToFront()
'Count down 10 seconds
Dim timenext As DateTime = Now.Add(TimeSpan.FromSeconds(10))
Do While timenext > Now
TextBox1.Text = Now.TimeOfDay.ToString 'Show time every 50 milliseconds
Application.DoEvents() 'to show the time
Threading.Thread.Sleep(50) 'pause for 50 milliseconds
Me.Opacity -= 0.004
Loop
RaiseEvent ready(Now.TimeOfDay.ToString) 'Raise "ready" event
End Sub
Private Sub Form2_Closing(ByVal sender As Object, ByVal _
e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
e.Cancel = True
End Sub
End Class


"Cor Ligthert" <no************@planet.nl> wrote in message
news:uZ**************@TK2MSFTNGP10.phx.gbl...
Robert,

Raise an event at the end of the thread and catch that in your main
thread. Have a look or try this sample (I changed it a little bit in this
text so there can be errors).

Nov 21 '05 #6
JustMe,

Read this walkthrough for it. (At the end marshaling etc)

http://msdn.microsoft.com/library/de...dcomponent.asp

I hope this clears it

Cor

Nov 21 '05 #7
"Robert Smith" <Ro*********@discussions.microsoft.com> wrote:
Hello I wish to execute some code when and only when a
thread has completed like so


Take a look at the 'IAsyncResult' interface in the documentation.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Prashanth Ellina | last post by:
Hi, I have used the low-level thread module to write a multi-threaded app. tid = thread.start_new_thread(process, ()) tid is an integer thread ident. the thread takes around 10 seconds to...
16
by: droopytoon | last post by:
Hi, I start a new thread (previous one was "thread timing") because I have isolated my problem. It has nothing to do with calling unmanaged C++ code (I removed it in a test application). I...
0
by: Kueishiong Tu | last post by:
I have a window form application. It requires to retrieve data from a web site. Since the web request is very time consuming, so I create a work thread to do the job. How does the window main form...
2
by: palaga | last post by:
hi I'm using QueueUserWorkItem to execute a bunch of tasks using the thread pool. Once started, I would like to wait for all of them to finish, using something like WaitAll. Is there a way I can...
12
by: semedao | last post by:
Hi all, someone know if I can use the IAsyncResult.IsCompleted Property of IAsyncResult that return from Socket.Beginxxx methods to determine if the Endxxx method already called perior or not ? I...
10
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005 and .net 2.0. I started 2 threadpool threads. How do I know when they're done with their tasks? Thanks. ThreadPool.QueueUserWorkItem(new...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
1
by: Chrace | last post by:
Hi all, I have a problem with with Thread.Join( Timeout ) where the timeout never occurs. I basically need to make a connection to an AS400 box which works fine. Once in a blue moon the AS400...
2
by: Steve | last post by:
Hi All, I've been trying to come up with a good way to run a certain process at a timed interval (say every 5 mins) using the SLEEP command and a semaphore flag. The basic thread loop was always...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.