2014-10-27 173 views
-2

我的项目是为了创建一个C#窗口,模拟一个假的GPS,有时不接收数据,并且大部分时间都接收数据。c#没有错误,但没有反应的程序?

我已经模拟使用随机类的c#。

它将所有这些存储在数据库ex_1中的sql服务器表m中。

程序启动一次我点击GPS ON,全球定位系统启动,并收集假数据,模拟使用其他文本框和按钮没有数据的时间段,同时,我应该能够查询数据库,:

  1. 一个文本框需要时间和对象(我跟踪几个对象,现在我跟踪A) 并给我当时的位置。

  2. 另一个文本框中包含位置和对象名称,并给出它在该位置的时间。

从数据库的信息retreival自行工作正常,问题是按钮GPSON!一旦它不起作用!

现在我知道我在程序中有一个无限循环,所以我想知道,是不是可以有其他按钮工作simulatenously,并且这工作无限直到我按close,结束该程序?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public class GPS 
     { 
      public String ObjName; 
      private Int32 Loc; 
      private DateTime date; 

      public void setLoc(Int32 Location) 
      { 
       Loc=Loc+Location; 
      } 

      public Int32 getLoc() 
      { 
       return Loc; 
      } 

      public void setDate(DateTime s) 
      { 
       date = s; 
      } 

      public DateTime getDate() 
      { 
       return date; 
      } 

      public GPS(String n) 
      { 
       ObjName =String.Copy(n); 
       Random rnd1 = new Random(); 
       Loc = rnd1.Next(50); 
      } 
     } 

     SqlConnection cs = new SqlConnection("Data Source=RAMAPRIYASR17A6;Initial Catalog=ex_1; Integrated Security=TRUE"); 
     System.Data.SqlClient.SqlCommand cmd=new System.Data.SqlClient.SqlCommand(); 


     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      cs.Open(); 
      SqlCommand SelectCommand = new SqlCommand("select TIME from m where [email protected] and [email protected]",cs); 
      SelectCommand.Parameters.Add("@name", tb2.Text); 
      SelectCommand.Parameters.Add("@location", tb3.Text); 
      SelectCommand.ExecuteNonQuery(); 

      SqlDataReader reader = SelectCommand.ExecuteReader(); 
      if (reader.Read()) 
      { 
       answerlabel.Text = reader.GetString(0); 
      } 

      cs.Close(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      cs.Open(); 
      SqlCommand SelectCommand = new SqlCommand("select LOCATION from m where [email protected] and [email protected]", cs); 
      SelectCommand.Parameters.Add("@name", tb2.Text); 
      SelectCommand.Parameters.Add("@time", tb1.Text); 
      SelectCommand.ExecuteNonQuery(); 

      SqlDataReader reader = SelectCommand.ExecuteReader(); 

      if (reader.Read()) 
      { 
       answerlabel.Text = reader.GetString(0); 
      } 

      cs.Close(); 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      GPS a = new GPS("A"); 

      Random rnd = new Random(); 
      Random time_skip = new Random(); 

      while(true) 
      { 
       Int32 skip_or_not = time_skip.Next(-61, 600);//if <0 then GPS off 

       //We check if skip_or_not < 0 

       if(skip_or_not<0)//GPS off or off radar 
       { 
        Int32 tChangePos = rnd.Next(-50, 50); 
        a.setLoc(tChangePos); 

        DateTime w = DateTime.Now; 
        TimeSpan timew = new TimeSpan(0, 0, 20, 0,0);//20 mins off radar 
        DateTime combined = w.Add(timew); 
        a.setDate(combined); 
       } 
       else 
       { 
        Int32 ChangePos = rnd.Next(-6, 6); 
        a.setLoc(ChangePos); 

        a.setDate(DateTime.Now); 
       } 

       string stmt = "INSERT INTO m(TIME,LOCATION,NAME) VALUES(@time, @location,@name)"; 

       SqlCommand cmd = new SqlCommand(stmt, cs); 
       cmd.Parameters.Add("@time", SqlDbType.VarChar, 100); 
       cmd.Parameters.Add("@location", SqlDbType.VarChar, 100); 
       cmd.Parameters.Add("@name", SqlDbType.VarChar, 100); 

       cmd.Parameters["@time"].Value = a.getDate().ToString("t"); 
       cmd.Parameters["@location"].Value = a.getLoc().ToString(); 
       cmd.Parameters["@name"].Value = a.ObjName.ToString(); 

       cs.Open(); 
       cmd.ExecuteNonQuery(); 
       cs.Close(); 
      } 
     } 

     private void button4_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 
    } 
} 

