Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to use a BackgroundWorker in .net

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 03-03-2010
Member
 
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.

Reply With Quote
  #2  
Old 03-03-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
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
Reply With Quote
  #3  
Old 03-03-2010
Member
 
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();
        }
    }
}
Reply With Quote
  #4  
Old 03-03-2010
Member
 
Join Date: May 2008
Posts: 2,008
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);
            }
        }
Reply With Quote
  #5  
Old 03-03-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search



All times are GMT +5.5. The time now is 08:39 AM.