Hi
After reading hundreds of lines of text about threading and keeping everything thread safe I still do not get the following.
I have created on my main thread Items into a UltraListView, now at another time(not at creation) on another worker thread I want to update a subitem and or the text of the main column. So using a keyIndex linked to the MainColumn Item I use Dim intIndex as Integer = UltraListView1.Items.IndexOf(ItemKey) to return the required index if available. If a valid index is returned then I use Dim clmX as UltraListViewItem = UltraListView1.Items(intIndex) to make a reference to the object. When I need to set the Text property I use the Invoke as required on the clmX and pass it to the correct thread.
Now as I do not get a cross thread error when returning the IndexOf and no error when getting the clmX object is this method classed as thread safe?
Should I be using a Invoke to get the IndexOf or Invoking to get a UltraListViewItem for reference in the Dim?
-Paul
Hi,
It's very tough to say since I still don't really know what you are trying to accomplish. Why are you using a background thread? What are you trying to do?
As a general rule, using a background thread to do anything that is even slightly related to the UI is a bad idea and will not work. Using threading is incredibly complex and difficult and most of the time it only makes sense if you are doing some big processing that can be done without affecting the UI, or at least only affecting the UI at widely-spaced intervals.
If the code you have here is running on a background thread, then there's still a problem, because you are still referencing the ListView control. Or at least, it seems like that's what you are doing. It's very hard to tell from just a code snippet with no context.
Hi Mike
Thank you for reply. I think this is why it gets so confusing, its wrong to do but nothing seems to go wrong.
I have substituted the Dim intIndex as Integer to :
Dim intIndex as Integer = GetUltraListViewIndexOf(UltraListView1,ItemKey)
Public Function GetUltraListViewIndexOf(ByVal UltraListView As Infragistics.Win.UltraWinListView.UltraListView, ByVal ItemKey As String) As Integer Dim intIndexOf As Integer = -1 If UltraListView.InvokeRequired Then intIndexOf = CInt(UltraListView.Invoke(New GetUltraListViewIndexOfInvoker(AddressOf GetUltraListViewIndexOf), UltraListView, ItemKey)) Else intIndexOf = UltraListView.Items.IndexOf(ItemKey) End If Return intIndexOf End Function
Dim clmX as UltraListViewItem = GetUltraListViewItem(UltraListView1,ItemIndex)
Public Function GetUltraListViewItem(ByVal UltraListView As Infragistics.Win.UltraWinListView.UltraListView, ByVal ItemIndex As Integer) As UltraListViewItem Dim clmXUltraListViewItem As UltraListViewItem = Nothing If UltraListView.InvokeRequired Then clmXUltraListViewItem = DirectCast(UltraListView.Invoke(New GetUltraListViewItemInvoker(AddressOf GetUltraListViewItem), UltraListView, ItemIndex), UltraListViewItem) Else clmXUltraListViewItem = UltraListView.Items(ItemIndex) End If Return clmXUltraListViewItem End Function
But I can see that you said 'There is no safe way to make that work' do you mean there is no way to do this on a worker thread at all? so even my new code will not work? I do not want to continue changing my code if I'm on the wrong path.
Hi Paul,
No Windows Forms controls are thread-safe. I am including the Infragistics controls as well as the inbox controls. I suppose there might be some other third-party controls out there that are, but if so, I am not aware of them.
It's hard to understand from your description what you are trying to do here. But you cannot refer to the control from another thread. There is no safe way to do that. So if you have a line of code like:
Dim intIndex as Integer = UltraListView1.Items.IndexOf(ItemKey)
And this code exists on the worker thread, that's a problem. There is no safe way to make that work, because you simply cannot reference a control that exists on the UI thread from a background thread safely.
FPSI said:Now as I do not get a cross thread error when returning the IndexOf and no error when getting the clmX object is this method classed as thread safe?
The fact that you are not getting an error is not indicative that this is a safe operation. In fact, one of the hallmarks of threading problems is that code like this often seems to work find and then causes an exception somewhere else in the code that is seemingly unrelated. This is why threading issues are do difficult to debug.