设置进程退出的等待时间 [英] Set waiting time for process to exit

查看:125
本文介绍了设置进程退出的等待时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我使用以下代码在bat文件中以
运行该过程

Hi
I use the following code to run the process in bat file as

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = "d:/my.bat";
Console.Write("Running {0} ", p.StartInfo.FileName)
p.Start();
while (!p.HasExited)
{
    Console.Write(".");
    // wait one second
    Thread.Sleep(1000);
}
Console.WriteLine(" done.");
p.Close();
p.Dispose();




它执行得很好,但是我的问题是

在那个bat文件中,我只是运行命令以进行db2数据库转储,而在正常单击bat文件时,大约需要1-2分钟才能完成,但是通过代码,我需要等到它克服那之后,它

我尝试了许多代码,但没有成功

等待您的宝贵意见




it executed well but my problem is

In that bat file i am just running a command to take db2 database dump while on normal clicking of bat file it takes around 1-2 min to complete but through code i need to wait until it get over that''s where i stuck on it

I tried many code but did not succeed

Waiting for your valuable comments

推荐答案

我是对的,您的问题是,程序执行正在等待进程停止,而您不这样做.看不到这些点吗?

如果那是问题,也许您可​​以尝试将进程执行放在单独的线程中,并且可以使用在主线程和进程线程之间共享的公共变量来控制循环.


嗯,第一次,我可能有点误解了你的问题.您是否要等待此批处理文件执行?我认为那您应该使用线程和Join方法.我写了一些代码,之后请告诉我它是否在做您想要的:).

代码:

Am I right, that your problem is, that the program execution is waiting for process'' halt, and you don''t see the dots?

If that''s the problem, maybe you can try putting the process execution in a separated thread and you can use a common variable shared within main thread and process thread to control the loop.


Hmm, first time I maybe a little misunderstood your question. Do you want to wait for this batch file execution? I think then you should use threading and Join method. I wrote a little code, please tell me after that, whether it is doing what you want or not :).

The code:

class ProcessThread
    {
        public void execute()
        {
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = false;
            p.StartInfo.FileName = "d:/my.bat";

            Console.Write("PThread:::Running {0} ", p.StartInfo.FileName);
            p.Start();

            while (!p.HasExited)
            {
                Console.Write(".");
                // wait one second
                Thread.Sleep(1000);
            }
            Console.WriteLine("PThread done.");

            p.Close();
            p.Dispose();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ProcessThread pt = new ProcessThread();
            Thread processThread=new Thread(new ThreadStart(pt.execute));

            processThread.Start();

            Console.WriteLine("Start waiting###");
            processThread.Join();
            Console.WriteLine("Continuing in main thread");

            //some calculations
            double a = 3;
            double b = 4;
            double c = Math.Sqrt(a * a + b * b);

            Console.WriteLine("The result is {0}",c);
          
        }
    }


在一些计算"中仅向您显示它们在该过程之后运行.


At the the ''some calculations'' are only for show you, that they are running after the process.


这篇关于设置进程退出的等待时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