473,396 Members | 2,004 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,396 software developers and data experts.

Does this thread code look OK

Trying threads for the first time.
This seems to hang and I wonder if it looks OK to you.
Right now the thread t is simply a Form with a Cancel Button.
If clicked it should raise an event that sets mCancelled to true in the main
thread.

---The main thread contains
Private Sub UserCancelled(ByVal sender As Object, ByVal e As EventArgs)
mCancelled = True
End Sub

Private Function SetupAndPreview() As Boolean
SNIP
Dim mCancelled = False
Dim ch As New FormCheckForCancel
ch.Show()
Dim t As New Thread(AddressOf ch.AskIfCancel)
t.Start()
AddHandler ch.Cancelled, AddressOf UserCancelled
SNIP

--The t thread code is a Form
Public Class FormCheckForCancel
Inherits System.Windows.Forms.Form
SNIP

Public Event Cancelled(ByVal sender As Object, ByVal e As EventArgs)

Public Sub AskIfCancel()
Do
Application.DoEvents()
System.Threading.Thread.Sleep(500)
Loop

End Sub

Private Sub ButtonCancel_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ButtonCancel.Click
RaiseEvent Cancelled(Me, Nothing)
Me.Close()
End Sub
End Class
Nov 21 '05 #1
13 1072
The first thing that jumps out at me is the variable m_Cancelled. You Dim
it in one function, but try to change the value of it in another sub. You
may want to declare it outside your subroutines and functions so it can be
accessible to everything.
" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...
Trying threads for the first time.
This seems to hang and I wonder if it looks OK to you.
Right now the thread t is simply a Form with a Cancel Button.
If clicked it should raise an event that sets mCancelled to true in the
main thread.

---The main thread contains
Private Sub UserCancelled(ByVal sender As Object, ByVal e As EventArgs)
mCancelled = True
End Sub

Private Function SetupAndPreview() As Boolean
SNIP
Dim mCancelled = False
Dim ch As New FormCheckForCancel
ch.Show()
Dim t As New Thread(AddressOf ch.AskIfCancel)
t.Start()
AddHandler ch.Cancelled, AddressOf UserCancelled
SNIP

--The t thread code is a Form
Public Class FormCheckForCancel
Inherits System.Windows.Forms.Form
SNIP

Public Event Cancelled(ByVal sender As Object, ByVal e As EventArgs)

Public Sub AskIfCancel()
Do
Application.DoEvents()
System.Threading.Thread.Sleep(500)
Loop

End Sub

Private Sub ButtonCancel_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ButtonCancel.Click
RaiseEvent Cancelled(Me, Nothing)
Me.Close()
End Sub
End Class

Nov 21 '05 #2
I'm sorry, I tried to make the code short and introduced a bug. I do have:
Friend mCancelled As Boolean

Is it true that using a Form for the thread is OK?

Thanks

"OpticTygre" <op********@adelphia.net> wrote in message
news:sZ********************@adelphia.com...
The first thing that jumps out at me is the variable m_Cancelled. You Dim
it in one function, but try to change the value of it in another sub. You
may want to declare it outside your subroutines and functions so it can be
accessible to everything.
" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...
Trying threads for the first time.
This seems to hang and I wonder if it looks OK to you.
Right now the thread t is simply a Form with a Cancel Button.
If clicked it should raise an event that sets mCancelled to true in the
main thread.

---The main thread contains
Private Sub UserCancelled(ByVal sender As Object, ByVal e As EventArgs)
mCancelled = True
End Sub

Private Function SetupAndPreview() As Boolean
SNIP
Dim mCancelled = False
Dim ch As New FormCheckForCancel
ch.Show()
Dim t As New Thread(AddressOf ch.AskIfCancel)
t.Start()
AddHandler ch.Cancelled, AddressOf UserCancelled
SNIP

--The t thread code is a Form
Public Class FormCheckForCancel
Inherits System.Windows.Forms.Form
SNIP

Public Event Cancelled(ByVal sender As Object, ByVal e As EventArgs)

Public Sub AskIfCancel()
Do
Application.DoEvents()
System.Threading.Thread.Sleep(500)
Loop

End Sub

Private Sub ButtonCancel_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ButtonCancel.Click
RaiseEvent Cancelled(Me, Nothing)
Me.Close()
End Sub
End Class


