Toolstrip and threading


Author
Message
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.5K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
I tried to create a user control that included a TxText control and a toolstring (within a toolstrip container). It works great (I used the SF richtextbox/richtextboxtoolstrip as a starting place) except when I use it on a form that I load via another thread (I instantiate the form on another thread, save it away in a static field). The form is a wizard form (SF wizard) and one of the pages has this TxTextControl user control on it. When that page is navigated to, I get a threading error saying that I can't access the toolstrip from another thread that it was created on. None of the other controls are having problems, just the .NET toolstrip. Does anyone know what the problem is?
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Yes....welcome to the world of threading.  We briefly covered this in class in regards to creating a thread and Invoking on the thread an object was created on.  Imagine two threads, Thread1 is the form where the controls reside, Thread2 is the thread where data is going to be retrieved, for example.  If Thread2 tries to access a control on Thread1, you will receive a Cross Thread Violation.  This is not safe.  If you look back in your notes, Ben talked about using an ISynchronize interface that allows you to invoke off of the thread where the control is dropped rather than the Thread2 where processing is taking place and is NOT the same thread the control was created on. The incorrect and "cheap-Johnny" way to get around this is to turn off the CrossThreadViolations:

System.Windows.Forms.Form.CheckForIllegalCrossThreads = False

BUT THIS IS NOT RECOMMENDED!  You should Invoke on the proper thread and correctly resolve the illegal cross thread.  You can implement the ISynchronizeInvoke interface on your user control (a sample was given during class).  You could also pass over a reference of the form to the user control (which is what the ISynchronizeInvoke interface would ultimately help you do) and Invoke off of that object.

_SyncObject.Invoke(...)

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.5K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
OK, I think I get this. But what is confusing me is that the second thread is long dead when this occurs. I.e. I use the thread to instantiate the form, but it is shown/hidden/interacted with on the main thread. I don't have another thread to invoke from/to. I.e.



During the MDI form load, I start a thread, it calls this code:



_wizardCachedForm = New WizardForm





That's it. _wizardCachedForm is a static field. Everything else occurs on the main thread, long after the thread that was used to instatiate it is dead and buried, basically the code is:



_wizardCachedForm.Visible=true



Except for the toolstrip, everthing works great. Maybe I can't use this technic? That would be beyond a major bummer Crying
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
You can, but the object has probably been disposed or is trying to be disposed and one thread is not allowed to touch the other.  If you manually set your cache property to Nothing before it is disposd then your error may go away.
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search