Click here to Skip to main content
15,887,416 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Capture a monochrome image of panel control Pin
Richard MacCutchan3-May-14 3:50
mveRichard MacCutchan3-May-14 3:50 
GeneralRe: Capture a monochrome image of panel control Pin
gwittlock3-May-14 5:09
gwittlock3-May-14 5:09 
GeneralRe: Capture a monochrome image of panel control Pin
Eddy Vluggen3-May-14 7:41
professionalEddy Vluggen3-May-14 7:41 
GeneralRe: Capture a monochrome image of panel control Pin
gwittlock3-May-14 7:53
gwittlock3-May-14 7:53 
Questionsimple multi thread app behaving weirdly Pin
Tony Snowden1-May-14 23:55
Tony Snowden1-May-14 23:55 
AnswerRe: simple multi thread app behaving weirdly Pin
Dave Kreskowiak2-May-14 5:31
mveDave Kreskowiak2-May-14 5:31 
GeneralRe: simple multi thread app behaving weirdly Pin
Tony Snowden5-May-14 21:42
Tony Snowden5-May-14 21:42 
GeneralRe: simple multi thread app behaving weirdly Pin
Dave Kreskowiak6-May-14 4:01
mveDave Kreskowiak6-May-14 4:01 
OK, it doesn't work the way you think because you are essentially blocking the UI thread You are having it do thousands of updates to the UI and those updates are competing with the message pump to get the window and controls repainted.

In this example, you may as well not even use threading. The end result is the same. You're not really removing "work" from the UI thread. The only "work" you have on the background threads is incrementing a couple of counters and if statements. The bulk of the "work" is still upating the UI and only the UI thread can do that.

Oh, and you don't need to Invoke consecutive statements and the Refreshes are unnecessary. By putting the Refreshes in, you're actually doubling the number of WM_PAINT messages Windows is sending your application.

Your code should look more like this:
Imports System.Threading

Public Class Form1

    Private Const MAXCOUNT As Integer = 10000
    Private i1 As Integer
    Private i2 As Integer

    Private Sub count1()
        Do While i1 < MAXCOUNT
            i1 += 1
            UpdateLabel(i1.ToString, Label1)
        Loop
    End Sub

    Private Sub count2()
        Do While i2 < MAXCOUNT
            i2 += 1
            UpdateLabel(i2.ToString, Label2)
        Loop
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        i1 = 0
        Dim t As New Thread(AddressOf count1)
        t.Start()
    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        i2 = 0
        Dim t2 As New Thread(AddressOf count2)
        t2.Start()
    End Sub

    Private Sub UpdateLabel(message As String, c As Label)
        If Me.InvokeRequired Then
            Me.Invoke(Sub()
                          c.Text = message
                      End Sub)
        Else
            c.Text = message
        End If

        Thread.Sleep(1)
    End Sub

End Class

But, wait a minute. What's with the Thread.Sleep()? By sleeping the background threads for a tiny bit, you're preventing them from hogging the CPU and giving the UI thread a chance to process the messages in the message pump. These messages are various UI things like WM_PAINT, WM_MOVE and WM_MOUSEMOVE messages that Windows sends to tell your application to paint various windows and controls, that the window is moving, and the mouse is moving and over a control.

Like I said, if you flood the UI thread with Invokes like you did, the UI will not be able to process these messages and update the window.

GeneralRe: simple multi thread app behaving weirdly Pin
Tony Snowden6-May-14 4:31
Tony Snowden6-May-14 4:31 
GeneralRe: simple multi thread app behaving weirdly Pin
Dave Kreskowiak6-May-14 4:52
mveDave Kreskowiak6-May-14 4:52 
GeneralRe: simple multi thread app behaving weirdly Pin
Tony Snowden6-May-14 22:05
Tony Snowden6-May-14 22:05 
GeneralRe: simple multi thread app behaving weirdly Pin
Tony Snowden7-May-14 4:31
Tony Snowden7-May-14 4:31 
GeneralRe: simple multi thread app behaving weirdly Pin
Dave Kreskowiak7-May-14 6:07
mveDave Kreskowiak7-May-14 6:07 
GeneralRe: simple multi thread app behaving weirdly Pin
Tony Snowden7-May-14 21:37
Tony Snowden7-May-14 21:37 
QuestionCombobox value not showed in the datagridview Pin
D.Manivelan1-May-14 23:33
D.Manivelan1-May-14 23:33 
SuggestionRe: Combobox value not showed in the datagridview Pin
Maciej Los2-May-14 8:55
mveMaciej Los2-May-14 8:55 
QuestionSubject message not passing to outlook window from VB6.0 Pin
kk20141-May-14 3:53
kk20141-May-14 3:53 
AnswerRe: Subject message not passing to outlook window from VB6.0 Pin
Dave Kreskowiak1-May-14 5:23
mveDave Kreskowiak1-May-14 5:23 
QuestionNeed help on "Competition Scheduling" Algorithm Pin
N.Wang30-Apr-14 16:43
professionalN.Wang30-Apr-14 16:43 
QuestionTwitter REST API V1.1 Pin
Pete_12328-Apr-14 8:10
Pete_12328-Apr-14 8:10 
AnswerRe: Twitter REST API V1.1 Pin
Richard Deeming28-Apr-14 8:27
mveRichard Deeming28-Apr-14 8:27 
GeneralRe: Twitter REST API V1.1 Pin
Pete_12328-Apr-14 9:20
Pete_12328-Apr-14 9:20 
AnswerRe: Twitter REST API V1.1 Pin
HenryHugo28-Apr-14 21:38
professionalHenryHugo28-Apr-14 21:38 
QuestionVB.Net - Best practice to store paths as relative path Pin
Bart Van Eyndhoven27-Apr-14 23:42
Bart Van Eyndhoven27-Apr-14 23:42 
AnswerRe: VB.Net - Best practice to store paths as relative path Pin
Richard Deeming28-Apr-14 2:07
mveRichard Deeming28-Apr-14 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.