Passing parameters to a thread by reference

davecove
davecove used Ask the Experts™
on
Imagine a List<MyStruct> that has x number of records in it. For each record in the List, a thread is created that goes and does something and then needs to update it's assigned record in the list.

The code below works in as much as it creates the threads and the threads can do their work because they can see what is in the record they are supposed to tend.

But they can't update the record in the List. They can, of course, modify their copy of the record, but that doesn't change the values of the record in the List. What I would like to do is pass in the List record by reference so that the thread can access it's record directly.

How do I pass an object to a thread by reference?

Thank you for your time,
Dave
Watcher my_watcher = new Watcher();
My_Struct a_rec = new My_Struct();
a_rec.s_a = "a";
a_rec.s_b = "b";
my_watcher.SU_List.Add(a_rec);

Thread fetcher_thread = new Thread(Fetcher);
//I want to be able to pass my_wather.SU_List[0] by ref
fetcher_thread.Start(my_watcher.SU_List[0]);  

public class Watcher
{
   public struct My_Struct
   {
      public string s_a;
      public string s_b;
   }

   public List<My_Struct> SU_List;

   public void Fetcher(object _SU)
   {
      My_Struct SU = (My_Struct)_SU;

      //now I can use SU, but not alter the 'original'
      //in my_watcher.SU_List
   }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You are passing a struct... when you pass a struct, it goes by value.  Turn your struct into a class, and it will be passed by reference.
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
You can create a parameter object to start your thread:
public class ParHelper
{
public List<My_Struct> SU_List;
public int TargetIndex;
}

//Fetcher method will look like:
   public void Fetcher(object _SU)
   {
      ParHelper help = (ParHelper )_SU;
      My_Struct SU = help.SU_List[help.TargetIndex];
 
      //now you can use  'original' SU
      //in my_watcher.SU_List
   }
Or change your method signature, so the struct is passed by reference...like this:
   public void Fetcher(ref object _SU)
   
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

I have changed your code in order to work according to your requirement. The three changes that I have made are
1. I declared the list variable SU_List as static (global to the class) so that you can use it freely in both the main thread and the worker thread. This is how the main thread and the worker thread share the information (threading basics).
2. I have made the My_Struct to be a class and not the struct.
3. I am not passing any argument of list to the thread function.

Here is the code.....
public class Watcher
    {
        public class My_Struct
        {
            public string s_a;
            public string s_b;
        }

        public static List<My_Struct> SU_List=new List<My_Struct>();

        static public void Fetcher()
        {
            //My_Struct SU = (My_Struct)_SU;   
           //no need to pass and collect the argument of list.

            SU_List[0].s_a = "value is changed";
            
            Console.WriteLine("From the thread = " +SU_List[0].s_a);
        }

        static void Main(string[] args)
        {
            Watcher my_watcher = new Watcher();
            Watcher.My_Struct a_rec = new Watcher.My_Struct();
            a_rec.s_a = "a";
            a_rec.s_b = "b";
            SU_List.Add(a_rec);

            Thread fetcher_thread = new Thread(Fetcher);
            //I want to be able to pass my_wather.SU_List[0] by ref
            fetcher_thread.Start();
            
            Thread.Sleep(4000);
            Console.WriteLine("From the Main = " +SU_List[0].s_a);
        }
    }

Open in new window

Author

Commented:
Most excellent answer Habeeballah! I was able to translate that to my exact implementation easily.

Dave
You are always most welcome Dave.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial