Click here to Skip to main content
15,910,009 members
Home / Discussions / C#
   

C#

 
GeneralRe: Service does not start Pin
Jordanwb15-Dec-09 2:18
Jordanwb15-Dec-09 2:18 
GeneralRe: Service does not start Pin
Jimmanuel15-Dec-09 3:26
Jimmanuel15-Dec-09 3:26 
GeneralRe: Service does not start [modified] Pin
Jordanwb15-Dec-09 5:31
Jordanwb15-Dec-09 5:31 
GeneralRe: Service does not start Pin
Jimmanuel15-Dec-09 6:33
Jimmanuel15-Dec-09 6:33 
GeneralRe: Service does not start Pin
Jordanwb15-Dec-09 7:17
Jordanwb15-Dec-09 7:17 
QuestionPerhaps a threading problem? Pin
George Quarton14-Dec-09 10:12
George Quarton14-Dec-09 10:12 
AnswerRe: Perhaps a threading problem? Pin
Luc Pattyn14-Dec-09 11:56
sitebuilderLuc Pattyn14-Dec-09 11:56 
GeneralRe: Perhaps a threading problem? Pin
George Quarton14-Dec-09 12:17
George Quarton14-Dec-09 12:17 
Okay sorry for not explaining this clearly. My goal is simple really. What I want to achieve is;

1. Detect a USB Insertion (this works fine)
2. Upon detection begin copying the contents from the USB drive onto the hard drive. (this works fine)
3. Update a label control on my form

Now if we call the FileCopying method in the same thread the form can not be updated (the form just freezes as copying is taking place)

To count-act this I went into the Designer View and added a 'BackgroundWorker' control which allowed me to run the FileCopying method in that thread so
that the form is able to be updated. (This works, the copying takes place and the form isn't frozen)

Now as point 3. on my list is to update the control this is what I need to do.
Hopefully this can be explained better in code than words so;

public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            CopyObject a = e.Argument as CopyObject;
            RecursiveCopy.FullCopy(a.sConfigLocation, a.sourceFolder, a.destFolder, a.sDriveName, a.iniFile);
            e.Result = "1";
        }


This is the 'BackgroundWorker' control as added before and the RecursiveCopy.FullCopy is called in this function (which is called in another thread).
This works fine, the file copy operation is called succesfully as shown here;

public class RecursiveCopy
    {
        public static void FullCopy(string sConfigLocation, string sourceFolder, string destFolder, string sDriveName, object iniFile)
        {
            if (!Directory.Exists(destFolder))
                Directory.CreateDirectory(destFolder);
            //System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
            int countFile = 0;
            string[] files = Directory.GetFiles(sourceFolder);
            foreach (string file in files)
            {
                string name = Path.GetFileName(file);
                string dest = Path.Combine(destFolder, name);
                // below here I am trying to update the label control on my form
                // Perhaps this is the wrong way to go about it?
                PenDriveBackup myClass = new PenDriveBackup(); // the PenDriveBackup class is where the form resides.
                myClass.backgroundWorker1.ReportProgress(0, file); // the backgroundWorker1 function sits in the PenDriveBackup class.
                // The ReportProgress() method will pass the file name into the ProgressChanged method which I will document below.
                File.Copy(file, dest, true);
            }
            string[] folders = Directory.GetDirectories(sourceFolder);
            foreach (string folder in folders)
            {
                string name = Path.GetFileName(folder);
                string dest = Path.Combine(destFolder, name);
                Console.WriteLine("Copying " + folder);
                FullCopy(sConfigLocation, folder, dest, sDriveName, iniFile);
            }
        }
    }


public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            string message = e.UserState as string;
            fileCopyingLbl.Text = message; // Update the label control.
        }


So from what I know the ProgressChanged method is there so you can update your Form. So from what I believed should work from calling this method is that
the label will be updated as shown in the code.

If I were to put a MessageBox in the method then I can see that the file names are passed into the method properly.
For example;

public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            string message = e.UserState as string;
            MessageBox.Show(message); // Here
            fileCopyingLbl.Text = message; // Update the label control.
        }


Everytime a file gets copied in the RecursiveCopy.FullBackup method it will send the data into that function and I can see the MessageBoxes for each file.
So I know the data is getting sent between the methods correctly just that the label control is not getting updated.

Perhaps I am tackling the problem the wrong way? Maybe with the way I'm calling it?
PenDriveBackup myClass = new PenDriveBackup();
     myClass.backgroundWorker1.ReportProgress(0, file);


I hope the information this time is enough to hel you get a grasp of the project I am under-going. Thanks for replying, George.
GeneralRe: Perhaps a threading problem? Pin
Luc Pattyn14-Dec-09 12:41
sitebuilderLuc Pattyn14-Dec-09 12:41 
GeneralRe: Perhaps a threading problem? Pin
George Quarton14-Dec-09 12:45
George Quarton14-Dec-09 12:45 
GeneralRe: Perhaps a threading problem? [modified] Pin
Luc Pattyn14-Dec-09 13:00
sitebuilderLuc Pattyn14-Dec-09 13:00 
QuestionTiming-related issue when connecting to a newly created database Pin
vtchris-peterson14-Dec-09 8:38
vtchris-peterson14-Dec-09 8:38 
AnswerRe: Timing-related issue when connecting to a newly created database Pin
Migounette14-Dec-09 20:44
Migounette14-Dec-09 20:44 
GeneralRe: Timing-related issue when connecting to a newly created database Pin
vtchris-peterson15-Dec-09 3:56
vtchris-peterson15-Dec-09 3:56 
QuestionC# RichTextBox strange behaviour Pin
ikurtz14-Dec-09 8:13
ikurtz14-Dec-09 8:13 
AnswerRe: C# RichTextBox strange behaviour Pin
OriginalGriff14-Dec-09 8:42
mveOriginalGriff14-Dec-09 8:42 
GeneralRe: C# RichTextBox strange behaviour Pin
ikurtz14-Dec-09 8:50
ikurtz14-Dec-09 8:50 
GeneralRe: C# RichTextBox strange behaviour Pin
OriginalGriff14-Dec-09 9:09
mveOriginalGriff14-Dec-09 9:09 
GeneralRe: C# RichTextBox strange behaviour Pin
ikurtz14-Dec-09 9:14
ikurtz14-Dec-09 9:14 
QuestionWONDER Pin
farokhian14-Dec-09 7:58
farokhian14-Dec-09 7:58 
AnswerRe: WONDER Pin
loyal ginger14-Dec-09 8:08
loyal ginger14-Dec-09 8:08 
AnswerGOLDEN WONDER Pin
Keith Barrow14-Dec-09 8:09
professionalKeith Barrow14-Dec-09 8:09 
GeneralRe: GOLDEN WONDER Pin
farokhian14-Dec-09 8:16
farokhian14-Dec-09 8:16 
AnswerRe: WONDER Pin
OriginalGriff14-Dec-09 8:39
mveOriginalGriff14-Dec-09 8:39 
GeneralRe: WONDER Pin
farokhian14-Dec-09 8:59
farokhian14-Dec-09 8:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.