问题描述
我见过关于应用程序冻结的许多类似帖子,但没有解决我的问题.一切都在try catch块和backgroundworker中 但应用程序仍会冻结Internet连接是否处于活动状态.我调试了代码,发现导致以下代码.每次我启动应用程序时,都会在随机的时间间隔内发生这种情况.有时效果很好,但是当我睡在窗户上时 然后唤醒它,使应用程序变得繁忙.当我删除以下代码时,它可以正常工作,但我不知道此代码中的问题在哪里.
I have seen many similar posts regarding application freezing but did not solve my problem. Everything is in try catch block and in backgroundworker but still application freezes either the internet connection is alive or not. I have debugged the code and found causing the below code. This happens in random time spans each time I start the application. Sometimes it works fine but when I sleep the windows and then wakeup it the application gets busy. When I remove the following code it works fine but I don't know where is the problem in this code.
private void backgroundWorkercheckonline_DoWork(object sender, DoWorkEventArgs e)
{
string selectremotevalue = "SELECT InstallationKey, Status FROM SMSClientTable";
Liscenseform formcallliscence = new Liscenseform();
string status = null;
for (; ; )
{
if (backgroundWorkercheckonline.CancellationPending)
{ e.Cancel = true;
break;
}
#region
MySqlConnection remotecon = new MySqlConnection(strsitecon);
MySqlConnection remotecon1 = new MySqlConnection(strsitecon);
try
{
remotecon.Open(); //open the connection
}
catch(Exception ex)
{
MessageBox.Show("not connected to remote site");
if (offlinestatus == "inactive")
{
Messagebox formcall = new Messagebox();
formcall.ShowDialog();
Environment.Exit(0);
}
Thread.Sleep(1000 * 2 * 60); continue;
}
try
{
MySqlCommand selectvalue = new MySqlCommand(selectremotevalue, remotecon);
MySqlDataReader remotereader = selectvalue.ExecuteReader();
while (remotereader.Read())
{
// MessageBox.Show(remotereader.GetString(0) + "|" + remotereader.GetString(1));
if (remotereader.GetString(0) == installationkey)
{
status = remotereader.GetString(1);
break;
}
}
remotereader.Close();
remotecon.Close();
}
catch (Exception ex)
{
MessageBox.Show("Unable to read or connect");
}
if (status != null)
{
#region
if (status == "Inactive" || status == "inactive")
{
try
{
this.bwsttsrpi = new BinaryWriter(new FileStream("sttsrpi.ocx", FileMode.Create));
this.bwsttsrpi.Write("inactive");
bwsttsrpi.Close();
}
catch { }
Messagebox formcall = new Messagebox();
formcall.ShowDialog();
MessageBox.Show("SMS and Email Marketing Software has been registered with fake serial. The environment will now try to close.", "Fake Serial Notification");
Environment.Exit(0);
}
if (status == "active")
{
if (offlinestatus == "inactive")
{
try
{
this.bwsttsrpi = new BinaryWriter(new FileStream("sttsrpi.ocx", FileMode.Create));
this.bwsttsrpi.Write("active");
bwsttsrpi.Close();
}
catch { }
}
Thread.Sleep(1000 * 2 * 60); continue;
}
#endregion
}
else
{
try
{
remotecon.Open(); //open the connection
}
catch (Exception ex)
{
}
try
{
String insrtablestr = "INSERT INTO SMSClientTable VALUES(@Name, @Email, @Phone, @Price, @InstallationKey, @SerialKey, @Status, @DateTime)";
MySqlCommand insertvalue = new MySqlCommand(insrtablestr, remotecon);
insertvalue.Prepare();
//we will bound a value to the placeholder
insertvalue.Parameters.AddWithValue("@Name", name);
insertvalue.Parameters.AddWithValue("@Email", email);
insertvalue.Parameters.AddWithValue("@Phone", Phone);
insertvalue.Parameters.AddWithValue("@Price", price);
insertvalue.Parameters.AddWithValue("@InstallationKey", installationkey);
insertvalue.Parameters.AddWithValue("@SerialKey", serialkey);
insertvalue.Parameters.AddWithValue("@Status", "active");
insertvalue.Parameters.AddWithValue("@DateTime", datetimecheck);
insertvalue.ExecuteNonQuery(); //execute the mysql command
}
catch (Exception ex)
{
}
}
#endregion
}
Environment.Exit(0);
}
推荐答案
|