Home All Groups Group Topic Archive Search About

Thread.Sleep slowing down the whole application

Author
12 Nov 2006 9:05 PM
davidst95
Hello,

I have a program that runs a long process.    I tried to add a thread
to check if the use hit a cancel button to abort the process.

        Dim t As Threading.Thread
        t = New Threading.Thread(AddressOf StartProcess)
        t.IsBackground = True
        t.Priority = ThreadPriority.Highest
        t.Start()

        Do

           Thread.CurrentThread.Sleep(500)
            Application.DoEvents()
           if bCancel=true then
               t.Abort()
               exit do
           end if

        Loop


When I add 'StartProcess' into a thread, it's about twice as slow if it
just ran it without a thread.   Could anyone tell me what I'm not doing
right?   Thanks.   I played around with this all day and I'm lost.
Thanks.

David

Author
12 Nov 2006 9:55 PM
Spam Catcher
davids***@gmail.com wrote in news:1163365534.236771.249840
@m7g2000cwm.googlegroups.com:

Show quoteHide quote
> Hello,
>
> I have a program that runs a long process.    I tried to add a thread
> to check if the use hit a cancel button to abort the process.
>
>         Dim t As Threading.Thread
>         t = New Threading.Thread(AddressOf StartProcess)
>         t.IsBackground = True
>         t.Priority = ThreadPriority.Highest
>         t.Start()
>
> When I add 'StartProcess' into a thread, it's about twice as slow if it
> just ran it without a thread.   Could anyone tell me what I'm not doing
> right?   Thanks.   I played around with this all day and I'm lost.
> Thanks.


I noticed that you set the thread priority to highest - I think when it's
set to highest, it will not yield CPU time to other processes.

You should leave the thread priority to normal and try again.

Also, take a look at callback functions - depending on the situation,
they're  abit better than firing a thread to run a long running function.
Author
12 Nov 2006 11:19 PM
Dennis K.
On Sun, 12 Nov 2006 21:55:01 GMT, Spam Catcher <spamhoneypot@rogers.com>
wrote:

>Also, take a look at callback functions - depending on the situation,
>they're  abit better than firing a thread to run a long running function.

Could you give a small example?

--

Dennis K.
Author
13 Nov 2006 1:36 AM
Spam Catcher
Dennis K. <nobody@iglou.invalid> wrote in
news:1vafl2lk575kg304dk5urumfj0395ju5i0@4ax.com:

> On Sun, 12 Nov 2006 21:55:01 GMT, Spam Catcher <spamhoneypot@rogers.com>
> wrote:
>
>>Also, take a look at callback functions - depending on the situation,
>>they're  abit better than firing a thread to run a long running function.
>
> Could you give a small example?
>

Here you go:

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpguide/html/cpovrasynchronousprogrammingoverview.asp

You'll probably want the example: "Executing a Callback Method When an
Asynchronous Call Completes" or "Polling for Asynchronous Call Completion".
Author
13 Nov 2006 2:40 AM
davidst95
Thank you very much for the information.   I been playing around with
it all day.   I have to wait until tomorrow to try this out but I think
this might work.

David


Spam Catcher wrote:
Show quoteHide quote
> Dennis K. <nobody@iglou.invalid> wrote in
> news:1vafl2lk575kg304dk5urumfj0395ju5i0@4ax.com:
>
> > On Sun, 12 Nov 2006 21:55:01 GMT, Spam Catcher <spamhoneypot@rogers.com>
> > wrote:
> >
> >>Also, take a look at callback functions - depending on the situation,
> >>they're  abit better than firing a thread to run a long running function.
> >
> > Could you give a small example?
> >
>
> Here you go:
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-
> us/cpguide/html/cpovrasynchronousprogrammingoverview.asp
>
> You'll probably want the example: "Executing a Callback Method When an
> Asynchronous Call Completes" or "Polling for Asynchronous Call Completion".
Author
13 Nov 2006 12:06 PM
Robinson
>        Dim t As Threading.Thread
>        t = New Threading.Thread(AddressOf StartProcess)
>        t.IsBackground = True
>        t.Priority = ThreadPriority.Highest
>        t.Start()

Good, you've created a new thread :)

>
>        Do
>
>           Thread.CurrentThread.Sleep(500)
>            Application.DoEvents()
>           if bCancel=true then
>               t.Abort()
>               exit do
>           end if
>
>        Loop
>

Ahh, you are sleeping the CurrentThread, which is your main thread, not the
thread you created.  So, instead of sleeping there, you should sleep in
StartProcess.  Secondly, it's rather bad to call "t.Abort".  You will get no
end of problems especially if your thread is handling COM objects.  The
accepted pattern for dealing with threads is to flag a "bCancel" inside the
thread that it checks periodically so it can terminate cleanly.


