1

I'm trying to create a class to handle my downloading and one thing I'm running into is

Invoke or BeginInvoke cannot be called on a control until the window handle has been created

The code I'm trying to run is

    public bool TransferProgress(TransferProgress progress)
    {
        if (!mainform.IsHandleCreated)
        {
            mainform.CreateControl();
        }
        mainform.amountdl.Invoke((MethodInvoker)(() => mainform.amountdl.Text = "Downloaded " + progress.ReceivedObjects + "/" + progress.TotalObjects));
        if (progress.TotalObjects == progress.ReceivedObjects)
        {
            mainform.amountdl.Invoke((MethodInvoker)(() => mainform.amountdl.Text = "Configuring Files Please Wait."));               
        }
        return true;
    }

Up top I thought I was creating a window handle, but C# doesn't agree with me. Last, but not least.

DoxramosManager mainform = new DoxramosManager();

Is up top within my class. My class is not a Winforms form.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Morgan Green
  • 1,012
  • 2
  • 10
  • 22

1 Answers1

0

You will need to make sure the form has loaded before calling invoke. Try using the Loaded event.

Edit: Something like this maybe:

if (!mainform.IsHandleCreated)
        {
            mainform.CreateControl();

            mainform.Loaded += (s, ea) =>
            {
                mainform.amountdl.Invoke((MethodInvoker)(() => mainform.amountdl.Text = "Downloaded " + progress.ReceivedObjects + "/" + progress.TotalObjects));
                if (progress.TotalObjects == progress.ReceivedObjects)
                {
                    mainform.amountdl.Invoke((MethodInvoker)(() => mainform.amountdl.Text = "Configuring Files Please Wait."));
                }
            };
        }
        else
        {
            mainform.amountdl.Invoke((MethodInvoker)(() => mainform.amountdl.Text = "Downloaded " + progress.ReceivedObjects + "/" + progress.TotalObjects));
            if (progress.TotalObjects == progress.ReceivedObjects)
            {
                mainform.amountdl.Invoke((MethodInvoker)(() => mainform.amountdl.Text = "Configuring Files Please Wait."));
            }
        }
Kris
  • 1,336
  • 11
  • 16
  • It's not a form however, can a loaded event be triggered on a c# script? – Morgan Green Nov 18 '16 at 05:30
  • Invoke and BeginInvoke are used to access objects on the UI thread. So the assumption here is that the parent object being invoked exists in the UI and has been completely loaded. – Kris Nov 18 '16 at 05:37
  • I'm trying to figure out what the Loaded event is. I've tried pulling it up on documents and everywhere I look I'm having no luck. – Morgan Green Nov 18 '16 at 05:42
  • Is mainform a UI element on the UI Thread? – Kris Nov 18 '16 at 05:43
  • Mainform is on the UI thread yes. It's working with my DownloadHandler.cs (No UI) – Morgan Green Nov 18 '16 at 05:44
  • CreateControl() does this add/remove UI elements? If so these operations will take some time on the UI thread to complete so simply calling CreateControl() then calling Invoke runs the risk of calling invoke on a control that hasn't been created yet. Maybe instead of form Loaded use the control HandleCreated event. – Kris Nov 18 '16 at 05:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128412/discussion-between-morgan-green-and-kris). – Morgan Green Nov 18 '16 at 05:47