VB.NET Threading CPU Issue

G

Guest

Hi, I am writing software to automate some testing. I have one main form to
set up the tests, and once this is complete, I open 4 identical forms to
monitor each different device that I am automating. Because there are many
fast timers on each, I have placed each one of these forms in their own
thread by using the following code:

Dim NewForm As New frmFormName
Dim NewThread As System.Threading.Thread

NewThread = New System.Threading.Thread(AddressOf NewRadio.Show)
NewThread.Start()

NewThread = Nothing
NewRadio = Nothing

Anyway, everything works fine, but when I try and minimize or maximize a
window in a different program, my CPU Usage goes to 100% and stays there
until I stop my automation application. Does anyone know how I can fix this?

Thanks,

Luc
 
G

Guest

Hi,

A thread runs in the background. It will keep the users computer from
feeling like it is locked up but your cpu might go to 100%.

Ken
 
M

Mehdi

Hi, I am writing software to automate some testing. I have one main form to
set up the tests, and once this is complete, I open 4 identical forms to
monitor each different device that I am automating. Because there are many
fast timers on each, I have placed each one of these forms in their own
thread by using the following code: [...]
Anyway, everything works fine, but when I try and minimize or maximize a
window in a different program, my CPU Usage goes to 100% and stays there
until I stop my automation application. Does anyone know how I can fix this?

It might be that your application really needs 100% of the CPU so there is
no reason why Windows would not give it to your application as long as no
other application needs it.

However, it might also be a problem with your application. Playing with UI
controls in different thread is a very tricky subject and should be avoided
unless you really know what you are doing. You must make sure that controls
in a given thread never interact in any way with controls in another thread
(for example, you cannot have a form in thread 1 parent of a control or
form in thread 2). Also, you must make sure that all the calls to control's
methods are properly marshalled to their respective threads.

Also keep in mind that if your system only has one CPU, then it will only
be able to do one thing at a time no matter how many threads you have
started. Even if you use 10000 threads, it won't run faster than if you
only has 1 thread. In fact, it would actually be slower since Windows would
need to constantly do thread context switches, which takes time.

From what you've said, it doesn't look like you really need to have 1 form
per thread. You could have your 4 forms in the UI thread and then have your
business logic running in 4 different threads. Or don't even start threads
at all and use a System.Timers.Timer which will call its callback method in
a thread from the thread pool from where you'll be able to do whatever you
want to do.
 
C

Cor Ligthert [MVP]

Luc,

100% processing time means mostly a continues loop inside one of your
threads.

Cor
 
R

Robert

Luc said:
Hi, I am writing software to automate some testing. I have one main form
to
set up the tests, and once this is complete, I open 4 identical forms to
monitor each different device that I am automating. Because there are
many
fast timers on each, I have placed each one of these forms in their own
thread by using the following code:

Dim NewForm As New frmFormName
Dim NewThread As System.Threading.Thread

NewThread = New System.Threading.Thread(AddressOf NewRadio.Show)
NewThread.Start()

NewThread = Nothing
NewRadio = Nothing

Anyway, everything works fine, but when I try and minimize or maximize a
window in a different program, my CPU Usage goes to 100% and stays there
until I stop my automation application. Does anyone know how I can fix
this?


I just did some work on an app like this.

Run one main thread with all the GUI stuff..
Run your worker threads at belownormal priority.

Otherwise all your threads will run by default at normal priority, and the
screen redraws may never have time to finish before they are preempted by
your other threads. This leads to an unresponsive app.
 
G

Guest

To monitor things with a UI try to use BackgroundWorker. Its' new in .NET
Framework 2.0 and allow multithreading without freezing your UI (100% CPU).

Let me know if it's help.

Truly,
ZENOU Nicolas.
 
G

Guest

Thank you everyone for the replies, you've been of so much help! I managed
to fix the 100% problem by using the sleep method for 1 millisecond every
time my timer method runs. I now have a new problem :-S.

I am using two ccrp timers on each of the four forms. One to update a clock
to show the time, and one in a dll to run all the automation routines.
Because I want the clocks on each to update at the same time, I have the
timers running really fast, and because of the required accuracy of the
automation processes, the second timer is running really fast. If I start
the entire thing and don't touch anything else, the program works as it
should. However, if I minimize or maximize a different window other than
that four forms after the program has been started, the second ccrp timer
(located in the dll) on each form either fails to run when it should, or just
run once and then never again.

Would anyone have an idea how to fix this?

Thanks,

Luc
 
R

Robert

Luc said:
Thank you everyone for the replies, you've been of so much help! I
managed
to fix the 100% problem by using the sleep method for 1 millisecond every
time my timer method runs. I now have a new problem :-S.

I am using two ccrp timers on each of the four forms. One to update a
clock
to show the time, and one in a dll to run all the automation routines.
Because I want the clocks on each to update at the same time, I have the
timers running really fast, and because of the required accuracy of the
automation processes, the second timer is running really fast. If I start
the entire thing and don't touch anything else, the program works as it
should. However, if I minimize or maximize a different window other than
that four forms after the program has been started, the second ccrp timer
(located in the dll) on each form either fails to run when it should, or
just
run once and then never again.

Would anyone have an idea how to fix this?

Timers and Sleep/Doevents sounds way to VB6'ish where we were forced to
simulate threads by sharing one thread among several activities.. Also,
stay far away from SendKeys.

..Net has much more effective and efficient ways to synchronize threads than
timers.


Your architecture seems WAY too complicated to me.
1) All .Net apps by default have one main thread. The GUI will run on it.
Designate this thread as the "Boss".
2) Have the boss thread have one timer to fire every 100ms.
3) When the timer fires, delegate chunks of work to each of the worker
threads(running at BelowNormal priority). Boss is now idle. Yielding to
lower priority threads.
4) Have the worker threads fire an event back to the main GUI including the
results of the work
5) The boss thread will then display the results on the next context switch.
Maybe call Application.DoEvents here, to flush the message queue.
6) if you resize the main window, the boss will again have some work to do,
and being at a higher priority, will step forward, and handle the resize.
When all events are processed, boss is again idle, allowing the workers
another go.

Not saying the above is the absolute best way, but it has clean readable
code, behaves well, and gives a responsive app.
I found it solved all the issues in my app.


I found this useful to queue up a bunch of work items, so they did not get
out of order, or in each others way.
It also has some good info on threading in general.

http://www.codeproject.com/csharp/s...orumid=88599&exp=0&select=1391682#xx1391682xx

Hope that helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top