Show quoteHide quote
>
> When I add 'StartProcess' into a thread, it's about twice as slow if it
> just ran it without a thread.   Could anyone tell me what I'm not doing
> right?   Thanks.   I played around with this all day and I'm lost.
> Thanks.
>
> David
>
Author
13 Nov 2006 2:41 PM
davidst95
Thanks for the reply.   Wouldn't "t.Sleep(1000)'  just slow down the
whole thread?   I tried it out and have similar results.    What I want
to try to do is run a thread and have another thread refresh the form
and check the cancel button without slowing down the 'StartProcess'
thread.

When I remove the thread itself and just run 'StartProcess', the time
decreases in half.

Thanks again.

David


Robinson wrote:
Show quoteHide quote
> >        Dim t As Threading.Thread
> >        t = New Threading.Thread(AddressOf StartProcess)
> >        t.IsBackground = True
> >        t.Priority = ThreadPriority.Highest
> >        t.Start()
>
> Good, you've created a new thread :)
>
> >
> >        Do
> >
> >           Thread.CurrentThread.Sleep(500)
> >            Application.DoEvents()
> >           if bCancel=true then
> >               t.Abort()
> >               exit do
> >           end if
> >
> >        Loop
> >
>
> Ahh, you are sleeping the CurrentThread, which is your main thread, not the
> thread you created.  So, instead of sleeping there, you should sleep in
> StartProcess.  Secondly, it's rather bad to call "t.Abort".  You will get no
> end of problems especially if your thread is handling COM objects.  The
> accepted pattern for dealing with threads is to flag a "bCancel" inside the
> thread that it checks periodically so it can terminate cleanly.
>
>
> >
> > When I add 'StartProcess' into a thread, it's about twice as slow if it
> > just ran it without a thread.   Could anyone tell me what I'm not doing
> > right?   Thanks.   I played around with this all day and I'm lost.
> > Thanks.
> >
> > David
> >
Author
13 Nov 2006 3:06 PM
Robinson
<davids***@gmail.com> wrote in message
Show quoteHide quote
news:1163428883.308461.208140@i42g2000cwa.googlegroups.com...
> Thanks for the reply.   Wouldn't "t.Sleep(1000)'  just slow down the
> whole thread?   I tried it out and have similar results.    What I want
> to try to do is run a thread and have another thread refresh the form
> and check the cancel button without slowing down the 'StartProcess'
> thread.
>
> When I remove the thread itself and just run 'StartProcess', the time
> decreases in half.
>
> Thanks again.
>
> David
>
>
> Robinson wrote:
>> >        Dim t As Threading.Thread
>> >        t = New Threading.Thread(AddressOf StartProcess)
>> >        t.IsBackground = True
>> >        t.Priority = ThreadPriority.Highest
>> >        t.Start()
>>
>> Good, you've created a new thread :)
>>
>> >
>> >        Do
>> >
>> >           Thread.CurrentThread.Sleep(500)
>> >            Application.DoEvents()
>> >           if bCancel=true then
>> >               t.Abort()
>> >               exit do
>> >           end if
>> >
>> >        Loop
>> >
>>
>> Ahh, you are sleeping the CurrentThread, which is your main thread, not
>> the
>> thread you created.  So, instead of sleeping there, you should sleep in
>> StartProcess.  Secondly, it's rather bad to call "t.Abort".  You will get
>> no
>> end of problems especially if your thread is handling COM objects.  The
>> accepted pattern for dealing with threads is to flag a "bCancel" inside
>> the
>> thread that it checks periodically so it can terminate cleanly.
>>
>>
>> >
>> > When I add 'StartProcess' into a thread, it's about twice as slow if it
>> > just ran it without a thread.   Could anyone tell me what I'm not doing
>> > right?   Thanks.   I played around with this all day and I'm lost.
>> > Thanks.
>> >
>> > David
>> >
>

Oh okay, I misunderstood.  I thought you were wanting to sleep the worker
thread so it didn't take too much time away from the main GUI thread (which
is the more normal scenario).  I suggest you re-archietect what you are
doing slightly.  Let your worker thread run free inside a loop checking for
"cancelled = true", at which point it exits.  You then can "fire and forget"
the worker thread.

When your main thread is finished, it can "invoke" a delegate on your form
to say it's finished, or a delegate to say it was cancelled or a delegate to
say it was failed (or you can put all 3 into the same method).

Something like this:

User Action
    Create Thread
    Start Thread (
        [Thread loops doing it's business]
            [If bCancelled Then Exit Thread]
        Exit:
            Invoke Completed Method on Main Form )

Completed:
    Update UI