Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello for some reason when im using a thread my cpu usage is very high and was wondering how i can reduce it. Here's my code to start it
Dim myThread As New Thread(AddressOf TimerStart)
           myThread.IsBackground = True
           myThread.Start()

and also how would i stop the thread without using thread.abort as ive heard it's evil, but i need it on a button click. So how would this all work. thanks in advance.

What I have tried:

ive looked around found a few things on stack overflow and not really finding anything that works for me.
Posted
Updated 19-Oct-17 23:52pm

A thread is a cpu user by definition: it runs on a core until it finds nothing to do and is either terminated or suspended for some reason. (It can be suspended by the system if a higher priority task needs to run as well).

So if your thread is using significant cpu resources, you need to look closely at what code it is executing: that is what is using the CPU, and we can't see that! Start with your TimerStart code and see what it is doing.
 
Share this answer
 
Comments
OfficalCodexPH 20-Oct-17 5:03am    
ok dude ill have a look now thanks man.
OriginalGriff 20-Oct-17 5:14am    
You're welcome!
OfficalCodexPH 20-Oct-17 5:22am    
dude you know you said if the thread was terminated ect well what if i wanted to0 get the user to push a stop button how would they stop the thread immediately just by clicking the stop button? Sorry about all question my multi threading inst the best :-)
OriginalGriff 20-Oct-17 5:29am    
Basically, they can't.
They can - a thread can be aborted, but that's like pulling the emergency cord on a train: it stops dead and everybody gets thrown around. When you abort a thread everything it's doing gets killed - and that means files aren't closed properly, data is scrapped, and so on. And if it's taken a lock then that lock doesn't get released, so everything else that wants it hangs forever. It's very much the last resort.

Instead, you use a variable that tells the thread to stop - it checks it often, and when it finds a stop request, it shuts down gracefully. How quickly that happens depends on how often your code checks the variable!
OfficalCodexPH 20-Oct-17 6:39am    
oh ok dude thanks for the help i understand what you mean now. :-)
Quote:
and also how would i stop the thread without using thread.abort as ive heard it's evil, but i need it on a button click
See Shutting Down Worker Threads Gracefully[^], it is C#, but you could easily adapt it to VB.NET (BTW C# lock is SyncLock in VB.NET).
 
Share this answer
 
Comments
OfficalCodexPH 20-Oct-17 6:41am    
ok dude cheers, and while your here i was wondering if you knew how i could put the time remaining of a progress bar into a label somehow sorry for inconvenience just wondering if you knew. Thanks for the help :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900