Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

UI Threading

Options
  • 08-07-2011 12:33pm
    #1
    Registered Users Posts: 1,686 ✭✭✭


    Hi,

    How are you all?
    I have been having a bit of problems regarding multi threading.
    Basically how do I cross threads safely because i need to get/set informations to the UI controls.

    Without thread the UI freezes till work is finished while showing "not responding" with threading it still shows "not responding" but the UI is not frozen.

    Rough Work

    Snippit - Button Click - UI Thread
    Dim oThread As Threading.Thread = New Threading.Thread(New Threading.ThreadStart(AddressOf work))
    
            If Not saveFilePath = Nothing Then
                If ComboBox1.Text = Nothing Then
                    MessageBox.Show("Select Your Character", "Reader", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                Else
                    ToolStripStatusLabel2.Text = "Loading"
                    Application.DoEvents()
                    oThread.Start()
    


    Snippit - Work Thread
    Public Sub work() 'As Object
            If ComboBox1.InvokeRequired Then
                ComboBox1.Invoke(New MethodInvoker(AddressOf work))
            Else
                TextBox1.Text = ReadModule.readCLevel(saveFilePath, ComboBox1.Text).ToString
                Dim tempCoin As Integer = ReadModule.readCCoins(saveFilePath, ComboBox1.Text)
    
                If tempCoin > 9999999 = True Then
                    NumericUpDown2.Value = NumericUpDown2.Maximum
                Else
                    NumericUpDown2.Value = tempCoin
                End If
                TextBox2.Text = ComboBox1.Text
                TextBox3.Text = ComboBox1.Text
                TextBox4.Text = ComboBox1.Text
    
                If Integer.Parse(TextBox1.Text) = 30 Then
                    NumericUpDown1.Value = 30
                Else
                    NumericUpDown1.Value = Integer.Parse(TextBox1.Text) + 1
                End If
                ' Return Nothing
            End If
    
        End Sub
    


Comments

  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Your worker thread shouldn't referring to your UI controls directly, I'm not sure but I guess that's why you're still getting the 'Not Responding' message.

    Your UI thread should contain a method for updating your UI controls, it takes in whatever values needed as parameters. Your UI thread can then pass it as a delegate to your worker thread which calls it passing the values as needed.


  • Registered Users Posts: 981 ✭✭✭fasty


    Since you call work on another thread, then check if an invoke is required to update the combo box, which is always is, you then invoke do work on the UI main thread anyway, defeating the purpose of threading!

    It should be something like this:

    Pass all params needed to background thread, so values from UI etc
    Do work
    Check if invoke required on controls you want to update
    If so, use invoke pattern to update controls
    return

    stevenmu wrote: »
    Your worker thread shouldn't referring to your UI controls directly, I'm not sure but I guess that's why you're still getting the 'Not Responding' message.

    Your UI thread should contain a method for updating your UI controls, it takes in whatever values needed as parameters. Your UI thread can then pass it as a delegate to your worker thread which calls it passing the values as needed.

    If you try to update UI components in .Net without using the invoke pattern, it'll throw an InvalidOperation exception rather than cause the app to become unresponsive.


Advertisement