-2

Here's DoWork:

private void uploadWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            uploadWorker.ReportProgress(20);

            int tiffError = 0;

            finalFiles = Directory.GetFiles(AppVars.FinalPolicyImagesFolder);

            foreach (string file in finalFiles)
            {
                if (!file.EndsWith(".tiff"))
                {
                    tiffError = 1;
                    break;
                }
            }

            uploadWorker.ReportProgress(50);

            if (tiffError == 1)
            {
                MessageBox.Show("There are files in this folder that are not .tiff. Please ensure only .tiff files are in this folder.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (finalFiles.Length == 0)
                {
                    MessageBox.Show("There are no TIFF files to be uploaded. Please generate files first.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    pbUpload.Value = 0;
                    EnableAllButtons();
                }
                else
                {
                    double count = finalFiles.Length;
                    int current = 0;
                    int pbValue = 0;

                    uploadWorker.ReportProgress(70);

                    foreach (string file in finalFiles)
                    {
                        current = current + 2;

                        if (file.Contains(".tiff") == true)
                        {
                            PolicyNumber = Path.GetFileName(file).Split('_')[0];
                            basePolicyNumber = PolicyNumber.Remove(PolicyNumber.Length - 2);
                            basePolicyNumber = basePolicyNumber + "00";

                            finalPolicyName = Path.GetFileName(file);

                            PolicyUUID = Transporter.GetPolicyUUID(AppVars.pxCentralRootURL, basePolicyNumber);

                            if (PolicyUUID == "")
                            {
                                MessageBox.Show("The InsightPolicyID for the policy you are trying to upload does not exist in ixLibrary. Please ensure the policy number is correct. If you are sure it should be in ixLibray, please contact IT.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            else
                            {
                                ixLibrarySourceFileURL = AppVars.ixLibraryPolicyAttachmentsURL + finalPolicyName;

                                UploadToixLibraryErrorCode = Transporter.UploadFileToixLibrary(AppVars.ixLibraryPolicyAttachmentsURL, file);

                                if (UploadToixLibraryErrorCode != 0)
                                {
                                    MessageBox.Show("There was an error uploading the file to ixLibrary. Please contact IT about this problem.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                                else
                                {
                                    GeneratePayLoadErrorCode = Transformer.GeneratePayLoad(ixLibrarySourceFileURL, finalPolicyName);

                                    if (GeneratePayLoadErrorCode != 0)
                                    {
                                        MessageBox.Show("There was an error generating the XML for pxCentral. Please contact IT about this problem.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                    else
                                    {
                                        pxCentralPOSTErrorCode = Transporter.pxCentralPOST(AppVars.pxCentralRootURL + PolicyUUID, AppVars.pxCentralXMLPayloadFilePath);

                                        pbValue = Convert.ToInt32(((current / count) * 30) + 70);

                                        uploadWorker.ReportProgress(pbValue);
                                    }
                                }
                            }
                        } 
                    }
                }
            }
        }

As soon as it hits the last }, I get the TargetInvocationException was unhandled error here (see comment in code):

static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            bool createdNew = false;

            Mutex mutex = new Mutex(true, "MyApplicationMutex", out createdNew);

            if (createdNew == true)
            {
                //error happens here
                Application.Run(new frmMain());
            }
            else
            {
                MessageBox.Show("The application is already running.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }

I'm not sure why this started happening all of the sudden. Does anyone know why?

Finally, here's RunWorkerCompleted:

private void uploadWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                DeleteFinalFiles(finalFiles);
                MessageBox.Show("Upload process complete.", "Complete!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            EnableAllButtons();
        }
JJ.
  • 9,580
  • 37
  • 116
  • 189
  • Something is probably wrong in your background worker's `RunWorkerCompleted` event handler. – Kevin Gosse Aug 28 '12 at 15:19
  • I added it in the OP, check it out – JJ. Aug 28 '12 at 15:20
  • Just a shot from the hip, but you might need to make sure you're not engaging some sort of cross-thread execution issue, eg, form invoked on separate thread, background thread running, UI control affected on non-UI thread. Should bring up an explicit error, but might be a different manifestation in this case... – David W Aug 28 '12 at 15:26
  • 1
    @AmiramKorach, I checked it and I figured out what the problem was. In the doWork, I was exceeding the maximum progress bar value (which is 100), it was showing 130... I fixed this and problem solved. Thanks!! – JJ. Aug 28 '12 at 15:28
  • @Testifier If that was your problem please post it as an answer and accept it so that the answer will be more visible to any future visitors. – Servy Aug 28 '12 at 15:31
  • added my own answer, but i can't accept my own answers tho... – JJ. Aug 28 '12 at 15:35
  • @Testifier That's fine. The big thing is that important information should always be in a question or answer (answer in the case of a solution to the problem), not a comment. Having the answer accepted is of a lower priority. – Servy Aug 28 '12 at 15:48

4 Answers4

3

You are calling EnableAllButtons in your DoWork handler. this, presumably, changes the Enabled state of buttons on the form. that is not legal from any other thread than the UI thread. You should make the call to EnableAllButtons in your ProgressChanged event handler or in your RunWorkerCompleted event handler. You're also calling ProgressBar.Value in the DoWork with the code pbUpload.Value = 0.

Also, you should call MessageBox.Show from your UI thread (i.e. in ProgressChanged or RunworkerCompleted handler) so that the MessageBox can be associated with your forms message pump propertly. You should pass a form object to MessageBox.Show to associate the message box with the form so you can't bring the form to the foreground while the message box is shown. e.g.:

MessageBox.Show(this, 
    "There are files in this folder that are not .tiff. Please ensure only .tiff files are in this folder.", 
    "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
0

In WinForms, you must only access a control on the thread that created the control. Your DoWork event handler is not running on the thread that created the form (which, of course, is the point). Therefore, you must not access any of the controls on the form in the DoWork handler. Doing so can create unpredictable results.

Scott
  • 4,458
  • 1
  • 19
  • 27
0

I had exactly the same problem with TargetInvocation Exception raised after the completion of the background process. Inside the backgroundWorker ProgressChanges Event I have references to controls as shown below`private void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {

       // This function fires on the UI thread so it's safe to edit
       // the UI control directly, no funny business with Control.Invoke :)
       CurrentState state =
               (CurrentState)e.UserState;
       txtProgress.Text = state.CrawlStatus;
       lblStatus2.Text = state.sStatus;
       txtItemsStored.Text = state.TotItems.ToString() + " items";
       txtLastRunTime.Text = state.MostRecentGatherDate.ToString();
       AppNameKey.SetValue("LastCrawlTime", txtLastRunTime.Text);
   }`

The DoWork event reads from a control

private  void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
   {

       DateTime LastCrawlTime;
       try
       {
         LastCrawlTime = Convert.ToDateTime(txtLastRunTime.Text);
        if (lblStatus2.Text != "Status: Running" || (!cmdRunNow.Enabled && cmdStopRun.Enabled)) // run is not currently running or cmdRunNow clicked
        {
            //lblStatus2.Text = "Status: Running";
            GetUpdated(LastCrawlTime,e);
        }
       }
       catch (Exception Ex)
       {
           MessageBox.Show(Ex.Message);
       }

   }

The RunWorkedrCompleted Event writes to a control:

void m_oWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
   {
        if (e.Cancelled)
       {
           lblStatus2.Text = "Status: Stopped";
           cmdStopRun.Enabled = false;
       }
       // Check to see if an error occurred in the background process.
       else if (e.Error != null)
       {
           lblStatus2.Text = "Fatal Error while processing.";
       }
       else
       {
           // Everything completed normally.
           //CurrentState state = (CurrentState)e.UserState;
           lblStatus2.Text = "Status: Finished";            
       }

   }

None of these caused problems. What did cause the problem was the attempt to reference e.UserState in the RunWorker_Completed event (commented out above)

SimonKravis
  • 553
  • 1
  • 3
  • 24
-1

I figured out the issue.

The progress bar was exceeding its maximum allowed (of 100).

The problem was that in the code, I was incrementing the progress bar as such:

current = current + 2;

I replaced it with:

current++;

The reason why I was incrementing by 2 was simply for testing purposes.

JJ.
  • 9,580
  • 37
  • 116
  • 189
  • That will not cause a `TargetInvocationException` it will cause a `System.ArgumentOutOfRangeException` – Peter Ritchie Aug 28 '12 at 15:48
  • If you're getting `TargetInvocationException` you're setting the progress bar `Value` on the wrong thread. If you're setting the progress bar `Value` to a value outside the range, you will get an `ArgumentOutOfRangeException`. Changing what value you set the progress bar `Value` to won't get rid of a `TargetInvocationException`. If that's not the problem you're having, don't put that in the title. – Peter Ritchie Aug 28 '12 at 16:01
  • @PeterRitchie, that was exactly the error I was receiving. Then when I looked at the INNER EXCEPTION as someone else mentioned here, I saw where the error was, which was the progressbar value issue. Fixing the progress bar value fixed my problem and TargetInvocationException disappeared. Don't believe me, do it yourself and see it for yourself. – JJ. Aug 28 '12 at 20:44
  • You've still got `pbUpload.Value = 0;` and `EnableAllButtons();` in your DoWork method, which will cause you problems `pbUpload` is a `ProgressBar` and `EnableAllButtons` does anything with a `Button` object. – Peter Ritchie Aug 28 '12 at 20:51