异常出现在以下代码中:
这是form1中的ActiveDownloadJob类:
private void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
ActiveDownloadJob adJob = e.UserState as ActiveDownloadJob;
if (adJob != null && adJob.ProgressBar != null)
{
adJob.ProgressBar.Invoke((Action)(() => adJob.ProgressBar.Value = e.ProgressPercentage));
}
}
在线上:
adJob.ProgressBar.Invoke((Action)(() => adJob.ProgressBar.Value = e.ProgressPercentage));
这是form1中的ActiveDownloadJob类:
class ActiveDownloadJob
{
public DownloadImages.DownloadData DownloadData;
public ProgressBar ProgressBar;
public WebClient WebClient;
public ActiveDownloadJob(DownloadImages.DownloadData downloadData, ProgressBar progressBar, WebClient webClient)
{
try
{
this.DownloadData = downloadData;
this.ProgressBar = progressBar;
this.WebClient = webClient;
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
}
我不确定是否需要调用这行代码,因为现在我没有使用BackgroundWorker,但我不确定。
完整的异常信息是:无法在控件的窗口句柄创建之前调用Invoke或BeginInvoke。
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
at WeatherMaps.Form1.DownloadProgressCallback(Object sender, DownloadProgressChangedEventArgs e) in d:\C-Sharp\WeatherMaps\WeatherMaps\WeatherMaps\Form1.cs:line 290
at System.Net.WebClient.OnDownloadProgressChanged(DownloadProgressChangedEventArgs e)
at System.Net.WebClient.ReportDownloadProgressChanged(Object arg)
InnerException:
如何在不使用Invoke的情况下更改此行?如果需要使用Invoke,我该如何修复这行和异常?
我知道我应该在Form1窗体关闭事件中处理它,但是怎么处理?在Form1窗体关闭事件中应该做什么?