简体   繁体   中英

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. -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.

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.

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().

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使其等待该进程退出。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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