UserAccountInfo时间倒计时

时间:2021-02-24 22:00:51

界面如下:

UserAccountInfo时间倒计时

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace WeChatTool
{
public partial class UserAccountInfo : UserControl
{
private TimeSpan timespan = new TimeSpan(2, 0, 0);
private double startTime;
System.Timers.Timer timer = new System.Timers.Timer();
public UserAccountInfo()
{
InitializeComponent();
timer.Enabled = true;
timer.Interval = 1000;
timer.Stop();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_tick);
}

private void UserAccountInfo_Load(object sender, EventArgs e)
{

}

private void btnProduceToken_Click(object sender, EventArgs e)
{
string token = "";
try
{
Uri url = new Uri("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx04ec9a465d7bbaf2&secret=a31fbf89aa42f984bb3f13a8bf5db949");
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";//默认方法是GET
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
token = reader.ReadToEnd();
this.rtbToken.Text = token;
Properties.Settings.Default.token = token;
startTime = timespan.TotalSeconds;
timer.Start();

}
catch (ProtocolViolationException ex)
{
MessageBox.Show("使用网络协议期间出错时引发的异常! " + ex.ToString(), "提示信息!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
//throw;
}
}

private void btnRefresh_Click(object sender, EventArgs e)
{
this.timer.Stop();
this.btnTimer.Text = null;
}

private void timer_tick(object sender, System.Timers.ElapsedEventArgs e)
{
setTimer(startTime);
}

public delegate void SetTimer(double timer);

private void setTimer(double timer)
{
if (this.InvokeRequired)
{
this.Invoke(new SetTimer(setTimer), timer);
}
else
{
DateTime datetime = new DateTime(2016,8,18,(int)(timer/3600),(int)((timer/60)%60),(int)((timer/1)%60));
this.btnTimer.Text = datetime.ToString();
startTime -= 1;
}
}

}
}