我的項目是爲了創建一個C#窗口,模擬一個假的GPS,有時不接收數據,並且大部分時間都接收數據。c#沒有錯誤,但沒有反應的程序?
我已經模擬使用隨機類的c#。
它將所有這些存儲在數據庫ex_1中的sql服務器表m中。
程序啓動一次我點擊GPS ON,全球定位系統啓動,並收集假數據,模擬使用其他文本框和按鈕沒有數據的時間段,同時,我應該能夠查詢數據庫,:
一個文本框需要時間和對象(我跟蹤幾個對象,現在我跟蹤A) 並給我當時的位置。
另一個文本框中包含位置和對象名稱,並給出它在該位置的時間。
從數據庫的信息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();
}
}
}
這是程序的截圖:
我是C#初學者,所以林不知道我做錯了。我認爲這與無限循環有關,不知道如何以另一種方式做。
謝謝:)
'while(true)'循環通常需要很長時間才能完成;)使用計時器代替。 – 2014-10-27 10:38:47
你明白'while(true)'是什麼意思嗎? – 2014-10-27 10:39:00
是的,這是可能的。你應該看看[線程](http://www.codeproject.com/Articles/26148/Beginners-Guide-to-Threading-in-NET-Part-of-n)。 – Adimeus 2014-10-27 10:39:01