none
Executing a command on a separate thread RRS feed

  • Question

  • I have a command that performs a few tasks and I'm wanting to execute it on a separate thread. What's the easiest and most barebones way to do this?

    For example, I'd like the following command executed in a separate thread...

            private void UpdateParameters()
            {
                for (int i = 0; i < MyElements.Count; i++)
                {
                    // Some code for each element
                }
            }

    Thanks!

    Wednesday, July 3, 2019 8:53 PM

Answers

  • Hi T Gregory,

    Thank you for posting here.

    For your question, you want to execute a command on a separate thread. I make a simple code, you could have a look.

      class Program
        {
            static void Main(string[] args)
            {
                Thread thread = new Thread(UpdateParameters);
                thread.Start();
                
            }
    
            private static void UpdateParameters()
            {
                List<string> list = new List<string>();
                list.Add("Test");
                list.Add("Student");
                list.Add("Teacher");
                for (int i = 0; i < list.Count; i++)
                {
                    list[i] = list[i] + i.ToString();
                }
                for (int i = 0; i < list.Count; i++)
                {
                    Console.WriteLine(list[i]);
                }
                Console.ReadKey();
            }
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 4, 2019 2:54 AM

All replies

  • Hi T Gregory,

    Thank you for posting here.

    For your question, you want to execute a command on a separate thread. I make a simple code, you could have a look.

      class Program
        {
            static void Main(string[] args)
            {
                Thread thread = new Thread(UpdateParameters);
                thread.Start();
                
            }
    
            private static void UpdateParameters()
            {
                List<string> list = new List<string>();
                list.Add("Test");
                list.Add("Student");
                list.Add("Teacher");
                for (int i = 0; i < list.Count; i++)
                {
                    list[i] = list[i] + i.ToString();
                }
                for (int i = 0; i < list.Count; i++)
                {
                    Console.WriteLine(list[i]);
                }
                Console.ReadKey();
            }
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 4, 2019 2:54 AM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 23, 2019 7:43 AM