Nov 21 '05 #3

" Just Me" <gr****@a-znet.com> wrote in message
news:ea***************@TK2MSFTNGP15.phx.gbl...
I'm sorry, I tried to make the code short and introduced a bug. I do have:
Friend mCancelled As Boolean and no Dim on mCancelled = False
I removed it below
Is it true that using a Form for the thread is OK?

Thanks

"OpticTygre" <op********@adelphia.net> wrote in message
news:sZ********************@adelphia.com...
The first thing that jumps out at me is the variable m_Cancelled. You
Dim it in one function, but try to change the value of it in another sub.
You may want to declare it outside your subroutines and functions so it
can be accessible to everything.
" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...
Trying threads for the first time.
This seems to hang and I wonder if it looks OK to you.
Right now the thread t is simply a Form with a Cancel Button.
If clicked it should raise an event that sets mCancelled to true in the
main thread.

---The main thread contains
Private Sub UserCancelled(ByVal sender As Object, ByVal e As EventArgs)
mCancelled = True
End Sub

Private Function SetupAndPreview() As Boolean
SNIP
mCancelled = False
Dim ch As New FormCheckForCancel
ch.Show()
Dim t As New Thread(AddressOf ch.AskIfCancel)
t.Start()
AddHandler ch.Cancelled, AddressOf UserCancelled
SNIP

--The t thread code is a Form
Public Class FormCheckForCancel
Inherits System.Windows.Forms.Form
SNIP

Public Event Cancelled(ByVal sender As Object, ByVal e As EventArgs)

Public Sub AskIfCancel()
Do
Application.DoEvents()
System.Threading.Thread.Sleep(500)
Loop

End Sub

Private Sub ButtonCancel_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ButtonCancel.Click
RaiseEvent Cancelled(Me, Nothing)
Me.Close()
End Sub
End Class



Nov 21 '05 #4
That, I'm not sure of. I've never tried it, though I've tried threading
just about everything else, from a simple subroutine defined in my Form.vb
to a separate class, and even separate DLL's.

I think the problem might just be the layout of the code. Do you actually
stop the thread anywhere? Just because you close the second form, it
doesn't stop the Do...Loop that is currently running on that thread. It's
untested, but I think you'll need to declare the thread object outside the
function, then call a thread.abort followed by a thread.join. Don't forget
you can also check the status of your thread by calling Thread.ThreadState.

Hope this helps,
-Jason

" Just Me" <gr****@a-znet.com> wrote in message
news:ea***************@TK2MSFTNGP15.phx.gbl...
I'm sorry, I tried to make the code short and introduced a bug. I do have:
Friend mCancelled As Boolean

Is it true that using a Form for the thread is OK?

Thanks

"OpticTygre" <op********@adelphia.net> wrote in message
news:sZ********************@adelphia.com...
The first thing that jumps out at me is the variable m_Cancelled. You
Dim it in one function, but try to change the value of it in another sub.
You may want to declare it outside your subroutines and functions so it
can be accessible to everything.
" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...
Trying threads for the first time.
This seems to hang and I wonder if it looks OK to you.
Right now the thread t is simply a Form with a Cancel Button.
If clicked it should raise an event that sets mCancelled to true in the
main thread.

---The main thread contains
Private Sub UserCancelled(ByVal sender As Object, ByVal e As EventArgs)
mCancelled = True
End Sub

Private Function SetupAndPreview() As Boolean
SNIP
Dim mCancelled = False
Dim ch As New FormCheckForCancel
ch.Show()
Dim t As New Thread(AddressOf ch.AskIfCancel)
t.Start()
AddHandler ch.Cancelled, AddressOf UserCancelled
SNIP

--The t thread code is a Form
Public Class FormCheckForCancel
Inherits System.Windows.Forms.Form
SNIP

Public Event Cancelled(ByVal sender As Object, ByVal e As EventArgs)

Public Sub AskIfCancel()
Do
Application.DoEvents()
System.Threading.Thread.Sleep(500)
Loop

End Sub

Private Sub ButtonCancel_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ButtonCancel.Click
RaiseEvent Cancelled(Me, Nothing)
Me.Close()
End Sub
End Class



Nov 21 '05 #5
Hi There,

So let me get this right, the thread is running in...

