Click here to Skip to main content
16,019,876 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have an application I created in C# .NET CORE 7. When I run the application in debug mode or release mode I get the same results,
If I open a .chm help file from the application and close the help file, the application also terminates.
Here is the code I use to call the help file from a button in the application,

C#
//This is in a Code File Called Standard
public static readonly string HlpPath = FilesHelper.FindingFileName(Application.StartupPath, "\\Help\\MyApplication Help.chm");


C#
//This is from the button in the application.
private void BtnHelp_ItemClick(object sender, ItemClickEventArgs e)
            {
            try
                {
                Cursor = Cursors.WaitCursor;

                Help.ShowHelp(this, Standard.HlpPath, HelpNavigator.Topic, "Introduction.html");

                Cursor = Cursors.Default;
                }
            catch (Exception ex)
                {
                Cursor = Cursors.Default;
                MessageBox.Show($"Help Error Message - {ex.Message}", "Error Opening Help File", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

When I run the application the help file opens as expected. When I close the help file, it also terminates the application. This doesn't happen in .NET Framework version of the program.
Any help would be appreciated

What I have tried:

C#
//This is in a Code File Called Standard
public static readonly string HlpPath = FilesHelper.FindingFileName(Application.StartupPath, "\\Help\\MyApplication Help.chm");


C#
//This is from the button in the application.
private void BtnHelp_ItemClick(object sender, ItemClickEventArgs e)
            {
            try
                {
                Cursor = Cursors.WaitCursor;

                Help.ShowHelp(this, Standard.HlpPath, HelpNavigator.Topic, "Introduction.html");

                Cursor = Cursors.Default;
                }
            catch (Exception ex)
                {
                Cursor = Cursors.Default;
                MessageBox.Show($"Help Error Message - {ex.Message}", "Error Opening Help File", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
Posted
Updated 26-Jun-23 4:33am

It sounds like an unhandled exception. Try capturing the unhanded exception
C#
try
{
    // set and show
    var mainForm= new MainForm();
    Application.Run(mainForm);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Unhandled Error", MessageBoxButton.OK, MessageBoxImage.Stop);
}


UPDATE

I don't work with .chm help files and yesterday I did not fire up a test project as I was heading out the door. What I did post was "good proactice" handling of exceptions that are not normally caught. There is also a general thread exception that you should also catch if doing any multithreading programming.

This morning a downloaded a test .chm file and wrote a bit of DotNet (Core) 7.0 code to test how the ShowHelp. Here is the sample code used:
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string helpPath = Path.Combine(Environment.CurrentDirectory, "PowerCollections.chm");
        string helpTopic = "Algorithms";
        Help.ShowHelp(this, helpPath , helpTopic);

        Debugger.Break();
    }
}

The code does not stop when the ShowHelp method is called. The Debugger.Break(); is hit immediately after the help file is shown. Continue running the code and close the help file and the WinForm app is still running. The app itself does not close when the help file is closed.
 
Share this answer
 
v2
Comments
BillWoodruff 25-Jun-23 9:48am    
the code shown is not adequate to really trap a thread exception after the .chm file is opened and then closed.
Graeme_Grant 25-Jun-23 10:35am    
The CHM file opens in a sperate app and the code keep executing. The isse is not with closing the CHM file, there is something else going on. Not enough code to identify. The unhandled exceptions is to capture the app exception not being caught outside of the code shown.
BillWoodruff 25-Jun-23 11:37am    
If your code had worked, I would have upvoted it.

The OP doesn't show the code for the 'Help' whatever: I don't think we have enough information to infer where the error is occurring,

The OP appears to have solved his problem himself.
Graeme_Grant 25-Jun-23 13:07pm    
A vote of 1 is extreme considering it is a suitable solution for catching standard unhandled errors. It is a stripped down version of what I use myself.

Did you try the updated code? This does work and I have also explained how it works. There was no indication of multithreading so so the original code would catch the unhandled error if it was not a threaded error.
Graeme_Grant 26-Jun-23 10:56am    
Solution accepted by the OP. You may want to revise that vote of 1 of yours.
Hi,
I tried your solution, it appears there is something else going on with the application. I'm getting a stack overflow error. with
C#
Debugger.Break();
I added to the call the help file. When I try to add or change a control in the development environment Visual Studio stops responding. So it appears there is something else going on in the application which is causing the problem. I need to investigate this. Thank You for your help.
 
Share this answer
 
Comments
Graeme_Grant 26-Jun-23 10:52am    
You should comment on my solution post, not post another solution.

Here is a link to my test: WinFormsHelpChm.zip - Google Drive[^]. Ignore the not found in the help file, it is not important. the sample chm came from here: SubMain / GhostDoc Download Sample CHM[^].

As you will see, there is no issues with my test project. When you hit the breakpoint, continue before closing the Help file. Comment out the breakpoint to see it work fully.
crmfghtr 26-Jun-23 10:57am    
Thank You for your help, It appears there is something else going on with the application. Visual Studio freezes when you try to add or edit a control. I also tried adding a helpprovider control from the toolbox and it does not allow me to add any controls. I need to investigate what is going on. Oddly enough there are NO errors in the .Net Framework (4.8) version of the program.
Graeme_Grant 26-Jun-23 10:59am    
I am using the latest version of VS 2022 CE. My project should run for you.
crmfghtr 26-Jun-23 11:01am    
Yes, so am I I believe it is 17.6.4 I am also using third party controls (DevExpress) in the application.
Graeme_Grant 26-Jun-23 11:08am    
That could be your problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900