Solved

Managing Windows Processes in Web Service

Posted on 2011-03-24
4
363 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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