creating threads

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BobLewiston
    New Member
    • Feb 2009
    • 93

    creating threads

    Can someone please break down a simple statement for a relative newbie?

    Code:
    Thread firstThread = new Thread (new ThreadStart (Method1));
    In other words, what is happening at each stage here:

    Code:
    new ThreadStart (Method1)
    Code:
    new Thread (new ThreadStart (Method1))
    Code:
    Thread firstThread = new Thread (new ThreadStart (Method1));
    Thanks for any help you can give.
  • IanWright
    New Member
    • Jan 2008
    • 179

    #2
    I'd recommend MSDN for things such as this, but here's a summary for you.

    Code:
    new ThreadStart (Method1)
    A Thread needs somewhere to start, which obviously will be a Method. Normally this will be
    a parameterless method with no return. e.g.
    Code:
    private void DoStuff()
    Overall we call this starting point a ThreadStart, and we pass in a method to the constructor.

    Code:
    new Thread (new ThreadStart (Method1))
    This simply takes what we had before 1 step further. We are now constructing a new Thread, and passing
    a ThreadStart to the constructor (which in this case is being constructed on the same line of code). This
    tells the Thread whereabouts it should start.

    Code:
    Thread firstThread = new Thread (new ThreadStart (Method1));
    You're now taking it further by saving a reference to your Thread in a parameter called firstThread so you
    can then refer to it later.

    Comment

    Working...