Infralution Support Forum Index Infralution Support
Support groups for Infralution products
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Cross-thread operation not valid: Control '' accessed from a

 
Post new topic   Reply to topic    Infralution Support Forum Index -> Virtual Tree Support
View previous topic :: View next topic  
Author Message
medbiller



Joined: 20 Dec 2007
Posts: 39
Location: Arroyo, PR

PostPosted: Wed Aug 21, 2013 8:27 pm    Post subject: Cross-thread operation not valid: Control '' accessed from a Reply with quote

"Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on."
This happens after 2 or 3 minutes of checking/unchecking a level 1 checkbox cell.
I have a VTree with 2 tables, "Req" & "Res" and 1 relation (1 to many). Both tables have checkbox editors bound at the rowbinding level & both are independently handled the same way. Example for table Req chekbox

Private Sub CellEditorChkReq_GetControlValue(sender As Object, e As Infralution.Controls.VirtualTree.CellEditorGetValueEventArgs) Handles CellEditorChkReq.GetControlValue
If TypeOf e.CellWidget.EditorControl Is CheckBox Then
If vtResults.SelectedRow IsNot Nothing AndAlso vtResults.SelectedRow.Item(TstReqSamplID.Name) IsNot Nothing Then
Dim chk As CheckBox = DirectCast(e.CellWidget.EditorControl, CheckBox)
vtResults.SelectedItem(selReq.Name) = chk.Checked
Dim Sql As New StringBuilder
Sql.Append("UPDATE TstRequest SET TstReqSelect = " & TrueFalse2OneZero(chk) & " WHERE RTRIM(TstReqSamplID) + RTRIM(TstReqSamplIDSeq) = '" _
& vtResults.SelectedRow.Item(sampleIDSent.Name).ToString & "';")
Using cmd As New SqlCommand(Sql.ToString, goConnMB)
cmd.ExecuteNonQuery()
End Using
End If
End If
End Sub

Private Sub vtResults_GetCellData(sender As Object, e As Infralution.Controls.VirtualTree.GetCellDataEventArgs) Handles vtResults.GetCellData
Dim vt As Infralution.Controls.VirtualTree.VirtualTree = DirectCast(sender, Infralution.Controls.VirtualTree.VirtualTree)
Dim binding As RowBinding = vt.GetRowBinding(e.Row) 'if these three lines are not here the tree shows up empty
If binding IsNot Nothing Then
binding.GetCellData(e.Row, e.Column, e.CellData) 'get the default binding for the given row and use it to populate the cell data
Dim oddCellStyle As New Infralution.Controls.Style(e.CellData.OddStyle) 'must define a new style derived from the current style
Dim evenCellStyle As New Infralution.Controls.Style(e.CellData.EvenStyle) 'must define a new style derived from the current style

Select Case e.Row.Level
Case 1
oddCellStyle.BackColor = reqColor 'change background color on new style
evenCellStyle.BackColor = ControlPaint.Light(reqColor, 0.25) 'see comment for oddCellStyle

If e.Column Is selReq Then 'this shows in vtResults checkbox col selReq what the CellEditorChkReq.GetControlValue set in database
If Not IsDBNull(e.Row.Item(selReq.Name)) AndAlso e.Row.Item(selReq.Name) Then 'e.Row.Item(selReq.Name) = current value in datarow's selReq cell
e.CellData.Value = True
Else
e.CellData.Value = False
End If
End If


Case 2
oddCellStyle.BackColor = ControlPaint.Light(resColor, 0.5)
evenCellStyle.BackColor = ControlPaint.Light(resColor, 0.5)

If (Not IsDBNull(e.Row.Item(highLow.Name)) AndAlso Field2Str(e.Row.Item(highLow.Name)).Length > 0) Then
oddCellStyle.BackColor = ControlPaint.LightLight(ControlPaint.Light(Color.LightSalmon, 1))
evenCellStyle.BackColor = ControlPaint.LightLight(ControlPaint.Light(Color.LightSalmon, 1))
End If

If e.Column Is selRes Then 'this shows in vtResults checkbox col selRes what the CellEditorChk.GetControlValue set in database
If Not IsDBNull(e.Row.Item(selRes.Name)) AndAlso e.Row.Item(selRes.Name) Then 'e.Row.Item(selRes.Name) = current value in datarow's selRes cell
e.CellData.Value = True
Else
e.CellData.Value = False
End If
End If

If e.Column Is highLow Then
If Not IsDBNull(e.CellData.Value) AndAlso Field2Str(e.CellData.Value).Length > 0 Then
oddCellStyle.ForeColor = Color.Red
evenCellStyle.ForeColor = Color.Red
End If
End If
End Select

e.CellData.OddStyle = oddCellStyle 'apply new style to this cell; if you just change back color (like e.CellData.OddStyle.BackColor = Color.???) every cell changes to this color because this is a default cell style
e.CellData.EvenStyle = evenCellStyle 'see comment for oddCellStyle

End If
End Sub

