How to stop function once message box displayed

雨燕双飞 提交于 2021-02-10 23:19:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!