0

Look at these screenshots. First shows a breakpoint in my code and shows current value of InvokeRequired.

Step 1

Seconds screenshot shows value of InvokeRequired after pressing F10 (step over) one time in debugger (IF statement content is not entered, ELSE is).

Step 2

What happens next, is InvalidOperationException on rtbOutput, because I try to change its fields in ReportProgress. Here is how I call OnProgressReported:

public event EventHandler<BuildProgressEventArgs> BuildProgresReported = delegate { };

public void InvokeReportBuildProgress(BuildProgress progress)
{
    BuildProgresReported.Invoke(this, new BuildProgressEventArgs(progress));
}

How is this even possible? Is there other, better way to detect if Invoke should be called?

Update:

I have changed method code to the following:

public void OnProgressReported( object caller, BuildProgressEventArgs progressEventArgs )
        {
            if( rtbOutput.InvokeRequired )
            {
                Debug.WriteLine($"Invoke was required on thread: #{Thread.CurrentThread.ManagedThreadId} named '{Thread.CurrentThread.Name}'");
                rtbOutput.BeginInvoke( new Action( () => OnProgressReported(caller, progressEventArgs) ) );
            }
            else
            {
                Debug.WriteLine($"Invoke was NOT required on thread: #{Thread.CurrentThread.ManagedThreadId} named '{Thread.CurrentThread.Name}'");
                if ( this.IsDisposed )
                {
                    throw new InvalidOperationException( "This form has been disposed" );
                }

                rtbOutput.Text = "abc";
                rtbOutput.Text = "xxx";
                //ReportProgress(progressEventArgs.Progress);
            }
        }

The only output is

Invoke was NOT required on thread: #15 named ''

and the method this throw an exception on line rtbOutput.Text = "abc";

HOWEVER! When the line rtbOutput.Text = "xxx"; is commented out, everything works fine, even though rtbOutput.Text = "abc"; is still present!

Update #2 - solution:

The problem was a window handle that was not yet created (I subscribe to an event right AFTER creating the window) I have to wait for handle to be created using

this.IsHandleCreated
  • Sounds like two different threads are talking to `OnProgressReported` - one from the worker, and one (presumably the previous update) on the UI thread. Check the `Environment.CurrentManagedThreadId` between the two steps, or just use the thread view in VS. Debugging with F10 becomes a dice game when multiple threads are involved. – Marc Gravell Sep 14 '16 at 11:23
  • Just two options: method has been called twice then second call is in another thread (see the _threads_ icon near current instruction) or control had not handle created (yet). Most probable is first one (see current thread in Stack Window)... – Adriano Repetti Sep 14 '16 at 11:23
  • @AdrianoRepetti it is the same and only thread that is calling this method, however you may be onto someting with handle not yet created. See the post update. – Mateusz Krzaczek Sep 14 '16 at 11:47

0 Answers0