Neat Thread that notifies when the amount of Volumes changed


#1

This Thread notifies when the amount of drives/volumes changed in the system, so you can update your TreeView containing the list of drives or whatever.
Just create an instance of it, passing as parameter a pointer to some ChangeListener (you can ofcourse add some more afterwards with addChangeListener() ) and start the Thread with startThread(1) [1 for low priority, so it doesn’t affect the system performance at all].

[code]class VolumesChangedNotifierThread:public Thread, public ChangeBroadcaster
{
public:
VolumesChangedNotifierThread(ChangeListener *listener):Thread(T(“VolumesChangedNotifierThread”))
{
addChangeListener(listener);
oldNumVolumes=getNumVolumes();
}

private:

int getNumVolumes()
{
	OwnedArray<File> volumes;

#if JUCE_WIN32
File::findFileSystemRoots(volumes);
#else
File rootDir=File(T("/Volumes"));
rootDir.findChildFiles(volumes, File::findDirectories | File::ignoreHiddenFiles, false);
#endif
return volumes.size();
}

void run()
{
	while (!threadShouldExit())
	{
		int n=getNumVolumes();
		if (n!=oldNumVolumes)
		{
			oldNumVolumes=n;
			sendChangeMessage(this);
		}
		Thread::sleep(500);
	}
}

int oldNumVolumes;

};[/code]