Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
| Search | Today's Posts | Mark Forums Read
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 25th, 2008, 04:48 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default How to Test for Presence of a File

I essentially want to do:
Code:
    fnam = Dir$("*.drg")
    Do While True
        If fnam <> "" Then
            ' Process the file
        End If
        Sleep 5000
        fnam = Dir$()
    Loop
    What is the .NET way to do this? The FileWatcher class will not do, because the app will not respond to events while asleep...
  #2 (permalink)  
Old April 26th, 2008, 05:02 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

This is exactly the kind of thing the FileWatcher is intended to do.

What determines that your app is asleep? What kind of application is this (console, service)?

If you call "Sleep", nothing will happen, it's a blocking call.

-Peter
peterlanoie.blog
  #3 (permalink)  
Old April 29th, 2008, 01:51 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Filewatcher seems less than optimal to me. If you start an app with Main(), the app ends when Main ends. To keep an app loaded but inert, it appears that you must have an open form. (It would be nice if there was a way to tell main to just hang out, and wait to either be told to take action, or explicitly told to end, as is the behavior with a form as the startup object.)

I do not want to make a service, because I want a less involved installation, upgrade, maintain process.

I want this app to be started on a server.
I want it to be started under a specific account by Scheduler, starting the app when the server is restarted.
I do not want to put anything on the desktop (like a form).
I want it to check a specific folder for the presence of a specific type of file.
If none, wait a preset amount of time (like 5 seconds) without using any processor resources.
Upon wakeup, check again.
When a file is found there, open it, and perform its designed task with the data contained therein.
Delete the file.
Start checking for a data file again, going to sleep when none found.

FileWatcher raises events. The app has to be able to respond to events for that to work. If Main is the startup object, I think i would need to be idling in a loop to be ready to respond to an event, unless I have a form on the screen to institute truly idle waiting. But I don't want to put a form on the screen...

It turns out that .NET still has Dir$().

System.Threading.Thread.Sleep(TimeSpan.FromSeconds (5)) will put the thread to sleep.

Have you some alternate methods you think would do this better?
  #4 (permalink)  
Old April 29th, 2008, 02:52 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

> If you start an app with Main(), the app ends when Main ends.

Almost true: the app will remain running as long as any thread of the app is still running. I just did a test with a simple console app that spins up 5 threads that each loop five times and sleep for 1 second during each loop. The Main() method exists, but the app stays running until each child thread completes. Of course, this requires you to create a "keep-alive" thread that ensures the app stays running (even if it's doing nothing). Then another thread can run the FileWatcher so file change events get picked up immediately.

> To keep an app loaded but inert, it appears that you must have an open form. (It would be nice if there was a way to tell main to just hang out, and wait to either be told to take action, or explicitly told to end, as is the behavior with a form as the startup object.)

Yes, indeed, this is what a service is all about.

> I do not want to make a service, because I want a less involved installation, upgrade, maintain process.

Done correctly, a service doesn't have to be a painful thing.

Based on everything you describe, you really should make a service. You want an automatically started non-interactive process on a server. Do you expect the server console to always be logged in? This is a security risk and not ideal for other reasons. Even a console app will be interactive to some extent.

You could certainly just build a command line app that scans the directory and does the work in a single pass. Then use some scheduling system (windows scheduler or another) to fire off the process at the desired intervals. I have grown to not rely on windows scheduler because it seems problematic. Take a look at my response on this thread: http://p2p.wrox.com/topic.asp?TOPIC_ID=70964

-Peter
peterlanoie.blog
  #5 (permalink)  
Old April 30th, 2008, 10:37 AM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

When you start an app as a given user with task scheduler so as to start the ap on machine start, the console isn't actually logged in.

And I haave started a VB6 app this way, then logged in as an admin, killed the process with task manager, replaced the .EXE as part of the iterative design/test process, and restarted the app by running the entry in the task-scheduler window. When I logged out as the administrator, the app continued to run as the user set up in task scheduler.

I have some really stringent restrictions that make it almost impossible to get improvements published. I have to keep a really low profile. I'm afraid that creating a service, then installing it would produce a level of visibility that might make the whole authoring action moot.
  #6 (permalink)  
Old April 30th, 2008, 03:55 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Having to operate under that kind of restriction is rather sad to hear.

If you have the authority to get onto the machine to create scheduled tasks and such, I would imagine that creating a service would not be any more visible. Plus, once you have a service up installed, updating it is merely just stopping the service, updating the binaries and starting it back up. I have created automated processed that do this regularly as part of the develop/deploy/test lifecycle.

-Peter
peterlanoie.blog
  #7 (permalink)  
Old April 30th, 2008, 04:39 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

The program I am modifying is already authorized, and installed. So the changes themselves are "under the radar."

We are actually under the constraint that, nationwide, the "corporate intranet" and all associated workstations will have no more than 2,000 applications total. If an MDB is used by more than one person, it counts against that number.

Once we receive our Authorization to Operate (ATO; we currently have an Interim Authorization To Operate), adding an application will necessitate 6+ months of testing (which involves paying a fee), and will, overall, be a 6 to 8 month process. Pretty hard to nimbly solve a problem this way. By the way, a version upgrade counts as adding an application as far as the testing and cost, though it does not count against the 2,000 apps—unless the previous version remains in use...
  #8 (permalink)  
Old April 30th, 2008, 05:44 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

All I can really say to that is "EEEEEK!"


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dealing with the presence of an XML tag rather tha Mike250 SQL Server 2005 1 June 6th, 2008 03:50 PM
Testing for presence of a variable rushman XSLT 2 September 12th, 2007 10:36 AM
How to check for the presence of an attribute dsekar_nat XSLT 2 July 27th, 2006 05:04 AM
check presence of <?xml version="1.0"> sonhir XSLT 2 June 8th, 2006 04:54 PM
test drarem Access 2 March 2nd, 2004 04:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.