2014-10-27 47 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

相關問題