multithreading - VBScript线程

标签 multithreading vbscript

不久前,我在vbscript中将这个多线程代码粉碎了起来,我刚刚又将其拔出,以将其应用于另一个问题。我现在遇到一个问题,其中运行命令(第20行)的返回值始终为0。我意识到我已经将False用作WaitOnReturn参数,但是否则不会进行线程化。我很确定它能很好地工作,否则我不会保存那么久...

谁能看到我做错了吗?

Class Thread
' Usage:
'    Dim x: Set x = New Thread
'    Call x.init(10)
'    Call x.queue("script.bat", Array("Arg1", "output_file.txt"))
'    Call x.queue("cscript.exe prog.vbs", Array("Arg1", "Arg2", "Arg3"))
'    Call x.setMax(20)
''''''''''''''''''''''''''''''''''''''''''''''
   Private p_threads
   Private p_max

   Private Function spawn(action, args)
      Dim wsh: Set wsh = WScript.CreateObject("WScript.Shell")
      Dim command: command = action
      Dim element
      For Each element In args
         command = command & " " & element
      Next
      spawn = wsh.Run(command, 0, False)
      Set wsh = Nothing
   End Function

   Public Sub queue(action, args)
      If Ubound(p_threads,1) < p_max Then
         ' create new thread
         ReDim Preserve p_threads(Ubound(p_threads, 1)+1)
         p_threads(Ubound(p_threads, 1)) = spawn(action, args)
      Else
         ' recycle old thread
         Do
            Dim i
            For i = 1 To Ubound(p_threads, 1)
               ' find a thread which has finished
               If p_threads(i) = 1 Then
                  p_threads(i) = spawn(action, args)
                  Exit Sub
               End If
            Next
            ' wait for a thread to finish
            WScript.Sleep 300
         Loop Until False
     End If
   End Sub

   Public Sub init(n)
      p_threads = Array(1)
      p_max = n
   End Sub

   Public Property Let setMax(n)
      p_max = n
   End Property
End Class

最佳答案

抱歉,wsh.Run()没有任何想象力。它启动一个新进程,而不是线程。

如果不使用第4个参数,则将0作为返回是预期的结果。文档is here

关于multithreading - VBScript线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4610686/

相关文章:

c++ - 改进服务器以处理多个客户端

c# - 在 C# 中等待 Parallel.Invoke() 结束

javascript - 如何使用 jQuery 调用 vbscript 函数?

web-services - 使用 vbscript 调用带有身份验证的 Webservice

java - 如何保证Java线程不间断地运行直到完成?

python - 使用 dask 作为任务调度来并行运行机器学习模型

vbscript - QTP/UFT Capturebitmap 直接保存到 Quality Center (QC)

windows - 计划任务不运行批处理文件

java - Java中C++的Futures相当于什么

vbscript - 0x800a0e7c - ADODB.Parameters : Parameter object is improperly defined. 提供的信息不一致或不完整