📄 mainform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.Threading;
namespace LiveQnA4
{
/// <summary>
/// This sample application takes its ideas from the MSDN Magazine article about RSS feeds on SmartPhones by John Papa.
/// This application only retrieves RSS feeds from Windows Live QnA though. In order to use this application you need to
/// sign up for Live QnA using your Windows Live ID. To use your own live ID in URL encoding when accessing the Live QnA site,
/// you should retrieve your UserID, as is explained here: http://liveqna.spaces.live.com/blog/cns!2933A3E375F68349!390.entry
///
/// This sample application makes use of asynchronous calls to the websites of which RSS feeds are retrieved.
/// The application does automatically refresh the feed. The refresh frequency can be set in the application.
/// The RSS feed is refreshed each time a System.Forms.Timer timer expires.
/// </summary>
/// <remarks>
/// The article and the corresponding source code can be found here:
/// http://msdn.microsoft.com/msdnmag/issues/06/12/DataPoints/
/// </remarks>
public partial class MainForm : Form
{
private DataSet ds;
private bool webRequestPending = false;
private delegate void RefreshListDelegate(DataSet ds);
public MainForm()
{
InitializeComponent();
}
private void menuItemSettings_Click(object sender, EventArgs e)
{
SettingsForm sf = new SettingsForm();
if (sf.ShowDialog() == DialogResult.OK)
{
timer1.Enabled = false;
timer1.Interval = SettingsForm.RefreshTime * 60 * 1000;
timer1.Enabled = true;
}
}
private void MainForm_Load(object sender, EventArgs e)
{
UpdateFeed();
timer1.Interval = SettingsForm.RefreshTime * 60 * 1000;
timer1.Enabled = true;
}
/// <summary>
/// Asynchronously call to the Live QnA service. The advantage of an asynchronous call is that our application
/// remains responsive during calling out to the service.
/// </summary>
private void UpdateFeed()
{
string searchUrl = "http://qna.live.com/Search.aspx?q=meta:Search.qauthor("
+ SettingsForm.UserID +
")&format=RSS";
// If you don't have a Windows Live ID, you can try this sample application by searching for all questions + answers.
// just replace the previous statement with this one:
// string searchUrl = "http://qna.live.com/Search.aspx?q=meta:Search.qstate(0)&format=rss";
webRequestPending = true;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(searchUrl);
IAsyncResult asyncResult = request.BeginGetResponse(new AsyncCallback(EndUpdateFeed), request);
}
/// <summary>
/// Process the reply on our previous WebRequest to find out if we have more questions pending.
/// Since this is an asynchronous operation, this method will be executed on a ThreadPool thread.
/// Note how we update the UI using control.Invoke, because we are running on a separate thread.
/// For more information about multithreaded programming, take a look here:
/// </summary>
/// <param name="result"></param>
public void EndUpdateFeed(IAsyncResult result)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
XmlTextReader xmlRdr = null;
try
{
request = (HttpWebRequest)result.AsyncState;
response = (HttpWebResponse)request.EndGetResponse(result);
xmlRdr = new XmlTextReader(response.GetResponseStream());
ds = new DataSet();
ds.ReadXml(xmlRdr);
response.Close();
xmlRdr.Close();
// check if there are any rows of pending questions to display
if (ds.Tables.Contains("item"))
//if (ds.Tables["item"].Rows != null)
{
Invoke(new RefreshListDelegate(RefreshList), ds);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (response != null)
response.Close();
if (xmlRdr != null)
xmlRdr.Close();
webRequestPending = false;
}
}
private void RefreshList(DataSet ds)
{
listView1.SuspendLayout();
listView1.Items.Clear();
foreach (DataRow itemRow in ds.Tables["item"].Rows)
{
string[] displayInfo = new string[2];
displayInfo[0] = itemRow["title"].ToString();
displayInfo[1] = itemRow["pubDate"].ToString();
ListViewItem item = new ListViewItem(displayInfo);
item.Tag = itemRow;
listView1.Items.Add(item);
}
listView1.ResumeLayout();
}
private void menuItemSelect_Click(object sender, EventArgs e)
{
if (listView1.SelectedIndices.Count == 1)
{
if (listView1.SelectedIndices[0] != -1)
{
DataRow itemRow = listView1.Items[listView1.SelectedIndices[0]].Tag as DataRow;
AnswerForm af = new AnswerForm(itemRow);
af.ShowDialog();
}
}
}
/// <summary>
/// Refresh the Live QnA RSS Feed to find out if we have more of our questions answered.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
UpdateFeed();
}
/// <summary>
/// Make sure that we don't have a WebRequest pending when the user wants to close down the application.
/// If a WebRequest is pending we wait for its reply before terminating the application to prevent against
/// ObjectDisposed exceptions.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Closing(object sender, CancelEventArgs e)
{
timer1.Enabled = false;
// if we have a request pending make sure to cancel it somehow.
while (webRequestPending)
{
Thread.Sleep(1000);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -