.NET Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgramming.NET Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 14th, 2015, 05:45 AM
Ron Miel Ron Miel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 2 Ron Miel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 29 sec
Reputation Power: 0
Updating form from backgroundworker

I can't get my backgroundworker to update my form.

This is just a test, trying to learn HOW to do it before I use it for anything serious.

I can run a procedure from the main thread, it works and updates the form, but locks up all the other controls while running.

I can run an identical procedure from a backgroundworker. I can step through it and see it working, but it doesn't update the form.

The only difference between the two is the second uses reportprogress.

=========

My form has 2 command buttons
- name= cmd_Start, text = "Start"
- name = cmd_thread, text = "Asynch"

It has three labels
- lbl_HH, lbl_MM, lbl_SS

It has one backgroundworker
- name = bgw_1, workerreportsprogress=true, workersupportscancellation=true



Code for the form
Code:
 Public Class Form1

    Private Sub cmd_start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_start.Click
        Dim my_clock As New Class_Clock
        my_clock.run_Clock()
    End Sub

    Private Sub cmd_thread_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_thread.Click
        start_thread()
    End Sub

    Private Sub BackgroundWorker1_DoWork(
                ByVal sender As System.Object,
                ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw_1.DoWork
        Dim my_clock As Class_Clock = CType(e.Argument, Class_Clock)
        my_clock.run_Clock_asynch()
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(
        ByVal sender As System.Object,
        ByVal e As System.ComponentModel.ProgressChangedEventArgs
    ) Handles bgw_1.ProgressChanged

        Dim state As Class_Clock.CurrentState = CType(e.UserState, Class_Clock.CurrentState)

        lbl_HH.Text = state.Hours
        lbl_HH.Refresh()
        lbl_MM.Text = state.Minutes
        lbl_MM.Refresh()
        lbl_SS.Text = state.Seconds
        lbl_SS.Refresh()

    End Sub
End Class
 



Module code
Code:
Module Module_1
    Sub start_thread()
        Dim my_clock As New Class_Clock
        Form1.bgw_1.RunWorkerAsync(my_clock)
    End Sub
End Module


Class code
Code:
 Public Class Class_Clock
    Public Class CurrentState
        Public Hours As Integer
        Public Minutes As Integer
        Public Seconds As Integer
    End Class

    Public Sub run_Clock()

        Dim s As New CurrentState

        For s.Hours = 0 To 24
            Form1.lbl_HH.Text = s.Hours
            Form1.lbl_HH.Refresh()
            For s.Minutes = 0 To 59
                Form1.lbl_MM.Text = s.Minutes
                Form1.lbl_MM.Refresh()
                For s.Seconds = 0 To 59
                    Form1.lbl_SS.Text = s.Seconds
                    Form1.lbl_SS.Refresh()
                    Threading.Thread.Sleep(10)
                Next 'seconds
            Next 'minutes
        Next 'hours
    End Sub

    Public Sub run_Clock_asynch()

        Dim s As New CurrentState

        For s.Hours = 0 To 24
            Form1.lbl_HH.Text = s.Hours
            Form1.lbl_HH.Refresh()
            For s.Minutes = 0 To 59
                Form1.lbl_MM.Text = s.Minutes
                Form1.lbl_MM.Refresh()
                For s.Seconds = 0 To 59
                    Form1.lbl_SS.Text = s.Seconds
                    Form1.lbl_SS.Refresh()
                    Threading.Thread.Sleep(10)
                    Form1.bgw_1.ReportProgress(0, s)
                Next 'seconds
            Next 'minutes
        Next 'hours
    End Sub 'run_Clock_asynch
End Class

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgramming.NET Development > Updating form from backgroundworker


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap