C#当出现新数据时,如何更新或刷新函数?
我正在做一个小计划,让我的工作生活更轻松。它查找用户名、主机名、MAC地址和IP地址 当用户将鼠标悬停在系统托盘图标上时,将显示包含所有信息的工具提示。到目前为止,它工作得很好,除非出现新的网络连接,否则它将无法获得IP 我必须关闭程序并重新打开它,然后它才能获得新的IP地址。我该怎么做才能让它在不重新启动应用程序的情况下获取新检测到的网络连接的IP地址 我不想定时刷新,它可能会占用太多资源。我把我申请的肉和土豆都包括进去了C#当出现新数据时,如何更新或刷新函数?,c#,function,ip,refresh,tooltip,C#,Function,Ip,Refresh,Tooltip,我正在做一个小计划,让我的工作生活更轻松。它查找用户名、主机名、MAC地址和IP地址 当用户将鼠标悬停在系统托盘图标上时,将显示包含所有信息的工具提示。到目前为止,它工作得很好,除非出现新的网络连接,否则它将无法获得IP 我必须关闭程序并重新打开它,然后它才能获得新的IP地址。我该怎么做才能让它在不重新启动应用程序的情况下获取新检测到的网络连接的IP地址 我不想定时刷新,它可能会占用太多资源。我把我申请的肉和土豆都包括进去了 class ProcessIcon : IDisposable {
class ProcessIcon : IDisposable
{
/// <summary>
/// The NotifyIcon object.
/// </summary>
NotifyIcon ni;
/// <summary>
/// Initializes a new instance of the <see cref="ProcessIcon"/> class.
/// </summary>
// Instantiate the NotifyIcon object.
public ProcessIcon()
{
ni = new NotifyIcon();
}
//Get DNS of computer
public static string GetDNS()
{
String strHostName = string.Empty;
strHostName = Dns.GetHostName();
return strHostName;
}
//Get IP Address(s) of computer
static public string GetIP()
{
string strReturn = string.Empty;
//This gets the computers DNS
String strHostName = string.Empty;
strHostName = Dns.GetHostName();
// Then using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
List<string> lstIP = new List<string>();
for (int i = 0; i < addr.Length; i++)
{
if (addr[i].AddressFamily == AddressFamily.InterNetworkV6)
{
//DO NOTHING. This If statements checks for IPV6 addresses and excludes them from the output.
}
else
{
//This if statement checks if the address is a IPV4 and if it is, it adds it to the string.
if (addr[i].AddressFamily == AddressFamily.InterNetwork)
{
strReturn += (addr[i].ToString() + "\t");
}
else
{
//Nothing for now
}
}
}
return strReturn;
}
//Gets the computers MAC address for ethernet
public static string getMAC()
{
string macAddresses = "";
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
// Only consider Ethernet network interfaces, thereby ignoring any
// loopback devices etc.
if (nic.NetworkInterfaceType != NetworkInterfaceType.Ethernet) continue;
if (nic.OperationalStatus == OperationalStatus.Up)
{
macAddresses += nic.GetPhysicalAddress().ToString();
break;
}
}
return macAddresses;
}
public static string GetUSER()
{
string username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
return username;
}
//String that combines the DNS,MAC and IP address strings into one that is formatted for easy viewing.
public static string showALL()
{
string showALL = "User: " + GetUSER() + Environment.NewLine + "DNS: " + GetDNS() + Environment.NewLine + "MAC: " + getMAC() + Environment.NewLine + "IP: " + GetIP();
return showALL;
}
/// <summary>
/// Displays the icon in the system tray.
/// </summary>
public void Display()
{
// Put the icon in the system tray and allow it react to mouse clicks.
ni.MouseClick += new MouseEventHandler(ni_MouseClick1);
ni.Icon = Resources.SystemTrayApp;
Fixes.SetNotifyIconText(ni,showALL());
ni.Visible = true;
// Attach a context menu.
ni.ContextMenuStrip = new ContextMenus().Create();
}
private void ni_MouseClick(object sender, MouseEventArgs e)
{
throw new NotImplementedException();
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
// When the application closes, this will remove the icon from the system tray immediately.
public void Dispose()
{
ni.Dispose();
}
/// <summary>
/// Handles the MouseClick event of the ni control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
void ni_MouseClick1 (object sender, MouseEventArgs e)
{
// Handle mouse button clicks.
if (e.Button == MouseButtons.Left)
{
Form1 form1 = new Form1();
form1.Text = "Whats my IP";
form1.Show();
form1.ShowInTaskbar = false;
}
}
类进程图标:IDisposable
{
///
///NotifyIcon对象。
///
尼娜;
///
///初始化类的新实例。
///
//实例化NotifyIcon对象。
公共进程图标()
{
ni=新的通知图标();
}
//获取计算机的DNS
公共静态字符串GetDNS()
{
String strHostName=String.Empty;
strHostName=Dns.GetHostName();
返回strHostName;
}
//获取计算机的IP地址
静态公共字符串GetIP()
{
string strReturn=string.Empty;
//这就得到了计算机的DNS
String strHostName=String.Empty;
strHostName=Dns.GetHostName();
//然后使用主机名获取IP地址列表。。
IPHostEntry ipEntry=Dns.GetHostEntry(strHostName);
IPAddress[]addr=ipEntry.AddressList;
List lstIP=新列表();
for(int i=0;i
您可以在数据更改时广播信号,并向侦听该广播的侦听器注册您的函数。registerReceiver可用于以后广播信号,其功能应与android中的C#类似。您可以添加事件NetworkChange.NetworkAddressChanged并对u更新数据,例如:
static void Main(string[] args)
{
try
{
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
Console.WriteLine(" do Something");
}
我可以用什么代码告诉我的函数GetIP()、GetMAC()、GetIP()获取新数据。如果我一直调用这些函数,我得到的都是相同的旧数据。我决定在我的函数上尝试一个计时器。它实际上工作正常,对资源也不错。所以我的函数GetIP、GetDNS、GetMAC都会更新它们的数据。现在我在更新工具提示时遇到问题。一旦它触发,它就不会改变了。如何在工具提示补丁中添加计时器。SetNotifyIconText(ni,showALL());在public void Display()下?