3

我在使用基于各种形式和各种线程的应用程序时遇到了问题。线程在应用程序结束之前关闭,但即便如此我也无法以 100% 的准确度终止我的应用程序。

有时该过程仍然可以继续工作,但没有出现任何内容(Forms 甚至Threads)。我认为问题出在program.cs上,几乎可以肯定,所以我将代码粘贴在这里

 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()这是我在应用程序中出现的第一个表格。

这是线程的代码:

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);
    }   
}

当我的注销功能上的标志“停止”更改为真时,这个停止。我已经尝试评论“thread.sleep”,但问题仍然出现。

4

3 回答 3

5

通常,发生这种情况是因为您有一些从未终止的线程。为避免这种情况,如果您希望应用程序结束非 UI 线程的任何状态,则应创建它们并将 IsBackground 标志设置为 true。

于 2012-07-17T13:58:27.823 回答
1

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.

于 2012-07-18T10:14:53.463 回答
0

除非有一个压倒一切的原因,你绝对必须明确终止线程,(有一些,例如,规范说所有数据库连接必须在退出时关闭,并且你有这样的连接绝对绑定到线程/s创建它们),不要显式终止线程!

如果可以的话,就让操作系统来做吧。它比你或我编写的用户代码要好得多。

于 2012-07-17T14:53:20.340 回答