Results 1 to 7 of 7

Thread: Passing in a string to a thread I am about to start? [C# 2002]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Question Passing in a string to a thread I am about to start? [C# 2002]

    I am trying to accomplish something like this:

    Code:
    {
       string sType ="Update";
       Thread th1 = new Thread (new ThreadStart(Operation1(sType)));
       th1.Start();
    }
    
    private void Operaton1(string sType)
    {
       if (sType == "Update")
       {
       }
       else
       {
       }
    }
    Obviouslly this doesn't work and returns and error message as follows:
    Method name expected

    It works fine if my "Operation1" has no parameters but I somehow need to pass the information from "sType" into the Thread itself...
    How could I accomplish passing a STRING into a THREAD I am about to start?

    Any help would be greatly appreciated.
    Thanks,

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Passing in a string to a thread I am about to start? [C# 2002]

    Hi there,
    I'm certainly no expert in threading, but I read up a little bit and using your version of the framework you can consider storing your variable in an instance of a class. So perhaps something like this:

    c# Code:
    1. // My form.
    2.     public partial class Form1 : Form
    3.     {
    4.         public Form1()
    5.         {
    6.             InitializeComponent();
    7.         }
    8.  
    9.         // New instance of my other class.
    10.         private void button1_Click(object sender, EventArgs e)
    11.         {
    12.             string str = "Update";
    13.             MyOperations op = new MyOperations(str);
    14.             new Thread(new ThreadStart(op.Operation1)).Start();
    15.         }
    16.     }
    17.  
    18.     // My other class.
    19.     public class MyOperations
    20.     {
    21.         string _str;
    22.  
    23.         public MyOperations (string str)
    24.         {
    25.             this._str = str;
    26.         }
    27.  
    28.         public void Operation1()
    29.         {
    30.             if (_str == "Update")
    31.             {
    32.                 MessageBox.Show("Update.");
    33.             }
    34.             else
    35.             {
    36.                 MessageBox.Show("No update.");
    37.             }
    38.         }
    39.     }

    I did find many sites that spoke about this topic in my search with much more information. Give Google a go and I'm sure you'll find plenty. I hope this little gets you started.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Re: Passing in a string to a thread I am about to start? [C# 2002]

    Thats a great idea - and while trying to implement it I ran into a single roadblock - Operation1 uses functions part of the class that is spawning the thread - if I pass in a class.function as opposed to a local function I loose scope of these functions and can no longer launch them (from what I can see). For example ... using your code ...

    Code:
    // My form.
    public partial class Form1 : Form
    {
    	public Form1()
    	{
    		InitializeComponent();
    	}
    	
    	// New instance of my other class.
    	private void button1_Click(object sender, EventArgs e)
    	{
    		string str = "Update";
    		MyOperations op = new MyOperations(str);
    		new Thread(new ThreadStart(op.Operation1)).Start();
    	}
    
    	private delegate void DoTheWorkDelegate
    	private void DoTheWork(string sStuff)
    	{
    		// DoTheWork stuff
    		// This can be called by a thread using “DoTheWorkDelegate”
    	}
    }
    
    // My other class.
    public class MyOperations
    {
    	string _str;
    	public MyOperations (string str)
    	{
    		this._str = str;
    	}
    	
    	public void Operation1()
    	{
    		if (_str == "Update")
    		{
    			DoTheWork(“update”);
    		}
    		else
    		{
    			DoTheWork(“not update”);
    		}
    	}
    }
    So doing it that way I get errors because "DoTheWork" is not defined in the new class "MyOperations" - any clue how to solve this aside from passing the "this" into the "MyOperations" class also and doing something like this.DoTheWork? (as I am not sure how threadsafe that kind of request would be).

    Thanks,

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Passing in a string to a thread I am about to start? [C# 2002]

    Heres a simple example of passing a string to a thread:

    C# Code:
    1. private void Form1_Load(object sender, EventArgs e)
    2.         {
    3.             System.Threading.Thread thr1 = new System.Threading.Thread(DoWork);
    4.             thr1.Start("Hello");
    5.  
    6.         }
    7.  
    8.         private void DoWork(object obj)
    9.         {
    10.             string Message = Convert.ToString(obj);
    11.             MessageBox.Show(Message);
    12.         }
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Passing in a string to a thread I am about to start? [C# 2002]

    Quote Originally Posted by Atheist
    Heres a simple example of passing a string to a thread:

    C# Code:
    1. private void Form1_Load(object sender, EventArgs e)
    2.         {
    3.             System.Threading.Thread thr1 = new System.Threading.Thread(DoWork);
    4.             thr1.Start("Hello");
    5.  
    6.         }
    7.  
    8.         private void DoWork(object obj)
    9.         {
    10.             string Message = Convert.ToString(obj);
    11.             MessageBox.Show(Message);
    12.         }
    Wow, that was quite easy. I can't believe that in all of sites that I looked on regarding passing a parameter to a new thread, that I didn't see that. All of them mentioned something with a class.

    Is this also available in 1.1? I'm not sure how the documentation on MSDN is exactly for 1.1, but they don't show an Overload for this method.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Passing in a string to a thread I am about to start? [C# 2002]

    If you want to be able to execute a method of an object you have no reference to then you need to pass a delegate. The very reason that delegates exist is to enable executing methods without references to their owners. Create a DoTheWorkDelegate instance and pass it to your MyOperations object, from which it can be invoked.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Re: Passing in a string to a thread I am about to start? [C# 2002]

    jmcilhinney: How do you pass in a delegate to a thread?

    As it stands the solution I found was to pass in the "this" pointer to the thread so that I can access all the required functions - and in the spawning class I created delegates for each function based on the Invoke requirements. I did a quick test and it looks like, when in my thread, if I do something like fMain.DoSomething the thread will go to the calling class and be Invoked and then run the function.

    Is this also a correct and threadsafe way to do it?
    Last edited by Shaitan00; Jul 23rd, 2007 at 12:16 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width