1

This is really short question. I don't understand try-catch mechanism completely.

This is my current code:

public static void WriteText(string filename, string text)
{
    try
    {
        System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
        file.Write(text);
        file.Close();
    }
    catch(Exception exc)
    {
        MessageBox.Show("File is probably locked by another process.");
    }
}

Background:

Im writing application that shares configuration files with another application.

I need some dialog messagebox with "retry" and "abort" buttons, when that file is used by other application. When that message will appear - I will close that other application and I will try to rewrite that file again by pressing "Retry" button.

4

4 回答 4

4

我们所拥有的是使用计数器进行重试,并且可能使用线程休眠。

所以像

int tries = 0;
bool completed = false;
while (!completed)
{
    try
    {
        System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
        file.Write(text);
        file.Close();
        completed = true;
    }
    catch(Exception exc)
    {
        tries++;
        //You could possibly put a thread sleep here
        if (tries == 5)
            throw;
    }
}
于 2013-09-09T18:35:05.937 回答
2

即使已经有一个很好的答案,我也会提交一个更适合 OP 问题的答案(让用户决定而不是使用计数器)。

public static void WriteText(string filename, string text)
{
    bool retry = true;
    while (retry)
    {
         try
         {
              System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
              file.Write(text);
              file.Close();
              retry=false;
          }
          catch(Exception exc)
          {
                MessageBox.Show("File is probably locked by another process.");
                // change your message box to have a yes or no choice
                // yes doesn't nothing, no sets retry to false
          }
    }
}

如果您需要有关如何实施消息框的更多信息,请查看以下链接;

http://msdn.microsoft.com/en-us/library/0x49kd7z.aspx

消息框按钮?

于 2013-09-09T18:43:00.743 回答
1

我会这样做:

 public static void WriteText(string filename, string text, int numberOfTry = 3, Exception ex = null)
    {
        if (numberOfTry <= 0)
            throw new Exception("File Canot be copied", ex);
        try
        {
            var file = new System.IO.StreamWriter(filename);
            file.Write(text);
            file.Close();
        }
        catch (Exception exc)
        {
            WriteText(filename,text,--numberOfTry,ex);
        }
    }
于 2013-09-09T18:43:12.793 回答
0

我更喜欢这样(示例尝试在关闭时保存 RichTextBox 并允许重试保存或中止关闭):

protected override void OnClosing(CancelEventArgs e)
{
    if (richTextBox_Query.Modified)
    {
        DialogResult result;
        do
            try
            {
                richTextBox_Query.SaveFile(
                    Path.ChangeExtension(Application.ExecutablePath, "sql"),
                    RichTextBoxStreamType.UnicodePlainText);
                result = DialogResult.OK;
                richTextBox_Query.Modified = false;
            }
            catch (Exception ex)
            {
                result = MessageBox.Show(ex.ToString(), "Exception while saving sql query",
                    MessageBoxButtons.AbortRetryIgnore);
                e.Cancel = result == DialogResult.Abort;
            }
        while (result == DialogResult.Retry);
    }

    base.OnClosing(e);
}
于 2015-10-21T14:04:11.773 回答