Subversion Repositories Code-Repo

Rev

Rev 46 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 46 Rev 48
Line 28... Line 28...
28
 
28
 
29
        public DriveLogger_Form()
29
        public DriveLogger_Form()
30
        {
30
        {
31
            // Disable thread safe checking
31
            // Disable thread safe checking
32
            CheckForIllegalCrossThreadCalls = false;
32
            CheckForIllegalCrossThreadCalls = false;
-
 
33
 
33
            driveEntryList.Clear();
34
            driveEntryList.Clear();
34
            InitializeComponent();
35
            InitializeComponent();
35
 
36
 
36
            this.listView_Drives.AfterLabelEdit += new LabelEditEventHandler(listView_Drives_AfterLabelEdit);
37
            this.listView_Drives.AfterLabelEdit += new LabelEditEventHandler(listView_Drives_AfterLabelEdit);
37
            this.KeyPreview = true;
38
            this.KeyPreview = true;
Line 58... Line 59...
58
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
59
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
59
            bgWorker.RunWorkerAsync();
60
            bgWorker.RunWorkerAsync();
60
        }
61
        }
61
        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
62
        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
62
        {
63
        {
-
 
64
            BackgroundWorker worker = sender as BackgroundWorker;
63
            // Compares a list of drives detected previously with the a list of current
65
            // Compares a list of drives detected previously with the a list of current
64
            // drives on the computer. If a change in length is detected, a drive is either
66
            // drives on the computer. If a change in length is detected, a drive is either
65
            // added or removed so the drive in question is found and operated upon
67
            // added or removed so the drive in question is found and operated upon
66
            List<string> drivesPreviouslyDetected = new List<string>();
68
            List<string> drivesPreviouslyDetectedList = new List<string>();
67
            DriveInfo[] newlyDetectedDrives = DriveInfo.GetDrives();
69
            DriveInfo[] newlyDetectedDrives = DriveInfo.GetDrives();
68
 
70
 
69
            // Resets both lists to hold the same drives
71
            // Resets both lists to hold the same drives
-
 
72
            drivesPreviouslyDetectedList.Clear();
70
            foreach (DriveInfo drive in newlyDetectedDrives)
73
            foreach (DriveInfo drive in newlyDetectedDrives)
71
                drivesPreviouslyDetected.Add(drive.Name);
74
                drivesPreviouslyDetectedList.Add(drive.Name);
72
 
75
 
73
            while (true)
76
            while (true)
74
            {
77
            {
-
 
78
                // Cancels thread if DriveLogger form is closed
-
 
79
                if (!instanceAlreadyRunning)
-
 
80
                {
-
 
81
                    //MessageBox.Show("Thread Stopped");
-
 
82
                    e.Cancel = true;
-
 
83
                    break;
-
 
84
                }
75
                // Pulls list of new drives
85
                // Pulls new list of current drives on computer
76
                newlyDetectedDrives = DriveInfo.GetDrives();
86
                newlyDetectedDrives = DriveInfo.GetDrives();
77
                if (newlyDetectedDrives.Length > drivesPreviouslyDetected.Count)
87
                if (newlyDetectedDrives.Length > drivesPreviouslyDetectedList.Count)
78
                {
88
                {
79
                    // Applies if a drive is attached to the computer
89
                    // Applies if a drive is attached to the computer
80
                    // Goes through each list and finds the drive that was recently attached
90
                    // Goes through each list and finds the drive that was recently attached
81
                    bool attachedDriveExisted = false;
-
 
82
                    bool newDrivesDetected = false;
91
                    bool newDrivesDetected = false;
83
                    string driveOwner = "";
92
                    string driveOwner = "";
84
                    string driveDock = "";
93
                    string driveDock = "";
85
                    foreach (DriveInfo drive in newlyDetectedDrives)
94
                    foreach (DriveInfo drive in newlyDetectedDrives)
86
                    {
95
                    {
87
                        attachedDriveExisted = false;
-
 
88
                        // Loop here checks for non-matching entries in the two lists
96
                        // Skips the drive if it is in the list of drives to ignore
89
                        foreach (string str in drivesPreviouslyDetected)
97
                        if (Settings_Form.driveLoggerDrivesToIgnore.Contains(drive.Name))
90
                        {
-
 
91
                            // If entries match, drive is unchanged
-
 
92
                            if (str == drive.Name)
-
 
93
                            {
98
                            continue;
-
 
99
 
94
                                attachedDriveExisted = true;
100
                        // Check for non-matching entries in the two lists
95
                                break;
-
 
96
                            }
-
 
97
                        }
-
 
98
                        // If list mismatch is detected, adds new entry to driveEntryList
101
                        if (!drivesPreviouslyDetectedList.Contains(drive.Name))
99
                        if (attachedDriveExisted == false)
-
 
100
                        {
102
                        {
101
                            // Creates and populates a new DriveEntry
103
                            // Creates and populates a new DriveEntry
102
                            DriveEntry newEntry = new DriveEntry();
104
                            DriveEntry newEntry = new DriveEntry();
103
                            if (newDrivesDetected == false)
105
                            if (newDrivesDetected == false)
104
                            {
106
                            {
Line 154... Line 156...
154
 
156
 
155
                            paintDriveListbox();
157
                            paintDriveListbox();
156
                        }
158
                        }
157
                    }
159
                    }
158
                    // Clears and refreshes the drivesPreviouslyDetected list
160
                    // Clears and refreshes the drivesPreviouslyDetected list
159
                    drivesPreviouslyDetected.Clear();
161
                    drivesPreviouslyDetectedList.Clear();
160
                    foreach (DriveInfo drive in newlyDetectedDrives)
162
                    foreach (DriveInfo drive in newlyDetectedDrives)
161
                    {
163
                    {
162
                        drivesPreviouslyDetected.Add(drive.Name);
164
                        drivesPreviouslyDetectedList.Add(drive.Name);
163
                    }
165
                    }
164
                }
166
                }
165
                else if (newlyDetectedDrives.Length < drivesPreviouslyDetected.Count)
167
                if (newlyDetectedDrives.Length < drivesPreviouslyDetectedList.Count)
166
                {
168
                {
167
                    // Applies if a drive is removed to the computer
169
                    // Applies if a drive is removed to the computer
168
                    // Goes through each list and finds the drive that was recently removed
170
                    // Goes through each list and finds the drive that was recently removed
169
                    bool removedDriveFound = false;
171
                    bool removedDriveFound = false;
170
                    foreach (string str in drivesPreviouslyDetected)
172
                    foreach (string str in drivesPreviouslyDetectedList)
171
                    {
173
                    {
-
 
174
                        // Skips the drive if it is in the list of drives to ignore
-
 
175
                        if (Settings_Form.driveLoggerDrivesToIgnore.Contains(str))
-
 
176
                            continue;
-
 
177
                        
172
                        removedDriveFound = false;
178
                        removedDriveFound = false;
173
                        // Loop here checks for non-matching entries in the two lists
179
                        // Loop here checks for non-matching entries in the two lists
174
                        foreach (DriveInfo drive in newlyDetectedDrives)
180
                        foreach (DriveInfo drive in newlyDetectedDrives)
175
                        {
181
                        {
176
                            // If entries match, drive was not removed
182
                            // If entries match, drive was not removed
Line 199... Line 205...
199
                            }
205
                            }
200
                            paintDriveListbox();
206
                            paintDriveListbox();
201
                        }
207
                        }
202
                    }
208
                    }
203
                    // Clears and refreshes the drivesPreviouslyDetected list
209
                    // Clears and refreshes the drivesPreviouslyDetected list
204
                    drivesPreviouslyDetected.Clear();
210
                    drivesPreviouslyDetectedList.Clear();
205
                    foreach (DriveInfo drive in newlyDetectedDrives)
-
 
206
                    {
-
 
207
                        drivesPreviouslyDetected.Add(drive.Name);
-
 
208
                    }
-
 
209
                }
-
 
210
                
-
 
211
                else
-
 
212
                {
-
 
213
                    // Sets the two lists to hold the same drives
-
 
214
                    drivesPreviouslyDetected.Clear();
-
 
215
                    foreach (DriveInfo drive in newlyDetectedDrives)
211
                    foreach (DriveInfo drive in newlyDetectedDrives)
216
                    {
212
                    {
217
                        drivesPreviouslyDetected.Add(drive.Name);
213
                        drivesPreviouslyDetectedList.Add(drive.Name);
218
                    }
214
                    }
219
                }
215
                }
-
 
216
 
220
                // Sleeps the thread for a specified amount of time
217
                // Sleeps the thread for a specified amount of time
221
                Thread.Sleep(Settings_Form.driveLoggerRefreshInterval);
218
                Thread.Sleep(Settings_Form.driveLoggerRefreshInterval);
222
            }
219
            }
223
        }
220
        }
224
        void DriveLogger_Form_KeyDown(object sender, KeyEventArgs e)
221
        void DriveLogger_Form_KeyDown(object sender, KeyEventArgs e)