Public Sub AskIfCancel()
Do
Application.DoEvents()
System.Threading.Thread.Sleep(500)
Loop
End Sub

I would suggest using a try catch and aborting the thread when needed,

Public Sub AskIfCancel()
Try
While True
'Your loop code in here
End While
Catch ex as ThreadAbortException
'//The thread was aborted.
End Try
End Sub

'//elsewhere
e.abort << This will cause the abort exception to be raised in
the thread, which usually gracefully exits it.

Nick.

" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...
Trying threads for the first time.
This seems to hang and I wonder if it looks OK to you.
Right now the thread t is simply a Form with a Cancel Button.
If clicked it should raise an event that sets mCancelled to true in the
main thread.

---The main thread contains
Private Sub UserCancelled(ByVal sender As Object, ByVal e As EventArgs)
mCancelled = True
End Sub

Private Function SetupAndPreview() As Boolean
SNIP
Dim mCancelled = False
Dim ch As New FormCheckForCancel
ch.Show()
Dim t As New Thread(AddressOf ch.AskIfCancel)
t.Start()
AddHandler ch.Cancelled, AddressOf UserCancelled
SNIP

--The t thread code is a Form
Public Class FormCheckForCancel
Inherits System.Windows.Forms.Form
SNIP

Public Event Cancelled(ByVal sender As Object, ByVal e As EventArgs)

Public Sub AskIfCancel()
Do
Application.DoEvents()
System.Threading.Thread.Sleep(500)
Loop

End Sub

Private Sub ButtonCancel_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ButtonCancel.Click
RaiseEvent Cancelled(Me, Nothing)
Me.Close()
End Sub
End Class

Nov 21 '05 #6
Just me,

What is the purpose from this extra workerthread. I get the idea that you
try to take over the normal handling from the OS which throws events when
something happened. That needs no extra thread.

The OS is working (semi) parallel on your program

Or do I see something wrong?

Cor
Nov 21 '05 #7
From what I can see, you have mCancelled as a local variable declared in the
function, but when you call UserCancelled it will error or not compile
because mCancelled is not declared in UserCancelled. So, declared that in
the declaration section instead.

Also, your ask if cancelled is an infinitive loop because there is no way
out of it. You should do this:

Private Sub AskIfCancelled()

Do

Thread.Sleep...

While mCancelled = True

End Sub

or even better change it to a boolean function

You are raising an event in your code, but what are you doing with it?
Unspecificed

Any chance of posting more code soI/we can see what is actually going on in
your code? If you use this forum with Outlook/Outlook Express then you can
zip your project & attach it to the post.

Awaiting your response
Nov 21 '05 #8
Thanks a lot
Actually I did get that from the book I was using but minimized the code I
presented, but thanks a lot for replying.
"Nick" <no****@altavente.com> wrote in message
news:u%****************@TK2MSFTNGP15.phx.gbl...
Hi There,

So let me get this right, the thread is running in...

Public Sub AskIfCancel()
Do
Application.DoEvents()
System.Threading.Thread.Sleep(500)
Loop
End Sub

I would suggest using a try catch and aborting the thread when needed,

Public Sub AskIfCancel()
Try
While True
'Your loop code in here
End While
Catch ex as ThreadAbortException
'//The thread was aborted.
End Try
End Sub

'//elsewhere
e.abort << This will cause the abort exception to be raised in
the thread, which usually gracefully exits it.

Nick.

" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl...
Trying threads for the first time.
This seems to hang and I wonder if it looks OK to you.
Right now the thread t is simply a Form with a Cancel Button.
If clicked it should raise an event that sets mCancelled to true in the
main thread.

---The main thread contains
Private Sub UserCancelled(ByVal sender As Object, ByVal e As EventArgs)
mCancelled = True
End Sub

Private Function SetupAndPreview() As Boolean
SNIP
Dim mCancelled = False
Dim ch As New FormCheckForCancel
ch.Show()
Dim t As New Thread(AddressOf ch.AskIfCancel)
t.Start()
AddHandler ch.Cancelled, AddressOf UserCancelled
SNIP

--The t thread code is a Form
Public Class FormCheckForCancel
Inherits System.Windows.Forms.Form
SNIP

Public Event Cancelled(ByVal sender As Object, ByVal e As EventArgs)

Public Sub AskIfCancel()
Do
Application.DoEvents()
System.Threading.Thread.Sleep(500)
Loop

