Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I'm getting exception: InvalidOperationException?

The exception is on the code:

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

On the line:

adJob.ProgressBar.Invoke((Action)(() => adJob.ProgressBar.Value = e.ProgressPercentage));

This is the ActiveDownloadJob class in form1:

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

Im not sure i need to invoke this line since im not using now a backgroundworker , but im not sure.

This is the full exception message: Invoke or BeginInvoke cannot be called on a control until the window handle has been created

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: 

How can i change this line to be without using Invoke or if Invoke is needed how can i fix the line and the exception ?

I know i should handle it in the Form1 Form closing event but how ? What should i do in the form1 form closing event ?

like image 787
Doron Muzar Avatar asked Mar 21 '23 16:03

Doron Muzar


1 Answers

Yes, you get an exception because Invoke needs to post the "Message" to "Message loop" but Handle has not been created.

Use InvokeRequired to see whether you need an Invoke, this will return false when Handle is not created yet so invoke it directly.

var method = (Action)(() => adJob.ProgressBar.Value = e.ProgressPercentage);
if(adJob.ProgressBar.InvokeRequired)
    adJob.ProgressBar.Invoke(method);
else
    method();
like image 108
Sriram Sakthivel Avatar answered Apr 07 '23 15:04

Sriram Sakthivel