Session 9

Threads
(TThread class, thread priorities, TListView class, file system search, ShellExecute, Windows clipboard)

Session #9

  1. During the first part of the session I show a very simple demo on how to capture all message sent to the window and how to capture events when idle. The application doesn't do much, but it gives you a glimpse into how you dealing with all of the messages that Windows might send to your application.

    I also showed a nifty tool from Microsoft called Spy++. It comes with Visual Studio and you can find it on the Start Menu under:
     Programs | Microsoft Visual Studio 2010 | Visual Studio Tools | Spy++
    
    It's kind of a niche tool, but when you need to know about Windows messages and stuff, it can be invaluable.


  2. During the second part of the session, I show some code that demonstrates how to search the file system for files. For example, you can find all of the .cpp files on the D: drive. The purpose of this was to set up for the next version which is going to be a multi-threaded file find program. This will allow you to search all of your drives simultaneously. Look at the code to get familiar with the algorithm. Since the directory structure in Windows uses a tree structure, it lends itself to a simple recursive algorithm. The tree isn't a binary tree, since a directory can have 0, 1, or N subdirectories (children). The algorithm isn't any more complicated than if there were just two subdirectories. The application itself is very simple because it's only meant to show the recursive search algorithm. When I extend it with multiple threads, the application will be more useful.

    If you've never "searched" a file system below, you should check out the help for the functions: FindFirst, FindNext, and FindClose. You should be able to figure out how they work from the example code and the online help. These functions are wrappers around _findfirst, _findnext, and _findclose. (Some compilers will accept these latter functions with or without the leading underscores.) Some information on these functions.


  3. During the third part of the session, I showed another way of traversing subdirectories in the Windows file system. This time, I made a multi-threaded application that allowed the application to search in multiple folders simultaneously. There is a lot of good information in this simple application. For details on working with the TThread class, look at Chapter 11 Writing multi-threaded applications in the Developer's Guide.

    I also demonstrated how to launch files from within the application. By double-clicking on an item in the list view, that file will "launch". If it's an executable, it will run, it if it's a text file, it will be opened by the associated text editor, if it's an HTML file, it will be loaded by the default browser, etc. You can choose how to "launch" the file based on the radio buttons.

    Finally, I showed how to use the clipboard to put files on it and then paste (copy) them to another folder in Windows. This is how it works when you copy and paste files in Explorer.

    Update for the Clipboard: RAD Studio 2010 and later uses Unicode throughout the library. This can cause some problems when migrating from the older Turbo C++ version (which all of these code samples are based). I discovered one such "issue" when using the Windows clipboard. You need to make a slight change in order for the code to work properly.

    Change c_str() to t_str() near the end of MainForm.cpp:

    // Copy the filename starting at 'next' in the buffer strcpy(next, Filelist->Strings[i].c_str());
    to
    // Copy the filename starting at 'next' in the buffer strcpy(next, Filelist->Strings[i].t_str());
    Without this change, you may get weird results when pasting into Windows Explorer:

    Update for XE 3: I just discovered that the above no longer works in XE 3. You need to cast the String to AnsiString:

    // Copy the filename starting at 'next' in the buffer strcpy(next, AnsiString(Filelist->Strings[i]).c_str());