سؤال

So I need to have my IP string to be used by 2 threads of my program. I don't know alot about classes and voids but at the time I have it kind of like this:

    static void Main(string[] args)
    {
        string IP = "127.0.0.1"
    }

and I want to use it in another thread:

    static void th1T()
    {
        while (true)
        {
            var ping = new Ping();
            ping.Send(IP);
        }
    }

How can I get that to work? I know I am doing something wrong, but don't know what i shall use instead.

هل كانت مفيدة؟

المحلول

The first issue is that IP is scoped to the method Main. You'll need to declare IP somewhere th1T can get to if you truly want to share it.

static string IP = "127.0.0.1"

static void Main(string[] args)
{
    // do some work and start th1T()
}

static void th1T()
{
    while (true)
    {
        var ping = new Ping();
        ping.Send(IP);
    }
}

Here you'll be able to reach IP even if th1T is running on a different thread. There are some other options as well though, like injecting the IP into th1T when the thread is started. To do that you'd have to change the signature of th1T to this:

static void th1T(object data)

and you'd have to change the code a little:

static void th1T(object data)
{
    while (true)
    {
        var ping = new Ping();
        ping.Send(data as string);
    }
}

You could then start that on another thread like this:

Thread newThread = new Thread(th1T);
newThread.Start(IP);

Threading is very much a subjective subject. If the value you're referencing from another thread is being read only, like in your example, and it's only set once by the controller (the class starting the work), then it's absolutely valid to share the variable between threads. Many however will argue to their death on this.

We can only speak in generalities in respect to this issue. For example, in general it's more appropriate to inject the value to avoid race conditions and dead locks. But again, that would really depend on what you're doing with the value. You can't inject the value if you're reading a flag from one thread that's set by another. In that case you have to safely synchronize the value.

There's no need to continue because as you see the rabbit hole only gets deeper.

نصائح أخرى

You should refactor your method that pings to accept a string as a parameter:

static void th1T(string IP)
{
    while (true)
    {
        var ping = new Ping();
        ping.Send(IP);
    }
}

And when you call the method, pass in your IP.

What you're wanting to do amounts to a global variable, which is generally not a good idea.

Another approach you could use is a static class to store the value:

public static class Values
{
    string IP { get; set; }
}

And in your method:

static void th1T()
{
    while (true)
    {
        var ping = new Ping();
        ping.Send(Values.IP);
    }
}

I want to add that there are several issues that could arise from this approach. It is probably not a best practice to do it this way.

You haven't shown how you are starting the thread but in all cases you have the possibility to send the string as parameter to the thread. For example if you are manually spawning a new Thread you could pass it as parameter to the Start method:

static void Main(string[] args)
{
    string IP = "127.0.0.1"
    Thread t = new Thread(th1T);
    t.Start(IP);
} 

static void th1T(object value)
{
    // The value parameter will contain the IP here
    string ip = (string)value;
    while (true)
    {
        var ping = new Ping();
        ping.Send(ip);
    }
}

Notice how the th1T method now takes an object parameter that you could cast back to the value being passed. In this example we have passed a simple string value but you could pass arbitrary complex objects.

If you are using Tasks you also have the possibility to pass parameters:

static void Main(string[] args)
{
    string IP = "127.0.0.1"
    Task.Factory.StartNew(th1T, IP);
} 

static void th1T(object value)
{
    // The value parameter will contain the IP here
    string ip = (string)value;
    while (true)
    {
        var ping = new Ping();
        ping.Send(ip);
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top