backgroundworker_progresschanged 什么都不做
backgroundworker_progresschanged not doing anything
我有一个程序,我想检查用户机器的现有内部 IP,然后在它连接到 VPN 后检查新的内部 IP。我用后台工作人员检查用户的 IP 是否已更改。这是我的以下代码:
var worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerAsync();
void worker_DoWork(object sender, DoWorkEventArgs e)
{
while (Connection.CheckForInternetConnection()) //Makes sure an internet connection is available
{
CurrentInternalIP = GetIPAddress(); //Regularly retrieve the current IP so we can compare against the old IP
if (CurrentInternalIP != OldInternalIP) //Compare the current IP address with the old one
{
MessageBox.Show("IP has changed");//Make sure the event is working correctly
break;
}
}
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lblStatus.Text = "Connected to VPN.";
}
现在 worker_DoWork 活动正常运行。但是,一旦 worker_DoWork 事件中的 IP 发生更改,worker_ProgressChanged 事件就不会更改标签的文本。我最初使用代码更改 worker_DoWork 事件中的标签文本,但这给了我一个错误,因为我访问的标签不是 UI 创建的标签 - 所以这不是'不可能。感谢您的帮助。
查看您的代码,您已将工作人员的 DoWork
事件分配给 worker_DoWork
EventHandler,但到目前为止您还没有分配 ProgressChanged
。为了引发此事件,您必须将 WorkerReportsProgress
设置为 true
要使其正常工作 您必须分配 Progress 的 EeventHandler 已更改,这可以通过以下方式完成:
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.WorkerReportsProgress = true;
并且您可以调用以下方法来触发进度更改事件:
worker.ReportProgress(intPercent);// intPercent is an integer denotes the percentage
我有一个程序,我想检查用户机器的现有内部 IP,然后在它连接到 VPN 后检查新的内部 IP。我用后台工作人员检查用户的 IP 是否已更改。这是我的以下代码:
var worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerAsync();
void worker_DoWork(object sender, DoWorkEventArgs e)
{
while (Connection.CheckForInternetConnection()) //Makes sure an internet connection is available
{
CurrentInternalIP = GetIPAddress(); //Regularly retrieve the current IP so we can compare against the old IP
if (CurrentInternalIP != OldInternalIP) //Compare the current IP address with the old one
{
MessageBox.Show("IP has changed");//Make sure the event is working correctly
break;
}
}
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lblStatus.Text = "Connected to VPN.";
}
现在 worker_DoWork 活动正常运行。但是,一旦 worker_DoWork 事件中的 IP 发生更改,worker_ProgressChanged 事件就不会更改标签的文本。我最初使用代码更改 worker_DoWork 事件中的标签文本,但这给了我一个错误,因为我访问的标签不是 UI 创建的标签 - 所以这不是'不可能。感谢您的帮助。
查看您的代码,您已将工作人员的 DoWork
事件分配给 worker_DoWork
EventHandler,但到目前为止您还没有分配 ProgressChanged
。为了引发此事件,您必须将 WorkerReportsProgress
设置为 true
要使其正常工作 您必须分配 Progress 的 EeventHandler 已更改,这可以通过以下方式完成:
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.WorkerReportsProgress = true;
并且您可以调用以下方法来触发进度更改事件:
worker.ReportProgress(intPercent);// intPercent is an integer denotes the percentage