这是程序的截图:

Screen shot of Prog

我是C#初学者,所以林不知道我做错了。我认为这与无限循环有关,不知道如何以另一种方式做。

谢谢:)

+0

'while(true)'循环通常需要很长时间才能完成;)使用计时器代替。 – 2014-10-27 10:38:47

+0

你明白'while(true)'是什么意思吗? – 2014-10-27 10:39:00

+0

是的,这是可能的。你应该看看[线程](http://www.codeproject.com/Articles/26148/Beginners-Guide-to-Threading-in-NET-Part-of-n)。 – Adimeus 2014-10-27 10:39:01

回答

2

你正在用你的无限循环阻塞主线程。你应该为你的软件的那部分使用另一个线程。另外,如果您想要停止该功能(例如GPSOFF按钮),您应该在while(some_var)while(true)时更改,并且在启动线程之前将some_var设置为true,并且在您想要停止线程时执行some_var = false 。这将优雅地结束循环。

using System.Threading; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public class GPS 
     { 
      public String ObjName; 
      private Int32 Loc; 
      private DateTime date; 

      public void setLoc(Int32 Location) 
      { 
       Loc=Loc+Location; 
      } 

      public Int32 getLoc() 
      { 
       return Loc; 
      } 

      public void setDate(DateTime s) 
      { 
       date = s; 
      } 

      public DateTime getDate() 
      { 
       return date; 
      } 

      public GPS(String n) 
      { 
       ObjName =String.Copy(n); 
       Random rnd1 = new Random(); 
       Loc = rnd1.Next(50); 
      } 
     } 

     SqlConnection cs = new SqlConnection("Data Source=RAMAPRIYASR17A6;Initial Catalog=ex_1; Integrated Security=TRUE"); 
     System.Data.SqlClient.SqlCommand cmd=new System.Data.SqlClient.SqlCommand(); 
     private Thread myThread; 

     public Form1() 
     { 
      InitializeComponent(); 
      myThread = new Thread(trackGPS); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      cs.Open(); 
      SqlCommand SelectCommand = new SqlCommand("select TIME from m where [email protected] and [email protected]",cs); 
      SelectCommand.Parameters.Add("@name", tb2.Text); 
      SelectCommand.Parameters.Add("@location", tb3.Text); 
      SelectCommand.ExecuteNonQuery(); 

      SqlDataReader reader = SelectCommand.ExecuteReader(); 
      if (reader.Read()) 
      { 
       answerlabel.Text = reader.GetString(0); 
      } 

      cs.Close(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      cs.Open(); 
      SqlCommand SelectCommand = new SqlCommand("select LOCATION from m where [email protected] and [email protected]", cs); 
      SelectCommand.Parameters.Add("@name", tb2.Text); 
      SelectCommand.Parameters.Add("@time", tb1.Text); 
      SelectCommand.ExecuteNonQuery(); 

      SqlDataReader reader = SelectCommand.ExecuteReader(); 

      if (reader.Read()) 
      { 
       answerlabel.Text = reader.GetString(0); 
      } 

      cs.Close(); 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      myThread.Start(); 
     } 

     private void button4_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 

     private void trackGPS() 
     { 
      GPS a = new GPS("A"); 

      Random rnd = new Random(); 
      Random time_skip = new Random(); 

      while(true) 
      { 
       Int32 skip_or_not = time_skip.Next(-61, 600);//if <0 then GPS off 

       //We check if skip_or_not < 0 

       if(skip_or_not<0)//GPS off or off radar 
       { 
        Int32 tChangePos = rnd.Next(-50, 50); 
        a.setLoc(tChangePos); 

        DateTime w = DateTime.Now; 
        TimeSpan timew = new TimeSpan(0, 0, 20, 0,0);//20 mins off radar 
        DateTime combined = w.Add(timew); 
        a.setDate(combined); 
       } 
       else 
       { 
        Int32 ChangePos = rnd.Next(-6, 6); 
        a.setLoc(ChangePos); 

        a.setDate(DateTime.Now); 
       } 

       string stmt = "INSERT INTO m(TIME,LOCATION,NAME) VALUES(@time, @location,@name)"; 

       SqlCommand cmd = new SqlCommand(stmt, cs); 
       cmd.Parameters.Add("@time", SqlDbType.VarChar, 100); 
       cmd.Parameters.Add("@location", SqlDbType.VarChar, 100); 
       cmd.Parameters.Add("@name", SqlDbType.VarChar, 100); 

       cmd.Parameters["@time"].Value = a.getDate().ToString("t"); 
       cmd.Parameters["@location"].Value = a.getLoc().ToString(); 
       cmd.Parameters["@name"].Value = a.ObjName.ToString(); 

       cs.Open(); 
       cmd.ExecuteNonQuery(); 
       cs.Close(); 
      } 
     } 
    } 
} 
+0

