migrating to VS 2005

pasensyoso_manigbas

Well-known member
Joined
May 19, 2006
Messages
64
Programming Experience
Beginner
hi pepz...

currently i migrate my code from VB.Net 2003 to 2005 version.. I have a couple of questio to ask..

1.) Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

2.) Cross-thread operation not valid: Control 'logview' accessed from a thread other than the thread it was created on.

I have just installed the 2005 version early this morning... anyone could explain this.. this are the errors i encountered when i convert my project and try to run it..
 
I've deleted your other thread. As Kulrom says, one question = one thread.

1. You need to apply the STAThreadAttribute to your Main method:
VB.NET:
<STAThread()> Public Sub Main()

2. VS.NET 2003 let you get away with it but you should NEVER access most of a control's members from a worker thread unless you are ABSOLUTELY positive that it will not cause any issues. You should always use a delegate to access the members of a control from a worker thread. Here's an example of setting the Text of a TextBox using a delegate:
VB.NET:
Private Delegate Sub SetTextBoxTextDelegate(ByVal text As String)

Private Sub SetTextBoxText(ByVal text As String)
    If Me.TextBox1.InvokeRequired Then
        'This is a worker thread.
        Me.TextBox1.Invoke(New SetTextBoxTextDelegate(AddressOf SetTextBoxText), text)
    Else
        'This is the UI thread.
        Me.TextBox1.Text = text
    End If
End Sub
Now you just call the SetTextBoxText method from wherever you are without worrying about what thread you're on. The method will detect whether it is in a worker thread and, if so, create the required delegate and marshal the call to the UI thread. Basically the method says "Hey thread, you can't touch the TextBox from here. Here's my friend the delegate. You tell him what you want to do and he'll go over to the UI thread and do it for you." The delegate crosses the thread boundary and then calls the same method again. This time it is on the UI thread so it can safely access the control.
 
doing delegation through text is not that complicated.. but how about in a list view?
 
If it's easy for one then it's easy for the other. The principle is exactly the same. You declare your method with the required arguments and you declare a matching delegate. You test the InvokeRequired property of the control you want to access. In the If block you create an instance of the delegate and pass the very same parameters that the method received to the Invoke call. In the Else block you do whatever it is that you want to do to the control. I'm not going to write it for you because you already have all you need to do it yourself. Don't just look at the code itself. Look at the methodology. You can then generalise that as I have done in this post and then apply that to any specific situation. If some taught you how to divide 6 by 4, you could quite easily extend that to divide very large numbers by 4. This is basically the same thing. You have the methodology, so just apply that to a more complex situation, although it's on just more complex.
 
If reader("card") = breaker(1) And reader("num") = breaker(2) Then
Dim displog AsNew ListViewItem
Dim lstnew AsNew ListViewItem
displog.ImageIndex = 0
displog.Text = breaker(0)
displog.SubItems.Add(breaker(1))
displog.SubItems.Add(breaker(2))
displog.SubItems.Add(_main.LogOpener.FileName.Substring(17, 1).ToCharArray)

_main.logview.Items.Add(displog) ---> this part here needs delegation

lstnew.ImageIndex = 0
lstnew.Text = reader(
"card")
lstnew.SubItems.Add(reader(
"num"))
lstnew.SubItems.Add(reader(
"datec"))
lstnew.SubItems.Add(reader(
"unit"))

_main.lst.Items.Add(lstnew)---> this part here needs delegation
EndIf

can u show me some code on how to do it in a listview..
i made some of my understanding but couldn't get it right..

Pls.. i need to configure this...
 
Look at the code I posted. It assigns a String value to the Text property of a TextBox. You want to add a ListViewItem to a ListView. Everywhere that I have the TextBox in my code you need to put the ListView and everywhere that I have the String object you need to put the ListViewItem. That's all. What you want is no more compilcated than what I've already shown you.
 
geez... thanx... i totally newbie to this.. thanx a lot..
 
As you may be able to tell I'm not a big fan of spoon-feeding. If I gave you this method to set the Text of a TextBox:
VB.NET:
Private Sub SetTextBoxText(ByVal text As String)
    Me.TextBox1.Text = text
End Sub
and I told you to substitute a ListView for the TextBox and a ListViewItem for the String I'm sure that you would have no trouble doing so and coming up with:
VB.NET:
Private Sub AddListViewItem(ByVal item As ListViewItem)
    Me.ListView1.Items.Add(item)
End Sub
This situation is no different, other than it requires a couple more substituions.
 
Ow... 1 more thing...
lets say i already have the delegation.. how am i able to call it to the function ive written above...?
 
pasensyoso_manigbas said:
Ow... 1 more thing...
lets say i already have the delegation.. how am i able to call it to the function ive written above...?
From post #3:
jmcilhinney said:
Now you just call the SetTextBoxText method from wherever you are without worrying about what thread you're on. The method will detect whether it is in a worker thread and, if so, create the required delegate and marshal the call to the UI thread.
 
Thank you so much... now its working fine.. Thank you so much for the help..
Now i can go on with my coding... Thanks again..!!!

Til next tym...
 
And do you feel better for having worked out a fair chunk of it yourself rather than having it handed to you? Also, you exercised and improved your problem solving skills, so working out other things for yourself in future will now be easier. We're here to help, but help doesn't always come in the form you expect, or so Confusion (sic) say. :)
 
Back
Top