Link to home
Start Free TrialLog in
Avatar of DigitalDan3
DigitalDan3

asked on

Implementing Multithreading to increase performance.

I am using ASP.Membership, Roles and Profiles in my WinForms and Web application.  I have serialized the profile information which is stored in SQL.  Because of the way I have serialized the object creating the profile objects take some time.  So if I open my app as an administrator and open the form that displays all the users in a grid that contains the Membership.Username, Profile.FirstName, Profile.LastName and Profile.PrimaryRole.  It takes approximately 40 seconds to display the 279 users. As an admin I can see all users  which takes along time to process.  

Basically I start with a list of UserNames that I iterate through.  I have created a SimpleUserInfo class and a SimpleUserInfoCollection that is used to populate the grid.

For each UserName as String in UserNames
  SimpleUserBindingSource.Add( GetSimpleUserInfo(UserName))
Next.

I could create a thread for each call to GetSimpleUserInfo but that would create at the moment 279 threads.  I have read that creating too many threads would slow down performance.

My UserNames is in Alphabetical order so using different threads does not guarantee that each thread will return in the order they were started.

This is what I would like to accomplish.

Use approximatelly 4 threads to retreive the data passing one username.
Add the information to the grid in username order as each thread is finished.  
Be able to select an item on the grid and keep it selected as the grid is updated.
Be able to show the progress in a progress bar.  And also capture when all the threads are complete.

I am attaching a simple Winforms app that simulates the long process.  I am using the DirectoryInfo and FileInfo of MyDocuments to populate the grid.  I have put a random delay to simulate that each call takes a differnt amount of time.  Currently the app is using an individual thread for each call.  


Form1.txt
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Avatar of DigitalDan3
DigitalDan3

ASKER

Zach,

Thanks for the article and code. I am a vb developer and have been traslating your code to vb.  I am stuck translating the MethodContainer's ExecuteMethod to VB.  

        public void ExecuteMethod()
        {
            try
            {
                if (_callback != null)
                    this.Callback(_state);
                else
                    this.Simple();
            }
            catch (Exception exception)
            {
                this.Exception = exception;
            }
        }

 
ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

A good VB<=>C# code translator should you get stuck!

http://www.carlosag.net/Tools/CodeTranslator/

and im not sure if this applies to Webforms, but have you tried using the BackgroundWorker component?