如何限制一次生成的进程数?

问题描述 投票:0回答:1

我正在使用VB.NET Windows Forms应用程序,在该应用程序中,用户应该能够确定一次允许应用程序启动多少个进程。

我当前的方法通常可以用,但是我注意到有时应用程序超出了设置的数量。为此,我使用了两个全局变量,_ConcurrentRuns在应用程序开始时为0,而_MaxConcurrentRuns由用户设置。

Private _sync As new Object()

' This is called Synchronously
Private Function RunModel() As Boolean
    If CancelExectuion Then Return CancelCleanup()

    Do While True
        SyncLock _sync
            If _ConcurrentRuns < _MaxConcurrentRuns Then
                Interlocked.Increment(_ConcurrentRuns)
                Exit Do
            End If
        End SyncLock
        Threading.Thread.Sleep(50)
    Loop

    'This is what will launch an individual process and close it when finished
    ret = RunApplication(arg)
    ' The process has been closed so we decrement the concurrent runs
    Interlocked.Decrement(_ConcurrentRuns)

    Return ret
End Function

目标是一次只让一个线程退出while循环,我无法在调试模式下捕获它,但是在任务管理器中,它偶尔会超出原本应该使用的1-3个进程。这使我假设多个线程以某种方式进入了同步锁,但是我不知道这种情况如何发生。

非常感谢您抽出宝贵的时间来阅读我的问题。

vb.net winforms
1个回答
0
投票

因此,看来我的解决方案可解决此问题,我不想删除此问题,因为它可能对将来的其他人有所帮助。

答案:使用更好的过程监控软件/在任务管理器中将优先级设置为高。

© www.soinside.com 2019 - 2024. All rights reserved.