I have attached a screen shot to show exactly what is happening and here is the code.
namespace Wunderground
{
public partial class Form1 : Form
{
private TaskScheduler scheduler = null;
public Form1()
{
InitializeComponent();
this.scheduler = TaskScheduler.FromCurrentSynchronizationContext();
}
private string apiKey = "my key";
private int numberOfDays = 0;
//private List<WxData> historicWeather = new List<WxData>();
public DateTime StartDate
{
get { return this.dateTimePicker1.Value; }
}
public DateTime EndDate
{
get { return this.dateTimePicker2.Value; }
}
private Uri GenerateHistoricURI(string apiKey, DateTime date)
{
string requestUri = String.Format("http://api.wunderground.com/api/{0}/history_{1}/q/MGE.json", apiKey, date.ToString("yyyyMMdd"));
return new Uri(requestUri);
}
private WxData GetWeatherForDate(DateTime date)
{
Uri uriForDate = GenerateHistoricURI(apiKey, date);
string rawWxData = GetJson(uriForDate);
WxData wxForDate = ParseWeatherInfo(rawWxData, date);
//historicWeather.Add(wxForDate);
return wxForDate;
}
private int GetNumberOfDays()
{
TimeSpan ts = EndDate - StartDate;
return ts.Days;
}
private string GetJson(Uri uri)
{
string json = String.Empty;
using (WebClient wc = new WebClient())
{
json = wc.DownloadString(uri);
}
return json;
}
private WxData ParseWeatherInfo(string jsonString, DateTime curDate)
{
//Units in C
int maxTempIndex = jsonString.IndexOf("\"maxtempm\":") + "\"maxtempm\":".Length;
int minTempIndex = jsonString.IndexOf("\"mintempm\":") + "\"mintempm\":".Length;
//Get max and min temps. Regex would be less brute-force
string maxTemp = jsonString.Substring(maxTempIndex, 4).Replace("\"", "").Replace(",", "");
string minTemp = jsonString.Substring(minTempIndex, 4).Replace("\"", "").Replace(",", "");
return new WxData(curDate, maxTemp, minTemp);
}
private void CreateCSV()
{
string path = Path.GetDirectoryName(Application.ExecutablePath);
string fileName = @"\weather.csv";
try
{
using (StreamWriter file = new StreamWriter(path + fileName))
{
foreach (string wx in listBox1.Items)
{
file.WriteLine(wx);
}
}
}
catch (IOException ioex)
{
MessageBox.Show(ioex.ToString());
}
finally
{
MessageBox.Show("Successfully wrote file to " + path);
}
}
private void button1_Click(object sender, EventArgs e)
{
numberOfDays = GetNumberOfDays();
DateTime curDate;
for (int i = 0; i <= numberOfDays; i++)
{
curDate = StartDate.AddDays(i);
//Pause after every 9 requests for 1 minute to comply with Weather Underground api TOS
if (i % 10 == 9)
{
//Find better way to pause execution
Thread.Sleep(60000);
}
else
{
if (curDate <= EndDate)
{
Task.Factory.StartNew<WxData>(() =>
GetWeatherForDate(curDate)).ContinueWith((r) =>
this.listBox1.Items.Add(String.Format("{0},{1},{2}", r.Result.Date.Date, r.Result.MaxTemp, r.Result.MinTemp)),this.scheduler);
//WxData data = GetWeatherForDate(curDate);
//listBox1.Items.Add(String.Format("{0},{1},{2}", data.Date.Date, data.MaxTemp, data.MinTemp));
}
}
}
}
}
}
Any help, ideas, or comments on my implementation are welcome.
Thanks
Attached image(s)
This post has been edited by pcaddict: 30 April 2012 - 08:25 AM

New Topic/Question
Reply




MultiQuote




|