Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- For my USB Disk Ejector program:
- --------------------------------
- {
- I`m catching windows messages related to drive addition/removal. When I get the messages that a drive has been added or removed I rescan all drives and repopulate my list of usb drives.
- However sometimes another drive might be added/removed when I`m rescanning all the drives - so I need to scan the drives again.
- This is perfectly fine, but what I do now is bad - I have a global variable that I increment each time I get the "DrivesHaveChanged" message. I have a thread that rescans the drives but this thread also writes to the global variable too.
- Help me please:
- ---------------
- This is essentially a threading problem. I`m writing to a global variable from within my main thread and a child thread - I know this is bad.
- Any ideas on how I fix this? A critical section is apparently one solution?
- At the moment I do this:
- ------------------------
- Pseudo-pascal code:
- }
- fChangeMessageCount: global variable - integer
- procedure CatchTheMessages
- begin
- if ItsTheMessageIWant then
- begin
- inc(fChangeMessageCount); //increment it
- if EventsThread.Suspended then EventsThread.Resume;
- end;
- end
- procedure TEventsThread.Execute;
- begin
- while not terminated do
- begin
- if self.Terminated then break;
- if (fChangeMessageCount > 0) and (fEjector.Busy = false) then
- begin
- sleep(500); //gives extra time for devices with multi volumes/partitions
- fChangeMessageCount:=0; //set it back to 0 because we're about to scan
- RescanAllDrives;
- end
- else
- self.Suspend;
- end;
- end;
Add Comment
Please, Sign In to add comment