Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Windows Form application Error : "Cross-thread operation not valid."
    primarykey
    data
    text
    <p>I am trying to build a simple windows form application in C#. where <code>lblIPAddress</code> show my local IP address and <code>lblInfo</code>shows my last updated time of my IP address.<code>btnRefresh</code> is use to refresh the data. But as soon as <code>tickTimer_Elapsed</code> event fired it throws the error "Cross-thread operation not valid: Control 'lblInfo' accessed from a thread other than the thread it was created on."</p> <p>Please suggest. My complete code has shown below:</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using Time = System.Timers; using Connect = System.Net.NetworkInformation.NetworkInterface; namespace ShowMyIP { public class UpdatedInfo { public string lastUpdate = string.Empty; public string localIP = string.Empty; } public partial class ShowLocalIP : Form { UpdatedInfo updatedIP; public static Time.Timer tickTimer = new Time.Timer(); public const int INTERVAL = 60 * 1000; public ShowLocalIP() { InitializeComponent(); InitializeTimer(); updatedIP = new UpdatedInfo(); updatedIP = GetLocalIP(updatedIP); lblIPAddress.Text = updatedIP.localIP; lblInfo.Text = updatedIP.lastUpdate; } public void InitializeTimer() { tickTimer.Interval = INTERVAL; tickTimer.Enabled = true; tickTimer.Elapsed += new Time.ElapsedEventHandler(tickTimer_Elapsed); tickTimer.Start(); GC.KeepAlive(tickTimer); } private void tickTimer_Elapsed(object sender, Time.ElapsedEventArgs e) { updatedIP = new UpdatedInfo(); updatedIP = GetLocalIP(updatedIP); lblIPAddress.Text = updatedIP.localIP; lblInfo.Text = updatedIP.lastUpdate; tickTimer.Interval = INTERVAL; tickTimer.Enabled = true; tickTimer.Start(); GC.KeepAlive(tickTimer); } private UpdatedInfo GetLocalIP(UpdatedInfo UpdatedIP) { if (Connect.GetIsNetworkAvailable()) { IPHostEntry host; host = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { UpdatedIP.localIP = ip.ToString(); UpdatedIP.lastUpdate = DateTime.Now.ToShortTimeString(); break; } } } else { UpdatedIP.localIP = "127.0.0.1"; UpdatedIP.lastUpdate = DateTime.Now.ToShortTimeString(); } return UpdatedIP; } private void btnRefresh_Click(object sender, EventArgs e) { updatedIP = new UpdatedInfo(); updatedIP = GetLocalIP(updatedIP); lblIPAddress.Text = updatedIP.localIP; lblInfo.Text = updatedIP.lastUpdate; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload