LonelyPyxel
LonelyPyxel

Reputation: 121

Passing variables between 2 threads in a c# console application (newbie here)

I'm wondering how I can send a variable from one thread to another in a c# console application. For example,

using System;
using System.Threading;

namespace example
{
    class Program
    {
        static void Main(string[] args)
        {
            int examplevariable = Convert.ToInt32(Console.ReadLine ());
            Thread t = new Thread(secondthread);
            t.Start();

        }

    static void secondthread()
    {
        Console.WriteLine(+examplevariable);
    }
}
}

I want to make "secondthread" recognize "examplevariable".

Upvotes: 3

Views: 260

Answers (2)

Mostafiz
Mostafiz

Reputation: 7352

There is an overload to Thread.Start() that takes a parameter as object. You can pass your main thread variable to that and cast it as your variable type

    using System;
    using System.Threading;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int examplevariable = Convert.ToInt32(Console.ReadLine());
                Thread t = new Thread(secondthread);
                t.Start(examplevariable);
            }

            static void secondthread(object obj)
            {
                int examplevariable = (int) obj;
                Console.WriteLine(examplevariable);
                Console.Read();
            }

        }
    }

if you want to pass multiple variable then use a model class and use property binding like below

using System;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TestModel tm = new TestModel();
            tm.examplevariable1 = Convert.ToInt32(Console.ReadLine());
            tm.examplevariable2 = Console.ReadLine();
            Thread t = new Thread(secondthread);
            t.Start(tm);
        }

        static void secondthread(object obj)
        {
            TestModel newTm = (TestModel) obj;
            Console.WriteLine(newTm.examplevariable1);
            Console.WriteLine(newTm.examplevariable2);
            Console.Read();
        }

    }

    class TestModel
    {
        public int examplevariable1 { get; set; }
        public string examplevariable2 { get; set; }

    }
}

Hope this will help

Upvotes: 3

markwatsonatx
markwatsonatx

Reputation: 3501

An easy way to do this, but might not work in all scenarios, would be to define a static variable on the class and assign the value read in from the console to the static variable. Like so:

class Program
{

    static int examplevariable;

    static void Main(string[] args)
    {
        examplevariable = Convert.ToInt32(Console.ReadLine ());
        Thread t = new Thread(secondthread);
        t.Start();

    }

    static void secondthread()
    {
        Console.WriteLine(+examplevariable);
    }

Also, see this question on how to pass parameters to a Thread:

ThreadStart with parameters

Upvotes: 1

Related Questions