AndreLucas13
AndreLucas13

Reputation: 41

C# application doesn't close

I'm having a trouble with my application that is based on various forms and various threads. The threads are closing before the application ends, but even so I can't terminate my application with 100% accuracy.

Sometimes the process still continues to work but nothing is showing up (Forms or even Threads). I think the problem is on program.cs, almost sure it is, so I'll paste here the code

 private static Process old;
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        XmlConfigurator.Configure();
        if (PriorProcess() != null)
        {
            try
            {
                old.Kill();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ja existe uma instancia do SpotLight em execucao.", "Aviso");
                return;
            }                
        }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Support());

    }



    public static Process PriorProcess()
    {
        Process curr = Process.GetCurrentProcess();
        Process[] procs = Process.GetProcessesByName(curr.ProcessName);
        foreach (Process p in procs)
        {
            if ((p.Id != curr.Id) && (p.MainModule.FileName == curr.MainModule.FileName))
            {
                old = p;
                return p;
            }
        }
        return null;
    }

New Support() its my first form showing up in the application.

This is the code for the threads:

private void check_calls()
{
        while (stop==false)
    {
        string actualTime = DateTime.Now.ToString("T");
        connection = new MySqlConnection(ConnectionString);
        MySql.Data.MySqlClient.MySqlCommand msqlCommand = new MySql.Data.MySqlClient.MySqlCommand();
        msqlCommand.Connection = connection;
        msqlCommand.CommandText = "Select cd.uniqueid,sup.timespan from contactCenterDevel.cc_cdr_support_pending sup, vboxZon.cdr cd WHERE sup.phone=cd.src AND cd.finish=0 AND cd.accountcode='serviin' AND cd.duration >0 AND cd.lastapp='Vxml' ORDER BY cd.calldate DESC LIMIT 1";

        try
        {
            connection.Open();
            MySql.Data.MySqlClient.MySqlDataReader msqlReader = msqlCommand.ExecuteReader();
            while (msqlReader.Read())
            {
                int timespanCompare = convertTimetoSecs(msqlReader.GetString(1));
                int actualtimeCompare = convertTimetoSecs(actualTime);

                if (timespanCompare < actualtimeCompare)
                {
                    updateFlagCDR(msqlReader.GetString(0));
                }
            }
        }
        catch (Exception er)
        { MessageBox.Show("Mysql actions error: " + er); }
        finally
        { connection.Close(); }

       Thread.Sleep(10000);
    }   
}

This one stops when the flag "stop" is changed to true on my logout function. I already tried commentating "thread.sleep" but the problem still appear.

Upvotes: 3

Views: 4257

Answers (3)

AndreLucas13
AndreLucas13

Reputation: 41

I solved the problem. My application connects with an Asterisk server and uses it as an integration to apply VOIP on it. Asterisk runs a thread that allows you to "log in and log off" to the server and that thread is always waiting for a final response from the user code part. Checking in the source code of Asterisk I noticed that the thread is not as IsBackground and as a Sleep that waits for a logoff function. This is the reason why most of the time the application didnt close and thank you for your support guys.

Upvotes: 1

Martin James
Martin James

Reputation: 24847

Unless there is an overriding reason why you absolutely must terminate the threads explicitly, (there are a few, eg. the spec says that all DB connections must be closed on exit and you have such connections that are absoultely bound to the thread/s that created them), don't explicitly terminate the threads!

If you can, just let the OS do it. It's way better at it than user code written by you or I.

Upvotes: 0

Kek
Kek

Reputation: 3195

Usually, this happens because you have some threads that have never been terminated. To avoid this, if you want your application to end whatever the status of the non-UI threads, you should create them setting the IsBackground flag to true.

Upvotes: 5

Related Questions