Private Sub vtResults_SetCellValue(sender As Object, e As Infralution.Controls.VirtualTree.SetCellValueEventArgs) Handles vtResults.SetCellValue
Dim vt As Infralution.Controls.VirtualTree.VirtualTree = DirectCast(sender, Infralution.Controls.VirtualTree.VirtualTree)
Dim binding As RowBinding = vt.GetRowBinding(e.Row)
binding.SetCellValue(e.Row, e.Column, e.OldValue, e.NewValue) 'TODO: Testing dissapearance of checkboxes
End Sub


DBNull values read from the database for columns selReq & selRes are filtered by using Sql COALESCE(field,0)

Cannot replicate the error when clicking on child table 'Res' checkbox. When clicking on parent table's checkbox & scrolling to click next row the VTree becomes jittery requiring extra clicks to select the row & then finally check/uncheck it.

Some (very few) checkboxes fail to paint at all & are un-clickable. Hovering the mouse over shows True.
I'm using W7, VTree 4.7, VS2010. Thanks for any help.

As I have a deadline I've kept trying stuff. I added
e.CellData.Editor.EnsureVisibleOnFocus = True in GetCellData event.
After a while of selecting/unselecting & scrolling rows the first row checkbox disengaged from the row. The checkbox stayed fixed in the upper left corner while I scrolled the 500+ rows. I couldn't tell if the checkbox belonged to the first row but if I clicked with the 1st row hidden the VTree would scroll to the 1st row & the checkbox was functional.
_________________
Medical billing & records developer.
Back to top
View user's profile Send private message Visit poster's website AIM Address
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Thu Aug 22, 2013 8:08 am    Post subject: Reply with quote

"Cross Thread" exceptions typically occur if you have a background thread that is performing some operation on a user control (Windows Forms controls are single threaded and you must use Invoke to access methods from other threads). Virtual Tree itself does not use any background threads and so it is unlikely to be causing the problem directly. It is more likely that the background thread is one from your application that some how is calling a control method/property. If you are using databinding events to automatically update virtual tree when data changes then the ListChangedEvent can sometimes occur on another thread - however VirtualTree detects this and will call Invoke to marshall the call to the main user interface thread - so this should not be the problem.

If you can provide a call stack trace at the point that the "Cross-Thread exception" occurs that may help pinpoint the problem. These sort of problems can be very difficult to track down with actually debugging so if you can put together a sample project that demonstrates the problem that would help greatly. You can email a zipped copy of the sample project to support@infralution.com
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
medbiller



Joined: 20 Dec 2007
Posts: 39
Location: Arroyo, PR

PostPosted: Thu Aug 22, 2013 11:52 am    Post subject: CheckBox dissapearing or disengaging from VTree Row Reply with quote

How about the 2nd issue explained in my post (please re-read):
"Cannot replicate the error when clicking on child table 'Res' checkbox. When clicking on parent table's checkbox & scrolling to click next row the VTree becomes jittery requiring extra clicks to select the row & then finally check/uncheck it.

Some (very few) checkboxes fail to paint at all & are un-clickable. Hovering the mouse over shows True.
I'm using W7, VTree 4.7, VS2010. Thanks for any help.

As I have a deadline I've kept trying stuff. I added
e.CellData.Editor.EnsureVisibleOnFocus = True in GetCellData event.
After a while of selecting/unselecting & scrolling rows the first row checkbox disengaged from the row. The checkbox stayed fixed in the upper left corner while I scrolled the 500+ rows. I couldn't tell if the checkbox belonged to the first row but if I clicked with the 1st row hidden the VTree would scroll to the 1st row & the checkbox was functional."
_________________
Medical billing & records developer.
Back to top
View user's profile Send private message Visit poster's website AIM Address
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Thu Aug 22, 2013 10:48 pm    Post subject: Reply with quote

I've had a play with the File Browser sample project which uses checkboxes at multiple levels in the tree and I can't replicate the behaviour you are seeing. Its very hard to resolve issues like this without code to examine and debug. If you can replicate the issue in a sample project that would really help.

It would also make reading code that you post to the forums much easier if you could format it (by selecting the block and clicking Code to put it in a code block that preserves formatting).

One thing I did note in your code that is clearly wrong is that you should not change the Editor properties like EnsureVisibleOnFocus in the GetCellData event. This property should be set at design time - or once before the control is bound to data.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
medbiller



Joined: 20 Dec 2007
Posts: 39
Location: Arroyo, PR

PostPosted: Fri Aug 23, 2013 5:01 am    Post subject: CheckBox control disangaged & displayed outside row. Reply with quote

Thanks for your response. Your File Browser example doesn't scale to a DataTable or DataSet with 1000 rows or more. How many drives a computer can have? Probably from A: to Z: at the most. Can't compare that simplistic example with real life. What I'm telling you is that in a STRESS test we performed (before releasing to production) checkboxes disappear at times & even disengage from their row & show up on screen elsewhere. By elsewhere I mean not on the row they are supposed to be. When you click the disengaged checkbox it brings the corresponding row into focus. By stress test I mean having 1000 rows, scrolling up & down quickly, checking/unchecking and repeating this routine for over a minute. The fact that it initially works for a while logically says that the virtual tree eventually breaks. Please don't dismiss me so fast. I'm trying to improve your product.
_________________
Medical billing & records developer.
Back to top
View user's profile Send private message Visit poster's website AIM Address
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Fri Aug 23, 2013 7:48 am    Post subject: Reply with quote

