简体   繁体   English

c#如何在下一个方法调用之前停止/暂停执行

[英]c# How to stop/pause execution before the next method call

I am a bit of a newbie with C# and hoping to get some guidance on this. 我是C#的新手,希望对此有一些指导。 -I am writing a program that is executing a .bat file when a xml file is dropped into a folder, and then moves the xml file to a done directory. -我正在编写一个程序,当将xml文件放入文件夹中时,该程序正在执行.bat文件,然后将xml文件移动到完成的目录中。

The problem is that the bat file takes ~10 seconds to run and requires the XML file to be present in the directory, however, my move method is moving the xml file immediately after executing the .bat file command, forcing the .bat file itself to fail. 问题是bat文件需要大约10秒钟才能运行,并且要求目录中存在XML文件,但是,我的move方法是在执行.bat file命令后立即移动xml文件,从而迫使.bat文件本身失败。

After researching around, a Thread.Sleep or Task.Delay would be the answer as i would simply like to pause the executing method while the .bat file completes then performs the movefile method however I can not seem to grasp/get the execution to stop for short duration before calling MoveFilesCompleted.MoveComp(). 经过研究,Thread.Sleep或Task.Delay将是答案,因为我只是想在.bat文件完成后暂停执行方法,然后执行movefile方法,但是我似乎无法掌握/让执行停止在调用MoveFilesCompleted.MoveComp()之前持续很短的时间。

This is what I have but hoping for any advice.. 这是我所拥有的,但希望能提供任何建议。

 private void _fileWatcher_Created(object sender, FileSystemEventArgs e)
    {
        FileInfo f = new FileInfo(e.FullPath);
        if (f.Extension.Equals(".xml") || f.Extension.Equals(".XML"))
        {
            Logger.log(String.Format("File Created: Path: {0}, Name: {1}", e.FullPath, e.Name));

            try
            {
                exBat.executeBAT();


            }
            catch (Exception ex)
            {
                Logger.log(String.Format("Running the XMLtoINF SQLload failed."));
            }

            Thread.Sleep(15000);

            MoveFilesCompleted.MoveComp();

        }

    }

It would really help if you posted a bit about how you are executing your batch file! 如果您发布了有关如何执行批处理文件的信息,那将真的有帮助!

Otherwise check here: How to wait until my batch file is finished 否则请在这里检查: 如何等待直到批处理文件完成

假设executeBAT使用System.Diagnostics.Process类启动BAT文件,则可以通过调用Process.WaitForExit使其等待该进程退出。

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

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