我遇到了基於各種形式和各種線程的應用程序的問題。線程在應用程序結束之前關閉,但即使如此,我也無法以100%的準確性終止我的應用程序。C#應用程序不關閉
有時過程仍然繼續,但沒有任何顯示(Form
s或甚至Thread
s)。我認爲這個問題是在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);
}
}
當標誌「停止」更改爲true在我的註銷功能這一個停止。我已經嘗試評論「thread.sleep」,但問題仍然出現。
您可以請發佈*主應用程序線程*如何結束的代碼? – GregRos 2012-07-17 14:22:22
你可以做的是:當你的應用程序處於幻想模式時,你運行VS並將它附加到你的進程中。然後,你暫停執行並顯示線程(Debug menu => windows => threads)。有沒有你開始的任何線程(直接或間接)? – Kek 2012-07-17 14:27:58
插入數據庫連接代碼的代碼 – AndreLucas13 2012-07-17 15:13:09