PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Q: Advice on threads

Reply

Q: Advice on threads

 
Thread Tools Rate Thread
Old 17-10-2006, 10:00 PM   #1
G .Net
Guest
 
Posts: n/a
Default Q: Advice on threads


Hi Everybody

I was wondering if you experts could cast your eyes over the following code
and tell me if I'm doing anything obviously wrong. The code works but causes
some unexpected things in an application I'm writing e.g. a form I'm setting
to be hidden using .Hide is not hidden:
Private m_threadProgress As New Thread(AddressOf ProgressForm)

' the next few lines occur in a sub

If Me.m_threadProgress.IsAlive = False Then

Me.m_threadProgress = New Thread(AddressOf ProgressForm)

End If

Me.m_threadProgress.Start()

' do some other stuff

Me.m_threadProgress.Abort()

' display another form

Can you give me any advice as to whether I'm doing this correctly?

Thanks in advance

G




  Reply With Quote
Old 18-10-2006, 06:33 AM   #2
Mattias Sjögren
Guest
 
Posts: n/a
Default Re: Q: Advice on threads

>Can you give me any advice as to whether I'm doing this correctly?

Calling Thread.Abort is generally a bad idea. Other than that it's
hard to say anything without seeing more of your code. Are you
creating or interacting with any UI components from your background
thread?


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
  Reply With Quote
Old 18-10-2006, 10:07 AM   #3
G .Net
Guest
 
Posts: n/a
Default Re: Q: Advice on threads

Hi Mattis

The thread displays a form with some flashing text. I'm using it to show
that long calculations are proceeding.

You say that Thread.Abort is a bad idea. How should I stop the thread and
dispose of it when it is no longer in use?

Thanks for your help.

"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:%23cGXQbn8GHA.3552@TK2MSFTNGP03.phx.gbl...
> >Can you give me any advice as to whether I'm doing this correctly?

>
> Calling Thread.Abort is generally a bad idea. Other than that it's
> hard to say anything without seeing more of your code. Are you
> creating or interacting with any UI components from your background
> thread?
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.



  Reply With Quote
Old 18-10-2006, 02:07 PM   #4
Michael D. Ober
Guest
 
Posts: n/a
Default Re: Q: Advice on threads

In the form, create a boolean property to tell the form you are ready for it
to close. Then, where you use the thread.Abort(), simply set this property
to true. Inside the form's code, periodically check this flag and exit.

Mike Ober.

"G .Net" <nodamnspam@email.com> wrote in message
news:VOudnQ4VdIh4c6jYRVnygg@pipex.net...
> Hi Mattis
>
> The thread displays a form with some flashing text. I'm using it to show
> that long calculations are proceeding.
>
> You say that Thread.Abort is a bad idea. How should I stop the thread and
> dispose of it when it is no longer in use?
>
> Thanks for your help.
>
> "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
> news:%23cGXQbn8GHA.3552@TK2MSFTNGP03.phx.gbl...
>> >Can you give me any advice as to whether I'm doing this correctly?

