我正在C#中處理應用程序,並且意外地發現了此異常「Slate.exe中發生了類型爲」System.StackOverflowException「的未處理異常」。在Slate.exe中發生未處理的類型爲「System.StackOverflowException」的異常
以下是我的代碼的詳細信息。
這是我初始化我的主類的對象的類。
public class MenuControls
{
MainWindow MainWindowObj = new MainWindow(); //This thing is raising Exception.
public void SaveAs()
{
SaveFileDialog SFD = new SaveFileDialog();
if (SFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
String FPath = SFD.FileName;
StreamWriter SWriter = new StreamWriter(File.Create(FPath));
SWriter.Write(MainWindowObj.PlayGround.Text);
SWriter.Dispose();
}
}
public void NewFile()
{
MessageBox.Show("You haven't saved your file yet ! Do you want to create new file?", "Something", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
}
public void OpenFile()
{
OpenFileDialog OFD = new OpenFileDialog();
if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StreamReader SReader = new StreamReader(File.OpenRead(OFD.FileName));
MainWindowObj.PlayGround.Text = SReader.ReadToEnd();
}
}
}
我的主類代碼。在那裏我已經初始化了我的MenuConrols類的對象。
public partial class MainWindow : Form
{
public RichTextBox GetBox()
{
return PlayGround;
}
MenuControls Menu = new MenuControls();
TextEditor_Preferences Prefrences = new TextEditor_Preferences();
public int SaveStatus = 0;
TextEditor_Preferences PreferencesForm = new TextEditor_Preferences();
public MainWindow()
{
InitializeComponent();
}
private void MainMenuFile_SaveAs_Click(object sender, EventArgs e)
{
Menu.SaveAs();
SaveStatus = 1;
}
private void MainMenuFile_New_Click(object sender, EventArgs e)
{
if (SaveStatus == 0 && PlayGround.Text.Contains(" "))
{
Menu.NewFile();
} else
{
PlayGround.Text = "";
}
}
private void MainMenuFile_Open_Click(object sender, EventArgs e)
{
Menu.OpenFile();
}
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode.ToString() == "N")
{
if (SaveStatus == 0 && !String.IsNullOrWhiteSpace(PlayGround.Text))
{
DialogResult DResult = MessageBox.Show("You haven't saved your file yet ! Do you want to create new file?", "Something", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (DResult == DialogResult.Yes)
{
PlayGround.Text = "";
} else
{
}
}
else
{
PlayGround.Text = "";
}
}
else if (e.Control && e.KeyCode.ToString() == "O")
{
Menu.OpenFile();
}
else if (e.Control && e.KeyCode.ToString() == "S")
{
Menu.SaveAs();
SaveStatus = 1;
}
}
private void MainMenuEdit_Preferences_Click(object sender, EventArgs e)
{
PreferencesForm.ShowDialog();
}
}
請給我一個解決方案。 如果我錯過了某些東西或者問題沒有得到正確的詢問,請明確告訴我。 謝謝。
你有一個遞歸方法調用的地方。檢查堆棧跟蹤,你會很快找到它。 – dymanoid
MainWindow的每個實例都會創建一個MenuControls的實例。 MenuControls的每個實例都創建了一個MainWindow的實例。 - >無盡的遞歸 – NineBerry
@dymanoid StackOverflowExceptions通常不包含帶有託管調試的堆棧跟蹤。 – vcsjones