Click here to Skip to main content
15,920,438 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: copying cells to another worksheet in next available cell per column Pin
David Rubin5-May-14 9:22
David Rubin5-May-14 9:22 
AnswerRe: copying cells to another worksheet in next available cell per column Pin
ZurdoDev5-May-14 9:29
professionalZurdoDev5-May-14 9:29 
GeneralRe: copying cells to another worksheet in next available cell per column Pin
David Rubin5-May-14 9:40
David Rubin5-May-14 9:40 
AnswerRe: copying cells to another worksheet in next available cell per column Pin
ZurdoDev5-May-14 9:43
professionalZurdoDev5-May-14 9:43 
GeneralRe: copying cells to another worksheet in next available cell per column Pin
David Rubin5-May-14 9:48
David Rubin5-May-14 9:48 
GeneralRe: copying cells to another worksheet in next available cell per column Pin
ZurdoDev5-May-14 9:49
professionalZurdoDev5-May-14 9:49 
GeneralRe: copying cells to another worksheet in next available cell per column Pin
David Rubin5-May-14 12:27
David Rubin5-May-14 12:27 
AnswerRe: copying cells to another worksheet in next available cell per column Pin
ZurdoDev5-May-14 12:58
professionalZurdoDev5-May-14 12:58 
QuestionMicrosoft.Visualbasic Pin
SPSandy5-May-14 4:57
SPSandy5-May-14 4:57 
AnswerRe: Microsoft.Visualbasic Pin
Dave Kreskowiak5-May-14 6:20
mveDave Kreskowiak5-May-14 6:20 
GeneralRe: Microsoft.Visualbasic Pin
SPSandy5-May-14 9:55
SPSandy5-May-14 9:55 
QuestionE-mail Notification Pin
waner michaud4-May-14 13:23
waner michaud4-May-14 13:23 
AnswerRe: E-mail Notification Pin
Dave Kreskowiak4-May-14 18:37
mveDave Kreskowiak4-May-14 18:37 
QuestionCapture a monochrome image of panel control Pin
gwittlock3-May-14 2:27
gwittlock3-May-14 2:27 
AnswerRe: Capture a monochrome image of panel control Pin
Richard MacCutchan3-May-14 3:32
mveRichard MacCutchan3-May-14 3:32 
GeneralRe: Capture a monochrome image of panel control Pin
gwittlock3-May-14 3:41
gwittlock3-May-14 3:41 
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 

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.