我遇到了基于各种形式和各种线程的应用程序的问题。线程在应用程序结束之前关闭,但即使如此,我也无法以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