I'm not dismissing you. I'm just pointing out that unless you can replicate the issue in an application that we can actually see the code for and play with it is very hard for us to determine whether the problem is due to a bug or the way you happen to be using the code and to get to the bottom of the issue.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Mon Aug 26, 2013 8:33 am    Post subject: Reply with quote

Based on your thought that the issue may be related to the number of rows I modified the sample Database Browser sample project to add a checkbox column and bound it for both levels of the data displayed. I then generated over 20000 rows of data. I tried checking/unchecking rows and scrolling rapidly for an extended period - but was unable to produce any suspect behaviour.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
medbiller



Joined: 20 Dec 2007
Posts: 39
Location: Arroyo, PR

PostPosted: Tue Aug 27, 2013 12:49 pm    Post subject: CheckBox editor problems - Improving. Some CkBoxes disappear Reply with quote

Thanks so much for your concern & time. Here is what we've done so far.
1. Leave the CellEditor hosting a Checkbox bound to the corresponding column BUT without any event handlers implemented. This is where we got confused since we've used InitializeControl, SetControlValue, GetControlValue in the past successfully with TextBox & ComboBox editors but not with Checkbox. DisplayMode is set Always in design time. EnsureVisibleOnFocus=False (design time).
2. Deleted all code handling the checkbox column (selRes) from the vTree.GetCellData event.
3. Implemented the following code in the SetCellValue event:

Code:

        If e.Column Is selRes Then
            If IsDate(e.Row.ParentRow.Item(TstReqResPrintDt.Name)) Then 'don't allow change if the report was printed
                binding.SetCellValue(e.Row, e.Column, e.OldValue, e.OldValue) 'TODO: Testing dissapearance of checkboxes
            Else
                binding.SetCellValue(e.Row, e.Column, e.OldValue, e.NewValue)

                Dim newVal As Integer = 0
                If e.NewValue Then
                    newVal = 1
                End If

                Dim Sql As New StringBuilder                    'permeate value to database
                Sql.Append("UPDATE TstResults SET TstResSelect = " & newVal.ToString & " WHERE TstResID = " _
                           & vtResults.SelectedRow.Item(TstResID.Name).ToString & ";")
                Using cmd As New SqlCommand(Sql.ToString, goConnMB)
                    cmd.ExecuteNonQuery()
                End Using
            End If


Basically we don't want to toggle the checkbox if a parent row column TstReqResPrintDt contains a date. Otherwise we do toggle the checkbox & save it to Sql table on the fly.

4. Insured the Checkbox column's equivalent's Sql field has no nulls. We were using Coalesce(field,0) AS selRes but this makes the value of column selRes numeric (0 or 1) instead of True/False. So eliminated Coalesce().

5. Status thus far:
a. the jittery behavior when scrolling & clicking directly on the cell of an unselected row appears to have disappeared & the checbox toggles.
b. sometimes you see a Checkbox disappear! The sqare is just not there & the cell cannot be toggled. This requires to exit the form & reload again.

Any advice? Again thanks very much for writing.
_________________
Medical billing & records developer.
Back to top
View user's profile Send private message Visit poster's website AIM Address
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Tue Aug 27, 2013 11:59 pm    Post subject: Reply with quote

It is a good idea to reduce the complexity of how your application is interacting with the checkbox column to try to understand what could be causing the issue. If you remove all handlers and binding to the column and still are seeing issues then you should be able to replicate this in a sample project. I'm happy to send you a copy of the modified sample project I created to test checkboxes with large amounts of data if that would help.

Your SetCellValue event logic is unusual. If you want to prevent a control changing value (ie the check box being set) then you should just set the Cancel flag in the event args ie:

Code:
     If e.Column Is selRes Then
            If IsDate(e.Row.ParentRow.Item(TstReqResPrintDt.Name)) Then 'don't allow change if the report was printed
                 e.Cancel = True
            Else
                binding.SetCellValue(e.Row, e.Column, e.OldValue, e.NewValue)
            ...


Because you aren't notifying VirtualTree that you are cancelling the SetCellValue action it will cause the check control state to get out of sync with your underlying data state.

Rejecting a users attempt to change an active active checkbox state (without displaying any message) is probably not a good idea as means the user interface is behaving in an unexpected way. It would probably be better to disable the checkbox to prevent them being able to change it in this case. You could do this by handling the SetControlValue event for the check control and setting both the Enabled and Check properties accordingly. But it may be worth waiting until you have sorted out the other issues you are having before adding this extra complexity.

Quote:
b. sometimes you see a Checkbox disappear! The sqare is just not there & the cell cannot be toggled. This requires to exit the form & reload again


Under what circumstances does this happen. Can you replicate this in a sample project - that would really help. Or failing that post (or email) an image of your screen.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Infralution Support Forum Index -> Virtual Tree Support All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group