Performance problem with threads

  • Thread starter Mats-Lennart Hansson
  • Start date
M

Mats-Lennart Hansson

Hi,
I'm trying to use threads to have a form showing the progress of a major
validation function. It works fine, but the performance is really poor. What
I do is:

When pressing the validate button, I create a form that is showing progress
info. After that, I'm starting a new thread that does the validation. This
thread communicates with the form by using events and delegates.

When I have the validation in a separate thread, it takes ~30 minutes to do
the validation. However, if I don't use the thread but calls the validation
directly, it takes only 3 minutes! In this case, my progress form will not
work.

Is this a common problem, does anyone know how to increase performance? I'm
using the code below to start a new thread.

System.Threading.Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(MajorValidationFunction));
thread.Start();

Thanks!

Mats-Lennart
 
P

Patrice

What if you use the thread but without reporting its progress ? For now I
would think there is a problem in the way you are reporting the progress.
Also do you share data between the form and your thread (badly hit by access
to shared resources) ?

Try to measure where this additional time budget is spent...
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Mats-Lennart,

The code that you posted didn't show enought, but when you handle the event
fired by the worker thread you should marshal the code the the main UI
thread that created the progress form. Only the thread created a control
(form) is allowed to manipulate the latter. If you don't do this a lot of
nad things could happen - e.g. exception, slow performance or even deadlock.

Take a look at Control.InvokeRequired and Control.Invoke.
 

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