Results 1 to 5 of 5

Thread: How to use a BackgroundWorker in .net

  1. #1
    Join Date
    Nov 2009
    Posts
    877

    How to use a BackgroundWorker in .net

    Hello, I am beginner in .net and searching for the program or the source code which will help to know the use of the BackgroundWorker in .net. If anyone from you know how to achieve it, then I will be thankful to you. So, please help me to to know the details about it.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to use a BackgroundWorker in .net

    Hello, I have the code below which will simply help you if you want to know the backgroundworkder in .net. So, just make use of it:
    I
    Code:
    mports System.Threading
    Public Class tee
        Private Sub backgroundWorkerTesting(ByVal sender As Object, _
        ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
    ...........
        End Sub
        Private last As String, StartedAt As String
        Private check As Boolean = False, show As Boolean
        Private WithEvents bgw As New System.ComponentModel.BackgroundWorker
        Public Sub startBackgroundTask()
        check = True
        StartedAt = "Started : " & Format(Now, "h:mm:ss") & "." & Now.Millisecond
        bgw.RunWorkerAsync()
        End Sub
        Private Sub bgw_done(ByVal sender As Object, _
        ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
        Handles bgw.RunWorkerCompleted 
            check = False
            last = "Ended   : " & Format(Now, "h:mm:ss") & "." & Now.Millisecond
            If show = True Then MsgBox(Timestamp, , "completed")
        End Sub
        Public ReadOnly Property Timestamp() As String
            Get
                Return StartedAt & Chr(13) & last
            End Get
        End Property
        Public ReadOnly Property IsWorking() As Boolean
            Get
                Return check
            End Get
        End Property
        Public Property getToKnow() As Boolean
            Get
                Return show
            End Get
            Set(ByVal value As Boolean)
                show = value
            End Set
        End Property
    End Class

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to use a BackgroundWorker in .net

    Hey, you can simply make use of the code below and get the solution for your backgroundworker in .net.
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    namespace Testing
    {
        public partial class BGW : Form
        {
            public BGW()
            {
                InitializeComponent();          
            }
    
            #region "forClass"
            DateTime startD = DateTime.Now;
            #endregion
    
            private void Activated(object sender, EventArgs e)
            {
                bg1.RunWorkerAsync();
                timer1.Start();
            }
    
            private void bg1_DoWork(object sender, DoWorkEventArgs e)
            {
                DataTable datatable;
                l1.Text = "Loading ... " + "Thanks .";
                datatable = getDataTable(1000000);          
                e.Result = datatable;
                l1.Text = "Please, wait ...";
            }
    
            private void bg1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
               -------------------------
            }
    
            private void bg1_done(object sender,doneEventArgs e)
            {
                pbar1.Value = 100;
                dataGridViewCities.DataSource = e.Result;
                l1.Text = "";
                pbar1.Value = 0;
                timer1.Stop();
                toolStripStatusLabelTime.Text = "";
            }
    
            private DataTable getDataTable(int Rows)
            {
                GetData.GetDataHelp getData = new GetData.GetDataHelp();
                return (getData.getDataSetCities(Rows).Tables[0]);
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                TimeSpan timespan = DateTime.Now.Subtract(startD);
                string str ="  ..." + timespan.Minutes.ToString("00") + ":" + timespan.Seconds.ToString("00") + ":" + timespan.Milliseconds.ToString("000");
                toolStripStatusLabelTime.Text = str;
                if (pbar1.Value == pbar1.Maximum)
                {
                    pbar1.Value = 0;
                }
                pbar1.PerformStep();
            }
        }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: How to use a BackgroundWorker in .net

    I have source code in C# which will help you for making use of the backgroundworker. So, use the code below:

    Code:
    private void getStarted(object sender, DoWorkEventArgs dwe)
            {
                CustomerCollection custcoll = new CustomerCollection();
                int totalRCount = 100;
                for (int i = 1; i <= totalRCount ; i++)
                {
                    if (this.bgwData.CancellationPending)
                    {
                        dwe.Cancel = true;
                        return;
                    }
                    Customer cust = new Customer(DateTime.Now.Ticks, Guid.NewGuid().ToString());                    custcoll.Add(cust);
                    this.bgwData.ReportProgress(i, cust);
                    System.Threading.Thread.Sleep(200);
                }
            }

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to use a BackgroundWorker in .net

    It is quiet simple to use. I have the code below which is in Vb.net you can simply use it and get your problem solved:

    Code:
    Private Sub bgwData_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwData.DoWork
    Dim newone As CustomerCollection = New CustomerCollection()
    Dim rcount As Integer = 100
    For i As Integer = 1 To rcount
    If Me.bgwData.CancellationPending Then
    e.Cancel = True
    Return
    End If
    Dim cust As Customer = New Customer(DateTime.Now.Ticks, Guid.NewGuid().ToString())
    newone.Add(cust)
    Me.bgwData.ReportProgress(i, cust)
    System.Threading.Thread.Sleep(20)
    Next
    End Sub

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,715,888.45186 seconds with 16 queries