Forums
New posts
Search forums
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
C# Community
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
VB.NET
Windows Forms
updating form from backgroundworker
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="elroyskimms, post: 170766, member: 51356"] [b]Updated Sample[/b] Ron, I apologize for the confusion. I wasn't very clear in my explanation. The problems I pointed out are what was preventing the backgroundworker from updating the form. My goal was to answer your question as to why your code wasn't working as intended. At bare minimum, your code was not working because you were calling Form1.bgw_1 from outside the Form. To make your original code work, you need to change your cmd_thread_Click method to: [CODE] Private Sub cmd_thread_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_thread.Click start_thread(bgw_1) End Sub [/CODE] The start_thread method in Module_1 needs to be: [CODE] Sub start_thread(ibgWorker As BackgroundWorker) Dim my_clock As New Class_Clock(ibgWorker) ibgWorker.RunWorkerAsync(my_clock) End Sub [/CODE] Your Clock_Class needs to have a backgroundworker property: [CODE] Private _bgWorker As BackgroundWorker Public Property bgWorker() As BackgroundWorker Get Return _bgWorker End Get Set(ByVal value As BackgroundWorker) _bgWorker = value End Set End Property Sub New() MyBase.New() End Sub Sub New(ibgWorker As BackgroundWorker) MyBase.New() bgWorker = ibgWorker End Sub [/CODE] and the ReportProgress call in run_Clock_asynch needs to be changed to: [CODE] bgWorker.ReportProgress(0, s) [/CODE] Do this, and your original code will work. Accessing Form1 controls outside of the Form1 class (especially on another thread) was causing the problem you were having. The other change I made (taking all UI control updates out of the backgroundworker code) wasn't necessary to make your sample code run, but it is necessary when you try and take what you learned here and use it in another project. When multiple threads try and update the same UI controls, bad things happen. One solution for that is to use a Delegate and CallBack, but that's beyond the scope of your question. The best way to answer your question without delving into Delegates and CallBacks, was to move the UI code to the proper place. It's not that I was trying to give you a proper study of classes, it's that I was trying to get your code to work and make sure you knew that touching the UI outside the main thread is a bad idea and will lead to all sorts of problems in the future. Trust me, I've been there, done that. You don't want to do it. As to your request for a very simple backgroundworker example, you will find one below. Create a form with 2 buttons: btnStart & btnCancel, a progressbar: pbBackground, and a backgroundworker: bgSample (ReportsProgress: True, SupportsCancellation: True) [CODE] Imports System.ComponentModel Public Class Form1 Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click bgSample.RunWorkerAsync() End Sub Private Sub SampleTask(sender As Object, e As DoWorkEventArgs) Handles bgSample.DoWork For i As Double = 0 To 1000 If bgSample.CancellationPending Then MessageBox.Show("Cancel request received. ", "Cancel", MessageBoxButtons.OK) e.Cancel = True Exit Sub End If bgSample.ReportProgress((i / 1000) * 100) Threading.Thread.Sleep(10) Next End Sub Private Sub ProgressBarUpdate(sender As Object, e As ProgressChangedEventArgs) Handles bgSample.ProgressChanged pbBackground.Value = e.ProgressPercentage End Sub Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click bgSample.CancelAsync() End Sub Private Sub bgSample_WorkComplete(sender As Object, e As RunWorkerCompletedEventArgs) Handles bgSample.RunWorkerCompleted If Not e.Cancelled Then MessageBox.Show("The backgroundworker completed the task.", "Work Complete", MessageBoxButtons.OK) End Sub End Class [/CODE] I hope this helps. -E [/QUOTE]
Insert quotes…
Verification
Post reply
VB.NET
Windows Forms
updating form from backgroundworker
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top
Bottom