IM即将试用此功能并回复:) – LoveMeow 2014-10-27 11:12:43

1

你的问题是,无限循环是在GUI线程,从而使图形用户界面反应迟钝。请阅读BackgroundWorker s。你需要将你的处理移动到后台线程,并且BackgroundWorker是一个简单的方法。

+0

所以如果我将它移动到背景,它仍然是一个无限循环吗?也可以一次运行两个按钮方法,比如我点击按钮1运行,按钮2被点击并同时运行,因为按钮1程序没有结束? – LoveMeow 2014-10-27 11:08:12

+1

可以通过单个按钮触发多个操作同时运行。这些按钮应该开始后台操作,然后立即从click方法返回。后台操作将继续运行。 – 2014-10-27 11:17:43

0

while(true)是无限循环,没有条件来打破循环。 如果您想在一段时间内重复使用Timer类。这里是一个使用定时器的例子:

private void button3_Click(object sender, EventArgs e) 
{ 
     aTimer = new System.Timers.Timer(2000); 
     aTimer.Elapsed += OnTimedEvent; 
     aTimer.Enabled = true; 
} 

private static void OnTimedEvent(Object source, ElapsedEventArgs e) 
{ 
    GPS a = new GPS("A"); 
    Random rnd = new Random(); 
    Random time_skip = new Random(); 
    Int32 skip_or_not = time_skip.Next(-61, 600);//if <0 then GPS off 

    //We check if skip_or_not < 0 
    if(skip_or_not<0)//GPS off or off radar 
    { 
     Int32 tChangePos = rnd.Next(-50, 50); 
     a.setLoc(tChangePos); 
     DateTime w = DateTime.Now; 
     TimeSpan timew = new TimeSpan(0, 0, 20, 0,0);//20 mins off radar 
     DateTime combined = w.Add(timew); 
     a.setDate(combined); 
    } 
    else 
    { 
     Int32 ChangePos = rnd.Next(-6, 6); 
     a.setLoc(ChangePos); 
     a.setDate(DateTime.Now); 
    } 

    string stmt = "INSERT INTO m(TIME,LOCATION,NAME) VALUES(@time, @location,@name)"; 
    SqlCommand cmd = new SqlCommand(stmt, cs); 
    cmd.Parameters.Add("@time", SqlDbType.VarChar, 100); 
    cmd.Parameters.Add("@location", SqlDbType.VarChar, 100); 
    cmd.Parameters.Add("@name", SqlDbType.VarChar, 100); 
    cmd.Parameters["@time"].Value = a.getDate().ToString("t"); 
    cmd.Parameters["@location"].Value = a.getLoc().ToString(); 
    cmd.Parameters["@name"].Value = a.ObjName.ToString(); 

    cs.Open(); 
    cmd.ExecuteNonQuery(); 
    cs.Close(); 
} 
+1

那么,这个循环只能通过一次?这似乎关闭。 – Adimeus 2014-10-27 10:46:36

+0

那么这个循环将运行2000秒?每2秒运行一次 – LoveMeow 2014-10-27 11:06:20

+0

。 – 2014-10-27 11:35:22