我有一个网络服务。
完全上传需要10-15秒。但有一段时间由于互联网连接速度慢,需要15秒以上。
我想打开一个消息框或弹出框,当服务超过15秒时打开该框,以显示消息“Slow Internet Connection”。
如何在我的代码中添加计时器,以便在Web服务需要超过15秒的时间加载时,在页面加载事件上打开弹出框(或警告说慢速连接)?
请帮帮我...
公共测验()
{
InitializeComponent();
DispatchTimer timer = new DispatcherTimer();
int serviceCount = 15;
Loaded += Quiz_Loaded;
}
protected void Quiz_Loaded(object sender, RoutedEventArgs e)
{
SystemTray.ProgressIndicator = new ProgressIndicator();
SystemTray.ProgressIndicator.IsIndeterminate = true;
SystemTray.ProgressIndicator.IsVisible = true;
SystemTray.ProgressIndicator.Text = "Loading Questions...";
pg2.Visibility = Visibility.Visible;
txtloading.Visibility = Visibility.Visible;
if (!timer.IsEnabled)
{
timer.Tick += timer_Tick;
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
}
}
void timer_Tick(object sender, object e)
{
serviceCount--;
if (serviceCount < 15)
{
PostData();
}
else
{
txtloading.Text = "slow connection.....";
}
}
private void PostData()
{
Uri uri = new Uri("my web service url");
string data = "device_id=test123&quiz_type=all";
WebClient wc = new WebClient();
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
wc.UploadStringAsync(uri, data);
wc.UploadStringCompleted += wc_UploadComplete;
}
public void wc_UploadComplete(object sender, UploadStringCompletedEventArgs e)
{
var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);
question = rootObject.questions;
DisplayQuestion();
SystemTray.ProgressIndicator.IsVisible = false;
pg2.Visibility = Visibility.Collapsed;
txtloading.Visibility = Visibility.Collapsed;
}
我希望在Web服务花费超过15秒的时间时调用消息块。 &安培;还有一个重试按钮,可以重新加载页面以从服务中获取所有数据..