>>
>> Calling Thread.Abort is generally a bad idea. Other than that it's
>> hard to say anything without seeing more of your code. Are you
>> creating or interacting with any UI components from your background
>> thread?
>>
>>
>> Mattias
>>
>> --
>> Mattias Sjögren [C# MVP] mattias @ mvps.org
>> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
>> Please reply only to the newsgroup.

>
>



  Reply With Quote
Old 18-10-2006, 02:21 PM   #5
Miro
Guest
 
Posts: n/a
Default Re: Q: Advice on threads

Do you by chance have a form that is opening as a showmodal form in a
different thread?

If so, could you lemmi know how u did that. Ive been trying to do something
simillar.

Miro

"G .Net" <nodamnspam@email.com> wrote in message
news:VOudnQ4VdIh4c6jYRVnygg@pipex.net...
> Hi Mattis
>
> The thread displays a form with some flashing text. I'm using it to show
> that long calculations are proceeding.
>
> You say that Thread.Abort is a bad idea. How should I stop the thread and
> dispose of it when it is no longer in use?
>
> Thanks for your help.
>
> "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
> news:%23cGXQbn8GHA.3552@TK2MSFTNGP03.phx.gbl...
>> >Can you give me any advice as to whether I'm doing this correctly?

>>
>> Calling Thread.Abort is generally a bad idea. Other than that it's
>> hard to say anything without seeing more of your code. Are you
>> creating or interacting with any UI components from your background
>> thread?
>>
>>
>> Mattias
>>
>> --
>> Mattias Sjögren [C# MVP] mattias @ mvps.org
>> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
>> Please reply only to the newsgroup.

>
>



  Reply With Quote
Old 18-10-2006, 05:40 PM   #6
G .Net
Guest
 
Posts: n/a
Default Re: Q: Advice on threads

Hi Michael

How can I set the property of the form from the thread?

G

"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:OViaVZr8GHA.788@TK2MSFTNGP05.phx.gbl...
> In the form, create a boolean property to tell the form you are ready for
> it to close. Then, where you use the thread.Abort(), simply set this
> property to true. Inside the form's code, periodically check this flag
> and exit.
>
> Mike Ober.
>
> "G .Net" <nodamnspam@email.com> wrote in message
> news:VOudnQ4VdIh4c6jYRVnygg@pipex.net...
>> Hi Mattis
>>
>> The thread displays a form with some flashing text. I'm using it to show
>> that long calculations are proceeding.
>>
>> You say that Thread.Abort is a bad idea. How should I stop the thread and
>> dispose of it when it is no longer in use?
>>
>> Thanks for your help.
>>
>> "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
>> news:%23cGXQbn8GHA.3552@TK2MSFTNGP03.phx.gbl...
>>> >Can you give me any advice as to whether I'm doing this correctly?
>>>
>>> Calling Thread.Abort is generally a bad idea. Other than that it's
>>> hard to say anything without seeing more of your code. Are you
>>> creating or interacting with any UI components from your background
>>> thread?
>>>
>>>
>>> Mattias
>>>
>>> --
>>> Mattias Sjögren [C# MVP] mattias @ mvps.org
>>> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
>>> Please reply only to the newsgroup.

>>
>>

>
>



  Reply With Quote
Old 18-10-2006, 05:43 PM   #7
G .Net
Guest
 
Posts: n/a
Default Re: Q: Advice on threads

Hi Miro

In the code I used above, simply have

Private Sub ProgressForm()
Dim form As MyForm = New MyForm()
form.ShowDialog()
End Sub

However, the problem is how do we close the form succesfully? From what I
gather, using Abort is not a good idea; although it does appear that the
form is closed in calling it.

G

"Miro" <mironagy@golden.net> wrote in message
news:u5k47fr8GHA.4808@TK2MSFTNGP03.phx.gbl...
> Do you by chance have a form that is opening as a showmodal form in a
> different thread?
>
> If so, could you lemmi know how u did that. Ive been trying to do
> something simillar.
>
> Miro
>
> "G .Net" <nodamnspam@email.com> wrote in message
> news:VOudnQ4VdIh4c6jYRVnygg@pipex.net...
>> Hi Mattis
>>
>> The thread displays a form with some flashing text. I'm using it to show
>> that long calculations are proceeding.
>>
>> You say that Thread.Abort is a bad idea. How should I stop the thread and
>> dispose of it when it is no longer in use?
>>
>> Thanks for your help.
>>
>> "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
>> news:%23cGXQbn8GHA.3552@TK2MSFTNGP03.phx.gbl...
>>> >Can you give me any advice as to whether I'm doing this correctly?
>>>
>>> Calling Thread.Abort is generally a bad idea. Other than that it's
>>> hard to say anything without seeing more of your code. Are you
>>> creating or interacting with any UI components from your background
>>> thread?
>>>
>>>
>>> Mattias
>>>
>>> --
>>> Mattias Sjögren [C# MVP] mattias @ mvps.org
>>> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
>>> Please reply only to the newsgroup.

>>
>>

>
>



  Reply With Quote
Old 19-10-2006, 12:28 AM   #8
=?Utf-8?B?RGVubmlz?=
Guest
 
Posts: n/a
Default Re: Q: Advice on threads

Generally, I would have the thread do the work and have the flashing dialog
in my main UI thread. You can have the thread trigger an event that you
catch in the UI thread when the worker thread is done calculating then close
the dialog by calling a method from the finished event that the worker thread
fired.
--
Dennis in Houston


"G .Net" wrote:

> Hi Miro
>
> In the code I used above, simply have
>
> Private Sub ProgressForm()
> Dim form As MyForm = New MyForm()
> form.ShowDialog()
> End Sub
>
> However, the problem is how do we close the form succesfully? From what I
> gather, using Abort is not a good idea; although it does appear that the
> form is closed in calling it.
>
> G
>
> "Miro" <mironagy@golden.net> wrote in message
> news:u5k47fr8GHA.4808@TK2MSFTNGP03.phx.gbl...
> > Do you by chance have a form that is opening as a showmodal form in a
> > different thread?
> >
> > If so, could you lemmi know how u did that. Ive been trying to do
> > something simillar.
> >
> > Miro
> >
> > "G .Net" <nodamnspam@email.com> wrote in message
> > news:VOudnQ4VdIh4c6jYRVnygg@pipex.net...
> >> Hi Mattis
> >>
> >> The thread displays a form with some flashing text. I'm using it to show
> >> that long calculations are proceeding.
> >>
> >> You say that Thread.Abort is a bad idea. How should I stop the thread and
> >> dispose of it when it is no longer in use?
> >>
> >> Thanks for your help.
> >>
> >> "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
> >> news:%23cGXQbn8GHA.3552@TK2MSFTNGP03.phx.gbl...
> >>> >Can you give me any advice as to whether I'm doing this correctly?
> >>>
> >>> Calling Thread.Abort is generally a bad idea. Other than that it's
> >>> hard to say anything without seeing more of your code. Are you
> >>> creating or interacting with any UI components from your background
> >>> thread?
> >>>
> >>>
> >>> Mattias
> >>>
> >>> --
> >>> Mattias Sjögren [C# MVP] mattias @ mvps.org
> >>> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> >>> Please reply only to the newsgroup.
> >>
> >>

> >
> >

>
>
>

  Reply With Quote
Old 19-10-2006, 03:04 PM   #9
Miro
Guest
 
Posts: n/a
Default Re: Q: Advice on threads

Yeah Im trying something similar but once all my calculations are done, I
put a command button "close" that shows up that the user selects to
continue.

-User wanted to know it was complete instead of it dissapearing
automatically.

I will try to close it automatically and see what I find too. Ill be
hitting my code this weekend agian.

Miro


"G .Net" <nodamnspam@email.com> wrote in message
news:EfWdnTLnOKJRxKvYnZ2dnUVZ8qudnZ2d@pipex.net...
> Hi Miro
>
> In the code I used above, simply have
>
> Private Sub ProgressForm()
> Dim form As MyForm = New MyForm()
> form.ShowDialog()
> End Sub
>
> However, the problem is how do we close the form succesfully? From what I
> gather, using Abort is not a good idea; although it does appear that the
> form is closed in calling it.
>
> G
>
> "Miro" <mironagy@golden.net> wrote in message
> news:u5k47fr8GHA.4808@TK2MSFTNGP03.phx.gbl...
>> Do you by chance have a form that is opening as a showmodal form in a
>> different thread?
>>
>> If so, could you lemmi know how u did that. Ive been trying to do
>> something simillar.
>>
>> Miro
>>
>> "G .Net" <nodamnspam@email.com> wrote in message
>> news:VOudnQ4VdIh4c6jYRVnygg@pipex.net...
>>> Hi Mattis
>>>
>>> The thread displays a form with some flashing text. I'm using it to show
>>> that long calculations are proceeding.
>>>
>>> You say that Thread.Abort is a bad idea. How should I stop the thread
>>> and dispose of it when it is no longer in use?
>>>
>>> Thanks for your help.
>>>
>>> "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
>>> news:%23cGXQbn8GHA.3552@TK2MSFTNGP03.phx.gbl...
>>>> >Can you give me any advice as to whether I'm doing this correctly?
>>>>
>>>> Calling Thread.Abort is generally a bad idea. Other than that it's
>>>> hard to say anything without seeing more of your code. Are you
>>>> creating or interacting with any UI components from your background
>>>> thread?
>>>>
>>>>
>>>> Mattias
>>>>
>>>> --
>>>> Mattias Sjögren [C# MVP] mattias @ mvps.org
>>>> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
>>>> Please reply only to the newsgroup.
>>>
>>>

>>
>>

>
>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off