Click here to Skip to main content
15,911,530 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm having an issue using the method ShowDialog() in my application. It runs in a WTS server, and there are thin clients that log in that server to run the application.
I was having some problems that when the user opens the window, the system just crashes and they cannot do anything more, then I had to force the logoff of their thin clients.
I put a timer that if the thread isn't closed in 30s the system logs off to avoid these crashes, and then log on again automatically, but it's just until I can't resolve the real issue.
Then I put a try/catch block in the ShowDialog call, but it simply doesn't catch anything and the system still crashes.

I simply can't debug that because it doesn't happens all the time. In my PC it never happened, and they use most of the time normally, but sometimes this happens and that really bugs me.

Anyone has something that could help me?

C#
try
    {
        //I tried like this:
        //base.ShowDialog();

        //and then like this (FPrincipal.ActiveForm is the Form that call this modal window)
        base.ShowDialog(FPrincipal.ActiveForm);
    } 
    catch (ArgumentException e)
    {
        sendsEmail();
        
    } 
    catch (InvalidOperationException e)
    {
        sendsEmail();
    }


This is a old project, it was made in VS2008 and uses .net Framework 2.0.

Thanks a lot!
Posted
Comments
Vedat Ozan Oner 6-Feb-14 13:56pm    
exception may not be ArgumentException or InvalidOperationException. What don't you try to catch all? catch(Exception ex) { processException(ex); }
Marcelo Camillo 6-Feb-14 14:12pm    
I forgot to mention but I've already tried that and it didn't work. So I looked what exceptions this method throws and put both specifically, and still doesn't catching anything. But thanks for the suggestion!
ZurdoDev 6-Feb-14 14:28pm    
Popping up a window in a Terminal Server used to be an issue years ago. I'm surprised you're still having this issue. What version of Terminal Server?
Marcelo Camillo 7-Feb-14 5:30am    
We use Windows Server 2003.

1 solution

Please try the following code. I think you can find the reason, or at least stops crashing.

C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace Test_UnhandledException
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // unhandled exception handling
            Application.ThreadException += Application_ThreadException;
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            // starting form1
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            AddLog((Exception)e.ExceptionObject);
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            AddLog(e.Exception);
        }

        static void AddLog(Exception exception)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(@"d:\tmp\app.log", true))
                {
                    sw.WriteLine(exception.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}
 
Share this answer
 
Comments
BillWoodruff 7-Feb-14 0:59am    
+5 an impressive response !
Marcelo Camillo 7-Feb-14 6:26am    
I just updated the application with your suggestion and I really think it can help me a lot. Thanks!
Vedat Ozan Oner 7-Feb-14 11:19am    
you are welcome. I hope it helps.
Marcelo Camillo 14-Feb-14 6:30am    
We changed the TS for a new server with Windows Server 2008 R2 three days ago, and since then we didn't have any problems. Thanks a lot for your attention!
Vedat Ozan Oner 14-Feb-14 8:12am    
you are welcome :)

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