简体   繁体   English

在 ThreadPool.UnsafeQueueUserWorkItem() function 中等待进程完成

[英]wait for a process to finish inside an ThreadPool.UnsafeQueueUserWorkItem() function

Im doing a web application that will play a media.我正在做一个将播放媒体的 web 应用程序。 I have a class A that contains a method,say MyMethod:我有一个包含方法的 class A,比如 MyMethod:

public void MyMethod()
        {
            .....
            Process proc = ....
            ThreadPool.UnsafeQueueUserWorkItem((o) =>
            {
                proc.WaitForExit();
                proc.Close();
                .....

             },null);
          }

in code behind of my page I will call this method that will run the process and then will play the media, the process converts file to.flv type.My problem is becoz im using an asynchronous system,even if the thread didnt finish its work on unload, the page will load but without playing the media.在我页面后面的代码中,我将调用此方法,该方法将运行该进程然后播放媒体,该进程将文件转换为.flv 类型。我的问题是因为我使用的是异步系统,即使线程没有完成它的工作卸载时,页面将加载但不播放媒体。 How can i wait for the process inside the ThreadPool to terminate?我如何等待 ThreadPool 中的进程终止? and THEN load my page??然后加载我的页面?? thanks alot.多谢。

if you want your asp.net resqponse to wait for the process result, call WaitForExit directly.如果您希望您的 asp.net 响应等待处理结果,请直接调用 WaitForExit。 Don't use the ThreadPool.不要使用线程池。

If for some reason you feel you absolutely need to use the ThreadPool, you can use a ManualResetEvent to wait for the result in the calling thread.如果由于某种原因你觉得你绝对需要使用 ThreadPool,你可以使用 ManualResetEvent 来等待调用线程中的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM