Solved

Managing Windows Processes in Web Service

Posted on 2011-03-24
4
367 Views
Last Modified: 2012-06-27
I am currently firing off a windows process using System.Diagnostics.Process and waiting for the process to complete before allowing the code to proceed.  This is currently inefficient especially when I have 30 or 40 different process to fire off.  I wouldn't want to run all of them at once but I would be ok with running five of them at a time.  

The main requirement that I have is that I need to be able to know when all of the processes are completed.  To accomplish this currently I am using an asynchronous web service call that runs the processes.  When the web service call is completed I know that the all of the processes are finished.  

So my question is can I run more than one process at a time and at the same time, know when all of them have completed, and keep the web service call from returning a response?  If so, can you provide a simple example of how I might accomplish this.
0
Comment
Question by:kmcbrearty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 35208110
I'm not sure to completely understand your question... but I'll give a try.

Is your call to the web service working ok? I mean, are you able to notify to web service caller at the moment that you want?

If so, why don't you try to instance a List of processes, launch them and periodically check if all of them have exited?

For example:
Dim myProcesses As List(Of Process) = New List(Of Process)
'Add your processes to the list
...
'Launch all the processes
For Each p As Process In myProcesses
    p.Start()
Next

'Keep on waiting until all processes has exited
Dim allExit As Boolean
Do
    'Sleep the task 100 ms
    System.Threading.Thread.Sleep(100)
    'Check if all the processes has exited
    allExit = True
    For Each p As Process In myProcesses
        If Not p.HasExited Then
            allExit = False
            Exit For
        End If
    Next
Loop Until allExit
'Once here, you know that ALL your processes has exited

Hope that helps.
0
 
LVL 3

Author Comment

by:kmcbrearty
ID: 35208205
Yes, my call to the web service works exactly the way I want it to.

I was thinking something along the lines of the code that you provided but I don't want to run all of them at once.  I don't want more than five process running at a time.  I like your idea of creating all of the processes before hand in a list and I didn't realize the process had a HasExited property.  That solves my initial problem with how to keep track of the processes.
0
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 35208406
Ok, with this changes I think you'll be in the right way...

Hope that helps.
Dim myProcesses As List(Of Process) = New List(Of Process)

'Add your processes here...
...

Const max_processes_at_same_time As Integer = 5
Dim allExit As Boolean
Do
    'Sleep the thread 100 ms
    System.Threading.Thread.Sleep(100)
    'Check the state of all the processes
    Dim runningProcesses As Integer = 0
    Dim unstartedProcesses As Integer = 0
    Dim firstUnstarted As Process = Nothing
    allExit = True
    For Each p As Process In myProcesses
        Try
            If p.Id <> 0 Then
                'The process has started
                If Not p.HasExited Then
                    'Not all process has existed
                    allExit = False
                    'Add 1 to running processes
                    runningProcesses += 1
                End If
            End If
        Catch ex As Exception
            'The process has not started
            unstartedProcesses += 1
            If IsNothing(firstUnstarted) Then firstUnstarted = p
            allExit = False
        End Try
    Next

    'If there are unstarted processes and we can start another one, start it
    If (unstartedProcesses > 0) AndAlso (runningProcesses < max_processes_at_same_time) Then
        firstUnstarted.Start()
    End If
Loop Until allExit

Open in new window

0
 
LVL 3

Author Comment

by:kmcbrearty
ID: 35208816
I took your code and expanded on it a little bit to make it more readable and efficient.  Thank you for a great starting point.

Here is my final code:

    Public Shared Sub RunReports(ByVal Reports As IList(Of ScheduledReportSummary))
        Const MaxRunningProcess As Integer = 5
        Dim LastRanProcess As Integer

        ' Create Management Lists
        Dim ToProcess As List(Of Process) = GetProcessList(Reports)
        Dim Running As New List(Of Process)

        ' Run Reports
        Do
            'Sleep the thread 100 ms
            System.Threading.Thread.Sleep(100)
            RemoveFinishedProceses(Running)

            While Running.Count < MaxRunningProcess AndAlso LastRanProcess < ToProcess.Count
                Dim p As Process = ToProcess.Item(LastRanProcess)
                p.Start()

                Running.Add(p)
                LastRanProcess += 1
            End While
        Loop Until Running.Count = 0
    End Sub

    Private Shared Sub RemoveFinishedProceses(ByRef ProcessList As List(Of Process))
        ' Create a copy of the list to enumerate
        Dim l As List(Of Process) = ProcessList.ToList

        For Each p As Process In l
            If p.HasExited Then
                ProcessList.Remove(p)
            End If
        Next
    End Sub

Open in new window

0

Featured Post

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question