CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2019

    [RESOLVED] Using a Background Worker in Visual Studio 2012 Express


    I am attempting to use a Backgroundworker in Visual Studio 2012 Express to display a ticker.

    I actually want to do this on 2 forms: a main form and a settings form.

    On the settings form, all I want to do is:

    1) When it is turned on (selected to show via radiobutton) display previously set text in a label character by character at a speed selected by the user (via trackbar value [ie: using a sleep call with the trackbar value * 1000]
    Then a small blank space repeated by the previously set text.

    2) If it is turned off then stop the background worker, clear the label text, and hide the label.

    I keep getting IllegalCrossThread calls when attempting to do this on the settings page so I haven't even attempted any code for the main page.

    On the main page, I just want the ticker to display the previously selected text character by character at the set "ticker speed" and loop just like on the settings page.

    This is what I have gotten thus far:

    Module Module1
        Public TickerTest As String = "***** This is how fast the ticker will run..."
        Public TickerMsg As String = "***** Today's winning bonus is: "
    End Module
    Public Class Form3
         Public CharAdd As Integer = 0
    Private Sub RadioButton4_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged
            RadioButton4.Font = IIf(RadioButton4.Checked = True, New Font(RadioButton4.Font, FontStyle.Bold), New Font(RadioButton4.Font, FontStyle.Regular))
            If RadioButton4.Checked = False Then
            ElseIf RadioButton4.Checked = True Then
            End If
            OKToolStripMenuItem.Enabled = True
            SAVEToolStripMenuItem.Enabled = True
    End Sub
    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            For CharNum = 1 To Len(Trim(TickerTest))
                CharAdd += 1
                Label7.Text = Mid(TickerTest, CharNum, CharAdd)
                System.Threading.Thread.Sleep(My.Settings.TickerSpeed * 1000)
                If BackgroundWorker1.CancellationPending = True Then
                    Label7.Text = Nothing
                End If
    End Sub 
    End Class
    Name:  Ticker controls.jpg
Views: 655
Size:  14.9 KB

    May I get some help please?
    Thank you.

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008

    Re: [RESOLVED] Using a Background Worker in Visual Studio 2012 Express

    You have to set up a delegate to do updates to the UI thread.
    'at the top of the form class 
    Delegate Sub SetTextDelegate()
    'Sub that would be called from the worker thread to trigger an update of the UI thread
    Private Sub DoUpdate()
            Dim textDelegate As New SetTextDelegate(AddressOf UpdateDisplay)
            Catch ex As Exception
            End Try
        End Sub
    'sub containing code that handles the actual update
    Private Sub UpdateDisplay()
      ' code that updates the UI thread
    End Sub
    Always use [code][/code] tags when posting code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width