dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

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

  1. #1
    Join Date
    May 2019
    Location
    Michigan
    Posts
    5

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

    Hello;

    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:

    Code:
    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
                BackgroundWorker1.CancelAsync()
            ElseIf RadioButton4.Checked = True Then
                BackgroundWorker1.RunWorkerAsync()
            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
                    Label7.Hide()
                End If
    
            Next
    
    End Sub 
    
    End Class
    Name:  Ticker controls.jpg
Views: 107
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
    Location
    WV
    Posts
    5,321

    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.
    Code:
    '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)
            Try
                BeginInvoke(textDelegate)
            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
      Label1.Text=MyVar
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)