Link to home
Start Free TrialLog in
Avatar of supertedusa
supertedusaFlag for United States of America

asked on

BeginInvoke - name the thread

Would someone show me how to name the thread launched by a BeginInvoke call?

Thanks,

MT
Avatar of eternal_21
eternal_21

I don't think there is any way for you to control this.  There is no rule that says a "BeginInvoke" method even has to create a new thread, or that it doesn't create more than one.
Avatar of supertedusa

ASKER

I built part of my app from this starting point:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp

delegate void ShowProgressDelegate(string pi, int totalDigits, int digitsSoFar);

void CalcPi(int digits) {
  StringBuilder pi = new StringBuilder("3", digits + 2);

  // Get ready to show progress asynchronously
  ShowProgressDelegate showProgress =
    new ShowProgressDelegate(ShowProgress);

  // Show progress
  this.Invoke(showProgress, new object[] { pi.ToString(), digits, 0});

  if( digits > 0 ) {
    pi.Append(".");

    for( int i = 0; i < digits; i += 9 ) {
      ...
      // Show progress
      this.Invoke(showProgress,
        new object[] { pi.ToString(), digits, i + digitCount});
    }
  }
}


It was my understanding that using delegates/BeginInvoke forced the method to be run on a new thread?  The UI is certainly respsonsive during the operations.  Am I off base on this?
look at here..u got u r answer...

http://www.dotnet247.com/247reference/msgs/11/55668.aspx

best of luck..

R.K.
Thanks for the reference, but it looks like it's just another way of threading, I think?

So if I'm using:

del = new DelegateMy(m_Del.SomeMethod);
BeginInvoke(del, new object[]{parm1, parm2, parm3, [,etc...]});

from my main form, am I spawning a new thread this way?  The UI does not lock up on me and I'm able to make several calls to methods on the main form from the delegate method that update the main UI.

Just trying to get some clarity on the concepts and name the threads, if possible.

Thanks,

MT
If you followed the MSDN example, you should have the following code:

  Thread piThread = new Thread(new ThreadStart(CalcPiThreadStart));
  piThread.Start();

You can name your thread at this point:

  Thread piThread = new Thread(new ThreadStart(CalcPiThreadStart));
  piThread.Name = "NAME_OF_THREAD"; //TODO: Name this thread.
  piThread.Start();

But if you are trying to control this thread, keep a reference to piThread... Why are you concerned about the name of the thread?
ASKER CERTIFIED SOLUTION
Avatar of rama_krishna580
rama_krishna580
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
Actually, if you read on in the example, he switches to using delegates and BeginInvoke/Invoke instead of the Thread calls.

I'm concerned about the name because I want to make it easier to determine what's going on while I'm debugging this thing.

Thanks,

MT
Thanks for the reference - not sure how I missed that one on codeproject, but I've obviously been implementing BeginInvoke incorrectly and getting lucky in the process.

Thanks for everyone's comments.

MT