The three things you mentioned (Cancel, Progressbar, Completed) are completely different, and need a differenet approach.
Completed is the easy one: it's automatically generated when the DoWork event handler exits. So there is nothing you need to do to handle this, as all background processing ends at that point.
Cancel is not complex, it just takes a little work: you need a class level bool which is set to true in the But_Cancel Click event handler and checked at the top of your Copyy method. If it's true, return immediately.
A ProgressBar is more complex - you need to have access to the BackgroundWorker instance in order to signal the ProgressChanged event via it's ReportProgress method. Fortunately, it is passed to the DoWork event handler as the
sender
parameter, so all you need to do is cast that to a BackgroundWorker and pass it into your Copyy method as a parameter:
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
if (worker != null)
{
private static void Copyy(string s,string d, BackgroundWorker worker = null)
{
Then, when you want to report progress in Copyy:
if (worker != null)
{
worker.ReportProgress(percentage);
}
And pass the worker value on down when you call each recursive instance.
But...you do realize that you don;t need to be recursive at all? You can use Directory.GetFiles to return the full path to all files in a folder, including subdirectories as one line, eliminating the need to recursively process the folders.