0

i will start with saying that i don't know c# very good and it's probable a very simple solution.

what i want to achieve is when network status is changed i want to change a label

i found out how to triger and event when network is changed

using System.Net.NetworkInformation;

    private void Form1_Load(object sender, EventArgs e)
    {
        NetworkChange.NetworkAddressChanged += new                                     
        NetworkAddressChangedEventHandler(AddressChangedCallback);                     
    }

    public void AddressChangedCallback(object sender, EventArgs e)                     
    {

        NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface n in adapters)
        {
          label1.Text = "bla bla";
        }
    }

now when i disabled a nic card i can see that AddressChangedCallback is called but then VS is stoping or an error "Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on."

what i need to change in my code in order to change the label from AddressChangedCallback

Thanks :)

ygarti
  • 3
  • 3
  • 2
    possible duplicate of [How to update the GUI from another thread in C#?](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c) – cbr Jun 18 '15 at 15:09

1 Answers1

0

after more search on the subject i found that this.Invoke((MethodInvoker)delegate is doing what i need

        this.Invoke((MethodInvoker)delegate
        {
                 //your code here
        });
ygarti
  • 3
  • 3