The COM Course - Part 3, Page 6
Let's dive straight into creating an application to test our ActiveX EXE file-monitoring component:
- Create a new 'Standard EXE' project
Now let's add a reference to our new File component, then throw in a little testing code for good taste:
- Click 'Project', 'References'
- Check the 'File' component, then click OK
- In the General Declarations section behind your Form, add the code:
Dim WithEvents MyFileObject As FileCheck
- Select 'MyFileObject' from the 'Object' drop-down list
- Ensure you're in the FileFound event
- Tap in the following code:
MsgBox "Found: " & Filename
- Add a Command Button to Form1
- Behind your Command Button, type:
Set MyFileObject = New FileCheckMyFileObject.MonitorFile ("c:\test.txt")
Here, we're setting MyFileObject to a new instance of FileCheck, then we're running the MonitorFile method passing c:\test.txt as an argument. Now behind the scenes this will fire off the Timer and every sixty seconds will check for the file.
Now, that test.txt probably doesn't exist on your computer yet. Wait a few minutes. Nothing should happen. Then create a new file in Notepad, calling it c:\test.txt
Within sixty seconds, a message box should pop up stating it has found your file! Well done, you've just created your own ActiveX EXE!
It's worth noting that this particular component uses asynchronous processing. As we discussed earlier, an ActiveX EXE has its own process space. So when your Timer fires up to check for the existence of a file, it doesn't delay the program using it. If you were using a DLL, it would have done.
And you could put anything behind that Timer code to create a whopping great huge report, a mega calculation, anything!
So far in this course, we've created a couple of real groovy components. But we've not talked about distributing them yet. Let's have a quick natter...
Page 6 of 9