问题
public void CreateFileOutput(object parameter)
{
TransactFileCreation();
WPFMessageBox.Show("test", "Process completed successfully.");
}
public void TransactFileCreation()
{
if (BatchFolderPath == null)
{
WPFMessageBox.Show("test", "Select a Batch folder");
return;
}
// code..
}
I am calling TransactFileCreation() from CreateFileOutput(). Once Msg Box displayed, further the function should not work. But in my case, it again go to the main function and displaying msg box present in that. How to stop execution after once message box is displayed. Provide me a solution. Thanks.
回答1:
You could return a boolean:
public bool TransactFileCreation()
{
if (BatchFolderPath == null)
{
WPFMessageBox.Show("test", "Select a Batch folder");
return false;
}
// code..
return true;
}
And then you call like this:
public void CreateFileOutput(object parameter)
{
if (!TransactFileCreation())
return;
WPFMessageBox.Show("test", "Process completed successfully.");
}
回答2:
Typically you return a bool
from TransactFileCreation
that tells if the operation was successful or not.
Or in serious cases you throw an exception, but that is just for non-regular error flows.
回答3:
You can use Application.Current.Shutdown();
to exit your application from any point.
public void TransactFileCreation()
{
if (BatchFolderPath == null)
{
WPFMessageBox.Show("test", "Select a Batch folder");
Application.Current.Shutdown();
}
// code..
}
回答4:
Create TransactFileCreation() to return bool.
public void CreateFileOutput(object parameter)
{
TransactFileCreation()? WPFMessageBox.Show("test", "Process completed successfully."):WPFMessageBox.Show("test", "Select a Batch folder");
}
public boolTransactFileCreation()
{
return BatchFolderPath == null
}
来源:https://stackoverflow.com/questions/12744154/how-to-stop-function-once-message-box-displayed