Cross Threading in General

Can someone answer me this question...

I have programmed in VBA for years. It seems to handle directly accessing simple form objects without any worries about threads or cross-threading or whatever... It seems ( also ) to me, that this threading issue could be internally managed by the Control Class to some degree or another. Is there a good reason for this new Thread Issue that I am unaware of




Answer this question

Cross Threading in General

  • x3tje

    Now that you're looking into multithreading, I'd suggest you keep trying that, but if you want an 'ugly/easy' way to cross threads, then take a look at the pseudocode in my last reply of my first question on this forum:
    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1109256&SiteID=1

    If you use a Timer in the sourceobject(xxLabel in the example) and make it synchronize with the targetobject(xxPanel in the example - the timer in the xxPanel is irrelevant), then you can access the targetobject in a threadsafe manner during the Timer's Tick/Elapsed event. And just to make sure that access to the target's thread is available, use an invokerequired/isHandleCreated condition like the pseudocode example in my last reply in that topic. I once read that a Timer's performancecost is extremely low, so don't worry about that aspect.

    Note that you're better off learning multithreading, though, but this is a little trick that can do the job if you're working on a small program that isn't affected by the possible "wait-for-the-sync" that occurs between the two objects.


  • paruchuri

    You shouldn't need to. Concentrate on making the UpdateAttchmentList method thread safe using the techniques from the other thread and then anything calling it won't have to worry about it.

    I'd suggest its best practice to encapsulate the thread issues of the control and only worry about them in the procedure that interacts with it.

    What problem are you getting now ... maybe I can help

    Richard


  • Denny B

    The WHOLE code Ok... you asked for it...

    [code=VB]

    Imports EngGraphicsTool.clsAttachmentGrid

    Imports EngGraphicsTool.clsAttachmentPackage

    Imports EngGraphicsTool.clsAttachmentPage

    Imports EngGraphicsTool.clsReferenceEvents

    Imports EngGraphicsTool.mdlConfiguration

    Imports System.Threading

    Public Class frmAttachmentTool

    Public WithEvents mainGrid As clsAttachmentGrid

    Public WithEvents RefEvtMgr As clsReferenceEvents

    Public txtBoxCollection As Collection

    Public chkBoxCollection As Collection

    Public cmdBtnCollection As Collection

    Private Delegate Sub fn1(Of T)(ByVal arg As T)

    Private Delegate Sub fn2(Of T, T1)(ByVal arg1 As T, ByVal arg2 As T1)

    Private Sub frmAttachmentTool_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    MSConn()

    Try

    Dim txtBoxCol As New Collection, chkBoxCol As New Collection, cmdBtnCol As New Collection

    Dim newRefEventMgr As New clsReferenceEvents

    With txtBoxCol

    .Add(Me.txt_MainPage, "Main")

    .Add(Me.txt_NorthPage, "North")

    .Add(Me.txt_SouthPage, "South")

    .Add(Me.txt_EastPage, "East")

    .Add(Me.txt_WestPage, "West")

    .Add(Me.txt_NorthEastPage, "NorthEast")

    .Add(Me.txt_NorthWestPage, "NorthWest")

    .Add(Me.txt_SouthEastPage, "SouthEast")

    .Add(Me.txt_SouthWestPage, "SouthWest")

    End With

    With chkBoxCol

    .Add(Me.chk_Base, "Base")

    .Add(Me.chk_Addressing, "Addressing")

    .Add(Me.chk_Contours, "Contours")

    .Add(Me.chk_CreekPond, "CreekPond")

    .Add(Me.chk_OldCreekPond, "OldCreekPond")

    .Add(Me.chk_Orthophoto, "Orthophoto")

    .Add(Me.chk_Paving, "Paving")

    .Add(Me.chk_Storm, "Storm")

    .Add(Me.chk_Waste, "Waste")

    .Add(Me.chk_Water, "Water")

    End With

    With cmdBtnCol

    .Add(Me.btn_BaseLoader, "Base")

    .Add(Me.btn_AddressingLoader, "Addressing")

    .Add(Me.btn_ContoursLoader, "Contours")

    .Add(Me.btn_CreekPondLoader, "CreekPond")

    .Add(Me.btn_OldCreekPondLoader, "OldCreekPond")

    .Add(Me.btn_OrthophotoLoader, "Orthophoto")

    .Add(Me.btn_PavingLoader, "Paving")

    .Add(Me.btn_Stormloader, "Storm")

    .Add(Me.btn_WasteLoader, "Waste")

    .Add(Me.btn_WaterLoader, "Water")

    End With

    txtBoxCollection = txtBoxCol

    chkBoxCollection = chkBoxCol

    cmdBtnCollection = cmdBtnCol

    mainGrid = New clsAttachmentGrid

    If IsNumeric(Me.txt_MainPage.Text) Then

    mainGrid.SetGrid(CInt(txt_MainPage.Text))

    Else

    mainGrid.SetGrid(CInt(0))

    End If

    RefEvtMgr = newRefEventMgr

    MyMSApp.AddAttachmentEventsHandler(RefEvtMgr)

    MyMSApp.RasterManager.AddRasterEventsHandler(RefEvtMgr)

    UpdateAttachmentList()

    Catch ex As Exception

    End Try

    End Sub

    Private Sub mainGrid_GridLoaded() Handles mainGrid.GridLoaded

    Dim tmpTextBox As TextBox

    For Each tmpTextBox In txtBoxCollection

    tmpTextBox.Text = mainGrid.Package(tmpTextBox.Tag).PageNumber

    Next

    End Sub

    Private Sub CheckAllControls()

    ActivateAllCheckBoxes(mainGrid.ActivePackage)

    ActivateAllButtons(mainGrid.ActivePackage)

    End Sub

    Public Function ActivateTextBox(ByVal TargetTextBox As TextBox, ByVal Activate As Boolean) As TextBox

    Try

    Select Case Activate

    Case True

    Me.DeActivateAllTextBoxes()

    TargetTextBox.BackColor = Color.Yellow

    mainGrid.ActivatePackage_byName(CStr(TargetTextBox.Tag))

    CheckAllControls()

    Case False

    TargetTextBox.BackColor = Color.White

    DeActivateAllCheckBoxes()

    DeActivateAllButtons()

    End Select

    Return TargetTextBox

    Catch ex As Exception

    Dim newTextBox As New TextBox

    Return newTextBox

    End Try

    End Function

    Public Sub DeActivateAllTextBoxes()

    Dim tmpTxtBox As TextBox

    For Each tmpTxtBox In txtBoxCollection

    ActivateTextBox(tmpTxtBox, False)

    Next

    End Sub

    Public Function ActivateCheckBox(ByVal TargetAttachmentPage As clsAttachmentPage) As CheckBox

    Try

    Dim tmpCheckBox As CheckBox

    tmpCheckBox = Me.chkBoxCollection.Item(CStr(TargetAttachmentPage.Name))

    ' tmpCheckBox = Me.Controls.Item(CStr("chk_") + CStr(TargetAttachmentPage.Name))

    With tmpCheckBox

    .Checked = CBool(TargetAttachmentPage.CheckAttachStatus)

    End With

    Return tmpCheckBox

    Catch ex As Exception

    Dim newCheckBox As New CheckBox

    Return newCheckBox

    End Try

    End Function

    Public Sub ActivateAllCheckBoxes(ByVal TargetAttachmentPackage As clsAttachmentPackage)

    With TargetAttachmentPackage

    ActivateCheckBox(.Base())

    ActivateCheckBox(.Addressing())

    ActivateCheckBox(.Contours())

    ActivateCheckBox(.CreekPond())

    ActivateCheckBox(.OldCreekPond())

    ActivateCheckBox(.Orthophoto())

    ActivateCheckBox(.Paving())

    ActivateCheckBox(.Storm())

    ActivateCheckBox(.Waste())

    ActivateCheckBox(.Water())

    End With

    End Sub

    Public Sub DeActivateAllCheckBoxes()

    Dim tmpChkBox As CheckBox

    For Each tmpChkBox In chkBoxCollection

    With tmpChkBox

    .Checked = False

    End With

    Next

    End Sub

    Public Function ActivatePageButton(ByVal TargetAttachmentPage As clsAttachmentPage) As Button

    Try

    Dim tmpCmdBtn As Button

    tmpCmdBtn = Me.cmdBtnCollection.Item(CStr(TargetAttachmentPage.Name))

    ' tmpCmdBtn = Me.Controls.Item("btn_" + TargetAttachmentPage.Name + "Loader")

    With tmpCmdBtn

    Select Case TargetAttachmentPage.Exists

    Case True

    .Enabled = True

    Select Case TargetAttachmentPage.CheckAttachStatus

    Case True

    .Text = "Unload " + TargetAttachmentPage.Name

    .Tag = "LOADED"

    Case False

    .Text = "Load " + TargetAttachmentPage.Name

    .Tag = "UNLOADED"

    End Select

    Case False

    .Enabled = False

    .Text = "N/A"

    .Tag = "EMPTY"

    End Select

    End With

    Return tmpCmdBtn

    Catch ex As Exception

    Dim newCmdBtn As New Button

    Return newCmdBtn

    End Try

    End Function

    Public Sub ActivateAllButtons(ByVal TargetAttachmentPackage As clsAttachmentPackage)

    With TargetAttachmentPackage

    ActivatePageButton(.Base())

    ActivatePageButton(.Addressing())

    ActivatePageButton(.Contours())

    ActivatePageButton(.CreekPond())

    ActivatePageButton(.OldCreekPond())

    ActivatePageButton(.Orthophoto())

    ActivatePageButton(.Paving())

    ActivatePageButton(.Storm())

    ActivatePageButton(.Waste())

    ActivatePageButton(.Water())

    End With

    End Sub

    Public Sub DeActivateAllButtons()

    Dim tmpCmdBtn As Button

    For Each tmpCmdBtn In cmdBtnCollection

    With tmpCmdBtn

    .Text = "Loader"

    .Enabled = False

    End With

    Next

    End Sub

    Private Sub txt_MainPage_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_MainPage.TextChanged

    Dim tmpNewPageNumber As Integer

    Select Case Me.txt_MainPage.Text

    Case "", "X", "0"

    tmpNewPageNumber = 0

    Case Else

    tmpNewPageNumber = CInt(Me.txt_MainPage.Text)

    End Select

    mainGrid.SetGrid(tmpNewPageNumber)

    ActivateAllCheckBoxes(mainGrid.Package(CStr(Me.txt_MainPage.Tag)))

    ActivateAllButtons(mainGrid.Package(CStr(Me.txt_MainPage.Tag)))

    End Sub

    Private Sub txt_MainPage_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_MainPage.Enter

    ActivateTextBox(Me.txt_MainPage, True)

    End Sub

    Private Sub txt_NorthPage_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_NorthPage.Enter

    ActivateTextBox(Me.txt_NorthPage, True)

    End Sub

    Private Sub txt_SouthPage_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_SouthPage.Enter

    ActivateTextBox(Me.txt_SouthPage, True)

    End Sub

    Private Sub txt_EastPage_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_EastPage.Enter

    ActivateTextBox(Me.txt_EastPage, True)

    End Sub

    Private Sub txt_WestPage_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_WestPage.Enter

    ActivateTextBox(Me.txt_WestPage, True)

    End Sub

    Private Sub txt_NorthEastPage_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_NorthEastPage.Enter

    ActivateTextBox(Me.txt_NorthEastPage, True)

    End Sub

    Private Sub txt_NorthWestPage_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_NorthWestPage.Enter

    ActivateTextBox(Me.txt_NorthWestPage, True)

    End Sub

    Private Sub txt_SouthEastPage_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_SouthEastPage.Enter

    ActivateTextBox(Me.txt_SouthEastPage, True)

    End Sub

    Private Sub txt_SouthWestPage_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_SouthWestPage.Enter

    ActivateTextBox(Me.txt_SouthWestPage, True)

    End Sub

    Private Sub btn_BaseLoader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_BaseLoader.Click

    Select Case Me.btn_BaseLoader.Tag

    Case "LOADED"

    mainGrid.ActivePackage.Base.DetachFile()

    Case "UNLOADED"

    mainGrid.ActivePackage.Base.AttachFile()

    Case "EMPTY"

    MsgBox("This button is temporarily out of order.", vbOKOnly, "Out Of Order")

    End Select

    CheckAllControls()

    End Sub

    Private Sub btn_CreekPondLoader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_CreekPondLoader.Click

    Select Case Me.btn_CreekPondLoader.Tag

    Case "LOADED"

    mainGrid.ActivePackage.CreekPond.DetachFile()

    Case "UNLOADED"

    mainGrid.ActivePackage.CreekPond.AttachFile()

    Case "EMPTY"

    MsgBox("This button is temporarily out of order.", vbOKOnly, "Out Of Order")

    End Select

    CheckAllControls()

    End Sub

    Private Sub btn_OldCreekPondLoader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_OldCreekPondLoader.Click

    Select Case Me.btn_OldCreekPondLoader.Tag

    Case "LOADED"

    mainGrid.ActivePackage.OldCreekPond.DetachFile()

    Case "UNLOADED"

    mainGrid.ActivePackage.OldCreekPond.AttachFile()

    Case "EMPTY"

    MsgBox("This button is temporarily out of order.", vbOKOnly, "Out Of Order")

    End Select

    CheckAllControls()

    End Sub

    Private Sub btn_ContoursLoader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ContoursLoader.Click

    Select Case Me.btn_ContoursLoader.Tag

    Case "LOADED"

    mainGrid.ActivePackage.Contours.DetachFile()

    Case "UNLOADED"

    mainGrid.ActivePackage.Contours.AttachFile()

    Case "EMPTY"

    MsgBox("This button is temporarily out of order.", vbOKOnly, "Out Of Order")

    End Select

    CheckAllControls()

    End Sub

    Private Sub btn_WaterLoader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_WaterLoader.Click

    Select Case Me.btn_WaterLoader.Tag

    Case "LOADED"

    mainGrid.ActivePackage.Water.DetachFile()

    Case "UNLOADED"

    mainGrid.ActivePackage.Water.AttachFile()

    Case "EMPTY"

    MsgBox("This button is temporarily out of order.", vbOKOnly, "Out Of Order")

    End Select

    CheckAllControls()

    End Sub

    Private Sub btn_WasteLoader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_WasteLoader.Click

    Select Case Me.btn_WasteLoader.Tag

    Case "LOADED"

    mainGrid.ActivePackage.Waste.DetachFile()

    Case "UNLOADED"

    mainGrid.ActivePackage.Waste.AttachFile()

    Case "EMPTY"

    MsgBox("This button is temporarily out of order.", vbOKOnly, "Out Of Order")

    End Select

    CheckAllControls()

    End Sub

    Private Sub btn_StormLoader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Stormloader.Click

    Select Case Me.btn_Stormloader.Tag

    Case "LOADED"

    mainGrid.ActivePackage.Storm.DetachFile()

    Case "UNLOADED"

    mainGrid.ActivePackage.Storm.AttachFile()

    Case "EMPTY"

    MsgBox("This button is temporarily out of order.", vbOKOnly, "Out Of Order")

    End Select

    CheckAllControls()

    End Sub

    Private Sub btn_PavingLoader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_PavingLoader.Click

    Select Case Me.btn_PavingLoader.Tag

    Case "LOADED"

    mainGrid.ActivePackage.Paving.DetachFile()

    Case "UNLOADED"

    mainGrid.ActivePackage.Paving.AttachFile()

    Case "EMPTY"

    MsgBox("This button is temporarily out of order.", vbOKOnly, "Out Of Order")

    End Select

    CheckAllControls()

    End Sub

    Private Sub btn_OrthophotoLoader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_OrthophotoLoader.Click

    Select Case Me.btn_OrthophotoLoader.Tag

    Case "LOADED"

    mainGrid.ActivePackage.Orthophoto.DetachFile()

    Case "UNLOADED"

    mainGrid.ActivePackage.Orthophoto.AttachFile()

    Case "EMPTY"

    MsgBox("This button is temporarily out of order.", vbOKOnly, "Out Of Order")

    End Select

    CheckAllControls()

    End Sub

    Private Sub btn_AddressingLoader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_AddressingLoader.Click

    Select Case Me.btn_AddressingLoader.Tag

    Case "LOADED"

    mainGrid.ActivePackage.Addressing.DetachFile()

    Case "UNLOADED"

    mainGrid.ActivePackage.Addressing.AttachFile()

    Case "EMPTY"

    MsgBox("This button is temporarily out of order.", vbOKOnly, "Out Of Order")

    End Select

    CheckAllControls()

    End Sub

    Private Sub txt_NorthPage_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_NorthPage.DoubleClick

    With txt_MainPage

    .Focus()

    .Text = txt_NorthPage.Text

    End With

    End Sub

    Private Sub txt_SouthPage_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_SouthPage.DoubleClick

    With txt_MainPage

    .Focus()

    .Text = txt_SouthPage.Text

    End With

    End Sub

    Private Sub txt_EastPage_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_EastPage.DoubleClick

    With txt_MainPage

    .Focus()

    .Text = txt_EastPage.Text

    End With

    End Sub

    Private Sub txt_WestPage_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_WestPage.DoubleClick

    With txt_MainPage

    .Focus()

    .Text = txt_WestPage.Text

    End With

    End Sub

    Private Sub txt_NorthEastPage_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_NorthEastPage.DoubleClick

    With txt_MainPage

    .Focus()

    .Text = txt_NorthEastPage.Text

    End With

    End Sub

    Private Sub txt_NorthWestPage_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_NorthWestPage.DoubleClick

    With txt_MainPage

    .Focus()

    .Text = txt_NorthWestPage.Text

    End With

    End Sub

    Private Sub txt_SouthWestPage_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_SouthWestPage.DoubleClick

    With txt_MainPage

    .Focus()

    .Text = txt_SouthWestPage.Text

    End With

    End Sub

    Private Sub txt_SouthEastPage_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_SouthEastPage.DoubleClick

    With txt_MainPage

    .Focus()

    .Text = txt_SouthEastPage.Text

    End With

    End Sub

    Private Sub btn_AssignActive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_AssignActive.Click

    With txt_MainPage

    .Focus()

    .Text = GetActivePageNumber()

    End With

    End Sub

    Private Sub btn_ClearPage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ClearPage.Click

    ClearAllAttachments()

    With txt_MainPage

    .Focus()

    .Text = GetActivePageNumber()

    End With

    UpdateAllViews()

    End Sub

    Private Sub RefEvtMgr_AttachmentStatus1(ByVal TheAttachment As MicroStationDGN.Attachment, ByVal Attached As Boolean) Handles RefEvtMgr.AttachmentStatus

    UpdateAttachmentList(CStr(TheAttachment.LogicalName), Attached)

    End Sub

    Public Sub UpdateAttachmentList(Optional ByVal TargetLogicalName As String = "BLANK", Optional ByVal LeaveThis As Boolean = True)

    Try

    Dim MyCmb As System.Windows.Forms.ComboBox

    Dim counter As Integer, EntryText As String

    Dim PageType As String, PageNumber As Integer, NumberOfAttachments As Integer

    Dim MyActiveModel As ModelReference, MyAttachments As Attachments, MyAttachment As Attachment

    MyActiveModel = MyAMR

    MyAttachments = MyActiveModel.Attachments

    MyCmb = Me.cmb_References

    clearCombobox(MyCmb)

    NumberOfAttachments = IIf((LeaveThis = True), MyAttachments.Count, MyAttachments.Count - 1)

    For counter = 0 To NumberOfAttachments - 1

    MyAttachment = MyAttachments.Item(counter + 1)

    PageType = LogicalName_to_PageType(MyAttachment.LogicalName)

    PageNumber = LogicalName_to_PageNumber(MyAttachment.LogicalName)

    EntryText = PageType + " Type, Page #: " + CStr(PageNumber)

    ' MyCmb.Items.Add(EntryText)

    addComboBoxItem(MyCmb, EntryText)

    Next counter

    Catch ex As Exception

    Select Case Err.Number

    Case 5 ' Cross-thread operation not valid: Control 'cmb_References' accessed from a thread other than the thread it was created on.

    End Select

    End Try

    End Sub

    Private Shared Sub clearCombobox(ByVal MyComboBox As ComboBox)

    If MyComboBox.InvokeRequired Then

    MyComboBox.Invoke(New fn1(Of ComboBox)(AddressOf clearCombobox), MyComboBox)

    Else

    MyComboBox.Items.Clear()

    End If

    End Sub

    Private Shared Sub addComboBoxItem(ByVal MyComboBox As ComboBox, ByVal ItemEntry As String)

    If MyComboBox.InvokeRequired Then

    MyComboBox.Invoke(New fn2(Of ComboBox, String)(AddressOf addComboBoxItem), MyComboBox, ItemEntry)

    Else

    MyComboBox.Items.Add(ItemEntry)

    End If

    End Sub

    Protected Overrides Sub Finalize()

    MyMSApp.RemoveAttachmentEventsHandler(RefEvtMgr)

    MyMSApp.RasterManager.RemoveRasterEventsHandler(RefEvtMgr)

    MyBase.Finalize()

    End Sub

    End Class

    [/code]

    Again, I am getting the errors whenever the RefEvtMgr event fires off UpdateAttachmentList...



  • Fredrik Melin

    Richard,

    I am so glad I caught you online... I think my original problem, with updating the combobox, is being caused by an internal Sub.

    I'm hoping you can tell me what to do.

    I use a Sub called UpdateAttachmentList to get a list of attached reference files in my working Microstation Application and update that list to my combobox.

    I just noticed that UpdateAttachmentList is called from a Microstation Event...

    [code=vb]

    Private Sub RefEvtMgr_AttachmentStatus1(ByVal TheAttachment As MicroStationDGN.Attachment, ByVal Attached As Boolean) Handles RefEvtMgr.AttachmentStatus

    UpdateAttachmentList(CStr(TheAttachment.LogicalName), Attached)

    End Sub

    [/code]

    So I wonder if there is a way to make sure that UpdateAttachmentList when called from THERE is threaded appropriately



  • Socrates Kapetaneas

    The problem I am getting now is that whenever it hits that ComboBox from a InvokeRequired = True event It just freezes

    My biggest problem with all of this is that I had this WHOLE thing working JUST FINE in VBA...

    However VBA has a Max Controls limit. So as I attempted to make my little tool / app larger I hit a point where it stopped allowing me to add new controls to it... SO, I am converting this thing over to .NET and now I am hitting the Thread Issues.



  • Bealby

    If you make your application multithreaded then you will have to deal with threading issues. If you want it to be single threader, then don't create threads. The application will respond more in-line to what you are used to.

    Threads don't magically create themselves (depends on what components you are using...) - what is it that's causing this cross thread issue



  • ctssoms

    MyComboBox.Invoke(New fn1(Of ComboBox)(AddressOf clearCombobox), MyComboBox)

    That line hangs...

    Sell Ha... My code is a little wasteful, especially with all the new stuff I am learning... chuckle. I just wish I had dealt with threading before. I am 100% self-taught, so every new thing is such a hurdle. Thankfully, I have this board and people like yourself who are willing to help out us newbs.



  • Kamii47

    ... is this code I can sell

    Can you advise which line causes the debugger to hang


  • Hrishikesh

    I did not purposely create a thread... having never worked with them up till now I was happy to continue on my path until I have a chance for some more courses on programming... that said...

    I am building a VB.NET Application to use with Bentley's Microstation v8

    Whenever you attach a reference to an active DGN file, it fires off an event. I want that event to insert the name and id of the reference that was attached.

    Well, THAT event is what is calling my UpdateAttachmentList Sub, and THAT is when I have problems. I also call the UpdateAttachmentList Sub when the Form is first Loaded. That way any references ALREADY attached are added into my ComboBox then as well.

    Like I said. I DO have UpdateAttachmentList called from other locations. BUT it is only hitting problems when called from the

    [code=vb]

    Public WithEvents RefEvtMgr As clsReferenceEvents

    ...

    Private Sub RefEvtMgr_AttachmentStatus1(ByVal TheAttachment As MicroStationDGN.Attachment, ByVal Attached As Boolean) Handles RefEvtMgr.AttachmentStatus

    UpdateAttachmentList(CStr(TheAttachment.LogicalName), Attached)

    End Sub

    [/code]

    SO, again... I am NOT using threads on purpose.



  • shakalama

    Help

  • Hiral

    Hanging:

    Me.cmb_References.Invoke(New MethodInvoker(AddressOf ClearCombobox))

    Help me Mr. Popeil... I'm in trouble. I guess this means I need to research and start learning Threads I am assuming that my 'thread' problem is arising because I am working with the Bentley Microstation. I believe there is serious threading done in it, it is a CADD program. Thanks for your time again...



  • Sang Hoang

    Hi

    I never used VBA but I suspect that it's used the single thread apartment model, the same way that VB6 application did.

    VB.NET opened up the possibility of developing multi-threaded applications in your favourite language.

    With regard to the control classes, these are just simple wrappers for the Win32 equivalent which also use an STA model. The Win32 controls use a message dispatcher that runs on a single thread (the one that was running when it was created). These controls are not therefore inherently thread safe.

    The .NET wrapper gives us an easy way to ensure that communication with the underlying control is on the same thread on which the control was created (assuming we need to worry about such things .. which most folk don't); the Invoke and InvokeRequired methods. Invoke simply ensures the passed delegate is executed on the controls thread thus allowing you to safely interact with it.

    Indeed I would agree that the .net developers could perhaps have solved this problem by invoking internal delegates, but I'd also suggest that there was little reason to saddle controls and control developers with the overhead where it (most of the time) is unneccessary. In short, it probably helps keep things simpler and speedier for most VB developers .. which is after all the ultimate goal of the language.

    Richard


  • Salte1

    He he .. it's like being a real programmer .. once you grasp it, you'll never look back (maybe).

    What do you mean by 'freezing' What is Your whole app or something else

    If you post your current code, I'll have a look.

    Richard


  • Lenmax

     randomblink wrote:

    MyComboBox.Invoke(New fn1(Of ComboBox)(AddressOf clearCombobox), MyComboBox)

    That line hangs...

    Sell Ha... My code is a little wasteful, especially with all the new stuff I am learning... chuckle. I just wish I had dealt with threading before. I am 100% self-taught, so every new thing is such a hurdle. Thankfully, I have this board and people like yourself who are willing to help out us newbs.

     

    Now I've seen how the UpdateWotsit function sits with the other code, I wouldn't pass the combobox as a parameter to your clear and update methods.  Instead change them to ....

    Private Sub ClearCombobox()

    If Me.cmb_References.InvokeRequired Then

    Me.cmb_References.Invoke(New MethodInvoker(AddressOf ClearCombobox))

    Else

    Me.cmb_References.Items.Clear()

    End If

    End Sub

    Private Sub addComboBoxItem(ByVal ItemEntry As String)

    If Me.cmb_References.InvokeRequired Then

    Me.cmb_References.Invoke(New fn1(Of String)(AddressOf addComboBoxItem), ItemEntry)

    Else

    Me.cmb_References.Items.Add(ItemEntry)

    End If

    Let me know how you get on.

    Richard


  • Cross Threading in General