Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Managing Windows Processes in Web Service

Posted on 2011-03-24
4
Medium Priority
?
370 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 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

688 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