Windows Processes - Allowing only one (VB.Net)

~J~

~J~

Soldato
Joined
20 Oct 2003
Posts
7,558
Location
London
Help!

Trying to get the code working below but to no joy.

In short, I want to load an application ONCE and ONCE only during a given time period (in the example, it's notepad).

But I've got two problems (big ones too!!)

Firstly, Notepad doesn't open up. It loads as a process, you can check by looking at the windows task manager, but not as an app.

Secondly, I just can't seem to get the thing checking to see if the process is already running, and if it is, then ignore the init for the notepad.

Anyone any ideas?

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
_BatchImportProcess.StartInfo.FileName = "C:\WINDOWS\NotePad.Exe"
_BatchImportProcess.StartInfo.CreateNoWindow = False
_BatchImportProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
_BatchImportProcess.StartInfo.ErrorDialog = True
_BatchImportProcess.StartInfo.UseShellExecute = True
Timer1.Enabled = True
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
If Not _BatchImportProcess.HasExited = True Then
_BatchImportProcess.Kill()
End If
Timer1.Enabled = False
End Sub

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Timer1.Enabled = False
Dim _StartTime As New TimeSpan(10, 0, 0)
Dim _EndTime As New TimeSpan(18, 1, 0)
Dim _Start As Integer = Now.TimeOfDay.CompareTo(_StartTime)
Dim _End As Integer = Now.TimeOfDay.CompareTo(_EndTime)

Dim c As Process()
c = Process.GetProcessesByName(_BatchImportProcess.ProcessName.ToString)

If _Start = 1 And _End = -1 And c.Length = 0 Then
EventLog1.WriteEntry("Watson Batch Import", "Batch entry started at " & Now.ToLongTimeString & " on " & Now.ToLongDateString)
_BatchImportProcess.Start()
End If

Timer1.Enabled = True
End Sub
 
Cheers for the help guys, just a couple of points...

I've managed to get only one instance working now, but I can't get the "Notepad" app to open up even though it's appearing within the task-manager.

Notepad is only an example, the system will launch the actual app (Sage) during the evening whilst no one is at work to import data. I agree totally that the service shouldn't get in the face of the user, hence why it's run at 20:00 on an evening, and will occur on the server that the Sage data is held.

It's literally now a case of asking why/how to get the Notepad to open up fully rather than a new process of Notepad that isn't accessible to the user.
 
So basically, it is working, but Notepad isn't the right way to test it then?

Sage doesn't have service capabilities in the way that the customer wants.

It would be fantastic if the customer had SQL Server as I could set up an automated DTS package to do the import of the data, but they are using MSDE which doesn't have this facility, and thus the API calls are within a DLL that Sage uses to copy, extract, covert and import the data (BACS payments) into Sage.

As for using a scheduled task, I agree, it would be an easy way out, but the client in question has one of these ridiculously water-right security policies where automated tasks that call an EXE are not allowed, it has to be done as a service.

I'll have a go with the actual app itself and see if that fires, there's a lot of examples on the net though that show the Calc or the Notepad been fired as examples, so I'm still not convinced I'm going forward correctly as they seem to be getting results quite good (http://www.vbforums.com/showthread.php?t=384214 is an almost word for word example of what I'm trying to achieve, my post attached at the bottom).

But thanks for the help again, it's really appreciated...
 
Tried the above mate, cheers for that.

Still no joy. I've tried loads of apps and none of them are firing at all. I get the process, but no interactive window at all.

I just don't understand where I'm going wrong. There's loads of examples that are using Notepad or Calc for a 'taster', mine just aint firing at all.

Starting to annoy me now - lol.
 
I know mate, it's really damn frustrating to see everyone else do it but me :(

Maybe I'm cursed! lol

I'll keep battling on, I have to, I'm just wondering if it's anything to do with my machine or not.

The code has changed since the above, only one instance, at the correct time, all works perfectly, just doesn't show for the user to interact with.

Thanks for the follow up though, glad to see there's some hope.
 
Back
Top Bottom