End Sub

Private Sub ButtonCancel_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ButtonCancel.Click
RaiseEvent Cancelled(Me, Nothing)
Me.Close()
End Sub
End Class


Nov 21 '05 #9
Cor, Please see my latest post for the reason.
But I just learned a word : workerthread
That I could have used in that reply!

Thanks

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Just me,

What is the purpose from this extra workerthread. I get the idea that you
try to take over the normal handling from the OS which throws events when
something happened. That needs no extra thread.

The OS is working (semi) parallel on your program

Or do I see something wrong?

Cor

Nov 21 '05 #10
I did post more and I hope better code.
But to answer this: the way out of the loop is the main thread will abort
this (code not shown) but I think it makes sense to end it as soon as it's
need end (as you did).
Two questions,
1) is it common to use: While mCancelled = True
rather then While mCancelled
which I think does the same?

2)I do t.Abort()
In case the workerthread has been terminated as you suggest do I have to
first check something like
If t Is Nothing
Thanks

"Crouchie1998" <cr**********@discussions.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
From what I can see, you have mCancelled as a local variable declared in
the
function, but when you call UserCancelled it will error or not compile
because mCancelled is not declared in UserCancelled. So, declared that in
the declaration section instead.

Also, your ask if cancelled is an infinitive loop because there is no way
out of it. You should do this:

Private Sub AskIfCancelled()

Do

Thread.Sleep...

While mCancelled = True

End Sub

or even better change it to a boolean function

You are raising an event in your code, but what are you doing with it?
Unspecificed

Any chance of posting more code soI/we can see what is actually going on
in
your code? If you use this forum with Outlook/Outlook Express then you can
zip your project & attach it to the post.

Awaiting your response

Nov 21 '05 #11
Did some looking so I revised below
" Just Me" <gr****@a-znet.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I did post more and I hope better code.
But to answer this: the way out of the loop is the main thread will abort
this (code not shown) but I think it makes sense to end it as soon as it's
need end (as you did).
Two questions,
1) is it common to use: While mCancelled = True
rather then While mCancelled
which I think does the same?

2)I do t.Abort()
In case the workerthread has been terminated as you suggest do I have to
first check something like
If t.IsAlive then t.Abort

Or is simply
t.Abort enough?

Thanks

"Crouchie1998" <cr**********@discussions.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
From what I can see, you have mCancelled as a local variable declared in
the
function, but when you call UserCancelled it will error or not compile
because mCancelled is not declared in UserCancelled. So, declared that in
the declaration section instead.

Also, your ask if cancelled is an infinitive loop because there is no way
out of it. You should do this:

Private Sub AskIfCancelled()

Do

Thread.Sleep...

While mCancelled = True

End Sub

or even better change it to a boolean function

You are raising an event in your code, but what are you doing with it?
Unspecificed

Any chance of posting more code soI/we can see what is actually going on
in
your code? If you use this forum with Outlook/Outlook Express then you
can
zip your project & attach it to the post.

Awaiting your response


Nov 21 '05 #12
Just me,

I checked it better, in my idea is it in short the same as this sample what
I once made and tailored a little bit in december.
http://groups-beta.google.com/group/...0333a996591291

When you want can you have a look at it.

Cor
Nov 21 '05 #13
Very nice
thanks

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uO**************@TK2MSFTNGP09.phx.gbl...
Just me,

I checked it better, in my idea is it in short the same as this sample
what I once made and tailored a little bit in december.
http://groups-beta.google.com/group/...0333a996591291

When you want can you have a look at it.

Cor

Nov 21 '05 #14

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

Similar topics

0
by: Mathias Mamsch | last post by:
Hi all, I got a problem with writing a python extension module which launches a bunch of C threads (currently under windows). The module launches a complete speech recognition engine in a new...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
3
by: Philip V Pham | last post by:
These questions apply to std vector, map, and cout: I am uncertain of the thread safety for reading/writing for std templates. I know if all threads are reading concurrently, it is thread...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
7
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
5
by: Rashad Rivera | last post by:
I need to know why the Application_Start function fires twice when it initializes. It is doing double work and messing up my process. Thanks for your help - Rashad
52
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it...
18
by: cj | last post by:
members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe. I'm under the impression before you can use a class you have to make an...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...

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.