JXITC
JXITC

Reputation: 1178

Cross thread manipulate WPF control error: cannot

I came across a problem while trying to manipulate the FlowDocumentScrollViewer control in WPF. I did searched through the internet about this kind of error, but found my problem is quite unique.

this is the code:

    delegate void delg_FlowDoc(FlowDocument fdoc);

    //Function provided for both internal calling and outside calling 
    public void setfDocDisplay(FlowDocument fdoc)
    {

        FlowDocumentScrollViewer display = this.fDocDisplay;  //control to be operated

        if (display.Dispatcher.CheckAccess())
        {
            //called from own thread, just operate directly!

            string debug_check_ThreadName = Thread.CurrentThread.Name;
            if (debug_check_ThreadName == "Thread_MainUI")
            {

                //Debug only, now current thread MUST be the thread
                //created the UI!!!!
                display.Document = fdoc;  //<-- Error this line
            }
        }
        else
        {
            //called from other threads, use Invoke()!
            delg_FlowDoc pFun = new delg_FlowDoc(setfDocDisplay);
            this.Dispatcher.Invoke(pFun, new object[] { fdoc });

            //display.Dispatcher.Invoke(pFun, new object[] { fdoc });
            //this.Dispatcher.BeginInvoke(pFun, new object[] { fdoc });
        }
    }

And an error occurred in line display.Document = fdoc;, saying still cannot access the control from a different thread.

And this is the detailed snapshot of error: Error!

I am confused since:

  1. I have already performed display.Dispatcher.CheckAccess() to ensure current thread has the permission to operate on the control display.

  2. I again add a debug_check_ThreadName to double ensure it's in the right thread for that moment.

  3. I tried exactly the same code in another but smaller WPF project that is free of errors

But still got the same problem telling me I try to operate from a different thread.

I am not familiar with other properties of Dispatcher or Invoke etc. properties so I have no idea now to solve this problem.

If anyone can give me any hint of this error?

Thank you!

[SOLVED]

Thank you 500 - Internal Server Error!

Since I am still shocked by the fact, I decide to put my understanding in the question to facilitate some one else have the same problem and saw this question.

I have now fixed the problem. It is because the FlowDocument fdoc is actually a type of UI control and created in another thread. So when the main windows thread want to operate that fdoc - although it has permission to FlowDocumentScrollViewer display - it doesn't have permission to the other control fdoc in this assignment sentence.

It's rather weird!! I thought fdoc is just a normal variable like a string or int vars... ><

Upvotes: 1

Views: 446

Answers (1)

It is not the control you (try to) assign to that's the problem - it's the source document that's not created on the UI thread.

Upvotes: 4

Related Questions