473,414 Members | 1,720 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,414 software developers and data experts.

Thread.Sleep slowing down the whole application

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

Nov 12 '06 #1
7 2559
da*******@gmail.com wrote in news:1163365534.236771.249840
@m7g2000cwm.googlegroups.com:
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.

Nov 12 '06 #2
On Sun, 12 Nov 2006 21:55:01 GMT, Spam Catcher <sp**********@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.
Nov 12 '06 #3
Dennis K. <no****@iglou.invalidwrote in
news:1v********************************@4ax.com:
On Sun, 12 Nov 2006 21:55:01 GMT, Spam Catcher <sp**********@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/de...l=/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".
Nov 13 '06 #4
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:
Dennis K. <no****@iglou.invalidwrote in
news:1v********************************@4ax.com:
On Sun, 12 Nov 2006 21:55:01 GMT, Spam Catcher <sp**********@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/de...l=/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".
Nov 13 '06 #5

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

Nov 13 '06 #6
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
Nov 13 '06 #7

<da*******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.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

Nov 13 '06 #8

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

Similar topics

6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
22
by: Brett | last post by:
I have a second thread, t2, that errors out and will stop. It's status is then "Stopped". I try to start t2 from thread 1, t1, by checking If t2.threadstate = "Stopped" Then t2.start() ...
2
by: Daniel Knöpfel | last post by:
Hi I am develloping an asp.net 2.0 application. For some tasks (daily notifications to users via email), we use background threads. I rather have this task as background thread of the asp.net...
5
by: John A. Bailo | last post by:
From a Windows service (NET 2.0) I want to launch serveral threads in a for loop that invokes a method using: new Thread(delegate() { myMethod(248);}).Start(); Will those threads stay...
11
by: Jon Slaughter | last post by:
Is there any way to start a terminated thread without using a pool or creating a new thread object? void counter() { clicks = 0; clock.Start(); while (counterActive) { clicks++;
10
by: Paul E Collins | last post by:
I want to fill an ImageList with bitmaps for a ListView from another thread, because it's a time-consuming process. I expect the ListViewItems' images to "load" one by one, as in a Web browser. ...
1
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
Sorry this is so long winded, but here goes. Following the model of http://msdn2.microsoft.com/en-us/library/system.runtime.remoting.channels.ipc.ipcchannel.aspx I made a remote object using the...
3
by: stumorgan | last post by:
Basically what I have is a form with a graph on it which graphs data that I'm reading from a USB device at 100 Hz (every 10ms). I have a thread reading and parsing the data from the USB, but when...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.