C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » Visual Studio »

Multi Thread implementation using Background worker process


Posted Date: 31 Aug 2009    Resource Type: Articles    Category: Visual Studio
Author: SivasakthivelMember Level: Bronze    
Rating: 1 out of 5Points: 5



This code shows Multi Thread implementation using Background worker process


namespace BackWorker
{
///
/// Created by Mss 31/8/09
/// Multi threading implementation
///

public partial class FileCopyWorker : Form
{

string SourcePath, DestPath;

private void btnStartCopy_Click(object sender, EventArgs e)
{
bgwWorker = new BackgroundWorker();
bgwWorker.WorkerReportsProgress = true;
bgwWorker.DoWork += new DoWorkEventHandler(bgwWorker_DoWork);
bgwWorker.ProgressChanged+=new ProgressChangedEventHandler(bgwWorker_ProgressChanged);

SourcePath = DestPath = string.Empty;
SourcePath = @"D:\ImportOCR Test files\Child\Child\001\CDS001.txt";
DestPath = @"C:\CopyoFQuarantine";

//Aynchronous call to the Complex large execution time method
//This will raise DoWork event
bgwWorker.RunWorkerAsync(this);

//
// Send argument to our worker thread
//
//backgroundWorker1.RunWorkerAsync(test);

}

void bgwWorker_DoWork(object sender, DoWorkEventArgs e)
{
//
// e.Argument always contains whatever was sent to the background worker
// in RunWorkerAsync. We can simply cast it to its original type.
//
//TestObject argumentTest = e.Argument as TestObject;

CopyFiles(SourcePath, DestPath);
}

private void CopyFiles(string SourcePath, string DestPath)
{
try
{
int i = 0;
do
{
//Thread.Sleep(200);
File.Copy(SourcePath, DestPath, true);
i++;

//Updating the progress value to control on Main Thread
lblStatus.Invoke(new MethodInvoker(delegate { lblStatus.Text = String.Format("{0} in progress",Convert.ToString(i * 2)); }));

//Background worker progress event, we can pass progress value to this event.
// If we pass like this, we can access main thread controls without use of Method Invoker
bgwWorker.ReportProgress(i*2, "In Progress");

} while (i < 50);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

///
/// This is on the main thread, so we can update a TextBox or anything.
///

private void bgwWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lblStatus.Text = e.ProgressPercentage.ToString()+" in progress";
}

///
/// This is on the main thread, so we can update a TextBox or anything.
///

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
lblStatus.Text = "Copy completed successsfully";
}
}
}


Attachments

  • Download background worker sample (32034-31411-Backworker.rar)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Multi thread + c#  .  Download backgroundworker sample  .  Background worker sample  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: How to filter happening in open file dialog (for beginners)
    Previous Resource: Know About TextWriterTraceListener Class (Csharp)
    Return to Discussion Resource Index
    Post New Resource
    Category: Visual Studio


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use