We help IT Professionals succeed at work.
Get Started

Multi Threading and forms

axnst2 asked
Last Modified: 2010-04-16
Hi Experts,

     I have a form with a text box and a command button.  When the user presses the command button, I want to create two seperate instances of the same class and have them run on a thread of there own.  The threads will each come up with a random number between 1 and 10.  When the threads are done I want the form to take the two randomly generated numbers, add them up, and display the added value in the text box.

Something like this:

namespace MyNameSpace
           Public Partial Class MyForm : Form
                      MyClass class1 = new MyClass;
                      MyClass class2 = new MyClass;

                      Public void AddThem()


namespace MyNameSpace
           public class MyClass
                      int RandNum;
                      public int Result
                                              return RandNum;

                      public void StartThread()
                                 Thread t = new Thread(new ThreadStart(ThreadJob));
                                 t.IsBackground = true;

                       private void ThreadJob()

What I am basically having problems with is how to call a function that only exists in the form's code from the class threads.  I know that in my example I could have a zero value for Class1.Result or Class2.Result depending on which thread finished first, but I am not worried about that for now.  I need the classes to be on their own threads so that the GUI is still functioning while my classes are coming up with the random numbers.  I know that in this example I wouldn't need to use mutliple threads, but this is just an example.  In reality ThreadJob takes about 30 seconds to execute.

Any help would be appreciated,

Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE