C#Windows窗体应用程序错误:“跨线程操作无效”。

时间:2013-08-14 16:12:18

标签: c# .net winforms

我正在尝试在C#中构建一个简单的Windows窗体应用程序。其中lblIPAddress显示我的本地IP地址,lblInfo显示我上次更新的IP地址时间。btnRefresh用于刷新数据。但是只要tickTimer_Elapsed事件触发它就会抛出错误“跨线程操作无效:控制'lblInfo'从其创建的线程以外的线程访问。”

请建议。我的完整代码如下所示:

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;             
        }
    }
}

2 个答案:

答案 0 :(得分:3)

为什么不使用System.Windows.Forms.Timer而不是System.Timers.Timer? System.Windows.Forms.Timer旨在避免这类问题。

答案 1 :(得分:1)

您需要从Timer线程“调用”UI上的操作。如果没有Invoke调用,则不允许在Winforms UI上进行跨线程调用。

How to invoke a UI method from another thread