Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 468
  • Last Modified:

Windows 2003 Custom Service

All,

I'm trying to use VB.NET to monitor an application on a WK2003 Server. Basically I would like the service to determine how long that process is running and if its been running for more than 5 minutes then terminate it like you would from task manager. I only know the name of the process. Is this possibile? I have VB.NET Studio 2005.

Thanks in advance,

Mike
0
MiketheD
Asked:
MiketheD
  • 3
  • 3
2 Solutions
 
3_SCommented:
Replace YourServiceName with the actual name.
Is a restart required? or you just want to stop the service?

I don see the point in stopping a service that runs for 5 minutes. Can you not add the program as a scheduled task and terminate it after 5 minutes?

otherwise if it is just a process and not a service
Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("YourProcessName")
For Each p As Process In pProcess
p.Kill()
Next
Imports System.ServiceProcess
Dim myService  As ServiceController
myService  = New ServiceController("YourServiceName")

Dim sStatus As String
myService .Refresh()

Try
   If myService .CanStop Then
      myService .Stop()
      myService .Refresh()
      myService .Start()
   else
      msgbox "Service can not be stopped"
   End If
Catch exp As Exception
   MsgBox("error occured")
End Try

myService.Refresh()
sStatus = myService.Status.ToString
Msgbox "Status of service: " & sStatus

Open in new window

0
 
MiketheDAuthor Commented:
3 S:,

Thanks for the fast reply... Here is the problem I want to solve... I work for a retailer that has a server and a desktop PC. In a nutshell when the user from the desktop runs a report it fires a process called grindq.exe ON the server. This process hangs quite a bit and the only way to get the reports to work again is to terminate the process. I was hoping to create a service that will monitor anytime the "grindq.exe" is launched on the server and if it runs for more than 5 mins terminate it. Hope this makes sense....

Thanks,

Mike
0
 
3_SCommented:
then use this code
Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("grindq.exe")
For Each p As Process In pProcess
p.Kill()
Next

Run this with a timer after five minutes when they launched the report. It the report created by a program of yours? If yes in which program language?

Do take note that this will kill a report even when not started whitin 5 minutes. If you control the program which launches the report you can take the program ID to only kill that specific process.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
MiketheDAuthor Commented:
3_S:,

The report is run from POS software of which I have limited knowledge due to the propriety nature of the system. The code to kill the process makes sense but since they run that report at various times throughout the day, is there a way to check how long a process has been running? I can then write a service that checks every so often to see if grindq.exe is resident & if it has been running longer than 5 minutes terminate it&.

Thanks,
0
 
3_SCommented:
Use this
Declare Function GetProcessTimes Lib "kernel32" (ByVal hProcess As Integer, ByRef lpCreationTime As FILETIME, ByRef lpExitTime As FILETIME, ByRef lpKernelTime As FILETIME, ByRef lpUserTime As FILETIME) As Integer  

The lpCreationTime contains the start time of the process. First you need to get a handle to your running process (=>grindq.exe)
Use
Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
0
 
MiketheDAuthor Commented:
3_S:

Thanks for the help, between your help and a bit of google I'm well on the way.

Mike
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now