这个是一个基于winform的客户端程序,使用了网络提供的web服务,进行国内机票查询,本文主要讲解一下具体的客户端开发
主界面如下图:
关于如何引用一个web服务,请参见一些网络资料,比如:MSDN
下面,我为了使用多线程,并且向线程中传递参数,新建了一个类库thread.cs,具体代码如下:
thread.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace WindowsService
{
class thread
{
string startcity;//定义输出参数,出发城市
string endcity;
string date;//出发时间
DataSet ds;//返回的数据集
public string Scity
{
get { return startcity; }
set { startcity = value; }
}
public string Ecity
{
get { return endcity; }
set { endcity = value; }
}
public string Dt
{
get { return date; }
set { date = value; }
}
public DataSet Ds
{
get { return ds; }
}
public DataSet GetTime()
{
try
{
airline.DomesticAirline airline = new WindowsService.airline.DomesticAirline();//实例化服务
ds = airline.getDomesticAirlinesTime(Scity, Ecity, Dt, "");//输入参数,得到查询结果
return ds;//返回结果
}
catch (Exception e)
{
throw new Exception(e.ToString());
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace WindowsService
{
class thread
{
string startcity;//定义输出参数,出发城市
string endcity;
string date;//出发时间
DataSet ds;//返回的数据集
public string Scity
{
get { return startcity; }
set { startcity = value; }
}
public string Ecity
{
get { return endcity; }
set { endcity = value; }
}
public string Dt
{
get { return date; }
set { date = value; }
}
public DataSet Ds
{
get { return ds; }
}
public DataSet GetTime()
{
try
{
airline.DomesticAirline airline = new WindowsService.airline.DomesticAirline();//实例化服务
ds = airline.getDomesticAirlinesTime(Scity, Ecity, Dt, "");//输入参数,得到查询结果
return ds;//返回结果
}
catch (Exception e)
{
throw new Exception(e.ToString());
}
}
}
}
通过这个类库,就完成了向线程中传递参数Scity,Ecity等工作了。下面我们设置窗体代码如下:
Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsService
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
UpdateGridview = new GetTimeAir(GetTimes);
}
string date;//预先定义好参数
string sc;
string ec;
delegate void GetTimeAir();//定义委托,实现多线程
GetTimeAir UpdateGridview;
private void button1_Click(object sender, EventArgs e)
{
try
{
Thread get = new Thread(GetTimes);
get.IsBackground = true;
get.Start();
statusBar1.Text = "正在获取数据,请耐心等待";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
statusBar1.Text = "数据收集完成,耐心等待显示";
}
}
private void GetTimes()//主要的方法
{
if (dataGridView1.InvokeRequired)
{
this.Invoke(UpdateGridview);
}
else
{
try
{
thread th = new thread();//实例化thread的类th
th.Scity = sc;
th.Ecity = ec;
th.Dt = date;
dataGridView1.DataSource = th.GetTime().Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
}
}
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
date = dateTimePicker1.Value.ToShortDateString();//获取起飞时间
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
sc = comboBox1.SelectedItem.ToString();//获取出发城市
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
ec = comboBox2.SelectedItem.ToString();//获取抵达城市
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsService
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
UpdateGridview = new GetTimeAir(GetTimes);
}
string date;//预先定义好参数
string sc;
string ec;
delegate void GetTimeAir();//定义委托,实现多线程
GetTimeAir UpdateGridview;
private void button1_Click(object sender, EventArgs e)
{
try
{
Thread get = new Thread(GetTimes);
get.IsBackground = true;
get.Start();
statusBar1.Text = "正在获取数据,请耐心等待";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
statusBar1.Text = "数据收集完成,耐心等待显示";
}
}
private void GetTimes()//主要的方法
{
if (dataGridView1.InvokeRequired)
{
this.Invoke(UpdateGridview);
}
else
{
try
{
thread th = new thread();//实例化thread的类th
th.Scity = sc;
th.Ecity = ec;
th.Dt = date;
dataGridView1.DataSource = th.GetTime().Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
}
}
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
date = dateTimePicker1.Value.ToShortDateString();//获取起飞时间
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
sc = comboBox1.SelectedItem.ToString();//获取出发城市
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
ec = comboBox2.SelectedItem.ToString();//获取抵达城市
}
}
}
至此,整个过程都好了,调试一下,由于网速,数据量大小问题,请耐心点哦。对了这里提供下载整个项目:点击下载