我的项目是为了创建一个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