读书人

小白求教DispatcherTimer定时刷新有

发布时间: 2012-12-14 10:33:07 作者: rapoo

小白求教,DispatcherTimer定时刷新问题。。。
小弟现在想让 手机 从某个网站定时 提取数据,并在一个textbox中显示出来。。然后代码如下


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Windows.Threading;
namespace WebClientHttpWebRequest
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(10);
timer.Tick += timer_Tick;
timer.Start();
}


private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
DoWebClient();
}

private void DoWebClient()
{
}
void timer_Tick(object sender, EventArgs e)
{

WebClient webClient = new WebClient();
webClient.OpenReadAsync(new Uri("http://quote.zhijinwang.com/xml/ag.txt"));
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
}


void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
using (StreamReader reader = new StreamReader(e.Result))
{
string contents = reader.ReadToEnd();
int begin = contents.ToString().IndexOf("t");
int end = contents.ToString().IndexOf("&");
webClientTextBlock.Text = contents.ToString().Substring(begin + 5, end-begin-5 );


string[] arry = contents.Split('|');
string note =arry[arry.Length - 8];
textBox1.Text = note;

}
}
}
}


运行效果只是第一次延时10S显示,该如何改让webClientTextBlock和textBox1每隔10s更新一次数据,还有如果加一个立即刷新按钮button1_Click代码应该怎么写。
可能问题有点白、有点多,希望大大们不要取笑。因为第一次接触编程,没有基础,语法什么的都不懂。上面的代码全是我从网上找来拼凑起来的。还有就是c#有什么好点的入门书籍也请大大一并推荐一下,小弟在此先行谢过了。。。

[解决办法]
运行效果只是第一次延时10S显示?
意思是Timer只起一起作用?

还有如果加一个立即刷新按钮button1_Click代码应该怎么写?
你可以在刷新按钮的事件里直接调用取数据的函数
[解决办法]
嗯,是的只是打开10s后显示一次,然后就一直没变过
引用:
运行效果只是第一次延时10S显示?
意思是Timer只起一起作用?

还有如果加一个立即刷新按钮button1_Click代码应该怎么写?
你可以在刷新按钮的事件里直接调用取数据的函数

[解决办法]
那应该不是Timer的问题,是你取数据的问题。可能是服务器限制了
[解决办法]
额,好像是取数据的问题,好像取数据的代码没加入10s一次循环重复取用。我把textbox里面数据删了,10s后又自动生成了,数据不变。
求大大教教我该怎么做才能10s一次循环取用,最好能帮忙写一段代码示例,小白实在是搞不定。谢谢,感激不尽。。。
引用:
那应该不是Timer的问题,是你取数据的问题。可能是服务器限制了

[解决办法]
你debug一下,跟踪一下你的代码运行流程先
[解决办法]
自己解决了,是缓存问题,把请求的网址加上个随机数变成动态的。

读书人网 >Windows Mobile

热点推荐