Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

c# - WaitForExit () remains don't finish

public void btnBackup_Click(object sender, EventArgs e)
    {
        BackupDatabase(
            "localhost",
            "5432",
            "postgres",
            "***********",
            "PerfectFreight",
            txtPath.Text,
            "Backup #",
            "C:\Program Files\PostgreSQL\13\bin\");
    }
    public static string BackupDatabase(
        string server,
        string port,
        string user,
        string password,
        string dbname,
        string backupdir,
        string backupFileName,
        string backupCommandDir)
    { 
        try
        {
            Environment.SetEnvironmentVariable("PGPASSWORD", password);

            string backupFile = backupdir + backupFileName + DateTime.Now.ToString("MM") + "_" + DateTime.Now.ToString("dd") + "_" + DateTime.Now.ToString("yyyy") + "_Time?" + DateTime.Now.ToString("HH") + ";" + DateTime.Now.ToString("mm") + ".backup";

            string BackupString = " -U " + user + " -h " + server + " -p " + port + " -d " + dbname + " -F c -b -v -f "" + backupFile;

            Process proc = new Process();
            proc.StartInfo.FileName = backupCommandDir + "\pg_dump.exe";

            proc.StartInfo.Arguments = BackupString;

            proc.StartInfo.RedirectStandardOutput = true;//for error checks BackupString
            proc.StartInfo.RedirectStandardError = true;

            proc.StartInfo.UseShellExecute = false;//use for not opening cmd screen
            proc.StartInfo.CreateNoWindow = true;//use for not opening cmd screen

            proc.Start();
            proc.WaitForExit();
            proc.Close();

            MessageBox.Show("Backup successfully created");


            return backupFile;

            
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
            return null;
        }
    }

The program starts to run but does not finish, it does not give an error. In the instruction proc.WaitForExit(); the execution gets stuck as in an infinite loop.

question from:https://stackoverflow.com/questions/66056680/waitforexit-remains-dont-finish

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The arguments to pg_dump will turn out as

-U postgres -h localhost -p 5432 -d PerfectFreight -F c -b -v -f "...Backup #02_05_2021_Time?10;32.backup

Apart from the crazy file name, there are some serious problems with that:

  • you forgot the closing double quote in the end

  • the option -v does not mean "verbose", but sets a variable and requires an argument

Try to come up with a command that works first, then use it in a program.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...