Click here to Skip to main content
15,914,594 members
Home / Discussions / C#
   

C#

 
GeneralRe: Service does not start [modified] Pin
Jordanwb14-Dec-09 17:15
Jordanwb14-Dec-09 17:15 
GeneralRe: Service does not start Pin
Jimmanuel15-Dec-09 1:01
Jimmanuel15-Dec-09 1:01 
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 
I have two classes, my main class (in which a form resides) and a file copying class. (I have other classes they just aren't important).

I have a proc event that determines when a USB drive is inserted and when caught it will fire a WorkerThread which will run the file copying method so that while the file operations are running the Form is still able to be updated.

protected override void WndProc(ref Message m)
        {
            if ((m.Msg == WM_DEVICECHANGE) && (m.WParam == DBT_DEVICEARRIVAL))
            {
                int deviceType = Marshal.ReadInt32(m.LParam, sizeof(int));


                if (deviceType == LOGICAL_VOLUME)
                {
                    uint unitMask = (uint)Marshal.ReadInt32(m.LParam, sizeof(int) * 3);


                    if (unitMask != 0)
                    {
                        char driveLetter = DriveLetterFromUnitMask(unitMask);
                        // some more code here but irrelevant.
                        backgroundWorker1.RunWorkerAsync(copy);
                        // the copy is a new object class containing data, however it is irrelevant
                    }
                }
             }
         }


The thread is not hard coded but simply added using the form controls (BackgroundWorker control).
The thread methods are so (added from double clicking the controls in the designer view)
After adding code I had this;

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";
        }
     public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {  
            string message = e.UserState as string;
            fileCopyingLbl.Text = message; // Update the label control.
        }

So the BackgroundWorker will call FullCopy 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);
  
            int countFile = 0;
            string[] files = Directory.GetFiles(sourceFolder);
            foreach (string file in files)
            {
                string name = Path.GetFileName(file);
                string dest = Path.Combine(destFolder, name);
                DateTime WriteTime = File.GetLastWriteTime(dest);
                IniFunctions.WriteToIni(sConfigLocation, sDriveName, file, WriteTime.ToString(), iniFile);
                object fileCopying1 = file;
                PenDriveBackup myClass = new PenDriveBackup();
                myClass.backgroundWorker1.ReportProgress(0, file); // This will call backgroundWorker1_ProgressChanged method in the main class.
                                                                   //  If I were to put a MessageBox.Show(message); into the ProgressChanged method
                                                                   //  then I can see the correct files changing however the label does not get updated.
                                                                   //  I'm not sure what the problem is but I'm guessing it's something to do with the
                                                                   //  PenDriveBackup myClass = new PenDriveBackup(); not taking control of the form?
                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);
            }
        }
     }


It's been annoying me a lot, trying a lot of different methods and to no avail none of them work.
I don't really understand threading all that much so I'm shooting in the dark here a little bit.

Thanks in advance George.
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 
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 

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.