Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinApp Form Crash Without any Error or Exception .Net

I have a problem with My WinApp Form program which contain a tab Control with WebBrowser control DLL (GeckoFX).

My application while running close without any exception or anything. It could happen after few minutes or max after 10 min. In visual studio I see application terminate with code 0. Anything.

In program.cs I catch all this unhandled excpetion

` // Add the event handler for handling UI thread exceptions to the event.
                 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(UIThreadException);

  // Set the unhandled exception mode to force all Windows Forms errors to go through
    // our handler.
                  Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

 // Add the event handler for handling non-UI thread exceptions to the event. 
                 AppDomain.CurrentDomain.UnhandledException +=
                     new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);`

I already Check in Windows Event logger for any error but it's clean. Just like the Program is terminated well. I don't know if it's Gecko DLL fault but I don't think so.

I use httpWebRequest to download a list which contains some URL.

Then I use a Backgroundworker which read the list of the URL , and invoke addTab Delegate Method Sleep a bit until page is loaded and continue with other AddTab Invoke.

When the list is empty I check if in the DOM page there is a certain string Then in Backgroundworker Complete I Close All Tabs and Dispose them and I click on button1 which start the Backgroundworker1.asyncall();

Is there something wrong with my Logic? I will post code too, I need that it's too long but I really need understand where could be the error that terminate my application. If someone can help me to see why it crash without any error or anything I will appreciate that.

private void Start_Back_Click(object sender, EventArgs e)
    {                         
        List<Links> tempList = getListFromWeb();

        if (!backgroundWorker1.IsBusy)
        { 
            backgroundWorker1.RunWorkerAsync(tempGoogle);
        } 
    }

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        { 
            List<Links> temp = (List<Links>)e.Argument; 
            foreach (Links link in temp)
            {                 
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true; return;                    
                }
                _busy.WaitOne();

                if (tabs.InvokeRequired)
                { 
                        m_addTab addTabInvoke = addTabUrl;
                       Invoke(addTabInvoke, new Object[] { link.url, link.StringToSearch }); 
                }
            } 
            Thread.Sleep(2000); 
            if (tabs.InvokeRequired)
            { 
                foreach (Browser tempBrowser in ListCurrentBrowser)
                {
                    if (backgroundWorker1.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }
                    _busy.WaitOne();
                    Thread.Sleep(1000);
                    m_SeachTab addSearchInvoke = addTabPSearch;
                    Invoke(addSearchInvoke, tempBrowser); 
                }
            }
        }

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {   //Check Stuff Error and Cancelled
            if (e.Error != null)
            {... }
            else if (e.Cancelled)
            { ....}
            else //Else remove all tab
            {  
              bool canRemove = this.TabCount >= 1;
            if (canRemove)
            { 
                WebBrowserTabPage tab = this.SelectedWebBrowserTagPage; 
                this.TabPages.Remove(tab);
                tab.Dispose();
            }
             **Start.Back.PerformClick();** //Click on button again to start another time the backgroundworker
}

}

like image 881
user1107078 Avatar asked Jan 21 '12 14:01

user1107078


1 Answers

From Microsoft Site: Starting with the .NET Framework version 4, this event is not raised for exceptions that corrupt the state of the process, such as stack overflows or access violations, unless the event handler is security-critical and has the HandleProcessCorruptedStateExceptionsAttribute attribute. Maybe you should try addding that attribute.

For Application.ThreadException, from the Microsoft Site again: "To guarantee that no activations of this event are missed, you must attach a handler before you call Application.Run." In your code it is not clear if you attach the handler before calling Application.Run.

Also - you may want to have the "generic" try catch block on places that may be calling unmanaged code:

try {
// Code goes here
}
catch { //Unmanaged exceptions will be caught here as well.

}

try { 
// Code goes here.
}
catch(Exception ex){ // only managed exceptions are caught, avoid that in your case.
}

The first will catch unmanaged exceptions while the second will not.

like image 100
Pavel Donchev Avatar answered Oct 05 '22 23:10

Pavel Donchev