问题描述
编辑:我接过的帮助:的(它递减计数器使用,但它不是我的应用程序工作)
Edited: I took help from :http://forum.codecall.net/topic/65434-c-working-with-timers/ (in it decremented counter is used but its not working in my app)
我开发在C#中,我实现了一个计时器的应用程序。
I am developing an application in C# in which I have implemented a Timer.
我有一些文本字段和两个按钮:提交和更新。
我实现了一个计时器从工具栏更新按钮。
I have some text field and two buttons : submit and update.I have implemented a timer from toolbar to update button .
我想这个定时器运行10分钟,然后禁用更新按钮。但是,presently其运行只需2分钟。
I wanted this timer to run for 10 minutes and then Disable the Update button. But presently its running for just 2 minutes.
请指导我在哪里,我做错了。
Please Guide me where I am doing wrong.
按钮code:
<asp:Button ID="Btnsave" runat="server" CssClass="bt3dbuttons"
onclick="Btnsave_Click" OnClientClick="return confirm('Data Submitted')"
Text="Submit" Width="77px" />
<asp:Timer ID="Timer2" runat="server" ontick="Timer2_Tick">
</asp:Timer>
<asp:Button ID="Butnupdate" runat="server" CssClass="btupbuttons"
onclick="Btnupdate_Click" Text="Update" visible="false" Width="85px" />
下面是code定时器:
Here is the code for timer:
private System.Timers.Timer aTimer = new System.Timers.Timer(600000)
{ AutoReset = false };
protected void Timer2_Tick(object sender, EventArgs e)
{
aTimer = new System.Timers.Timer(600000);
aTimer.Interval = 600000;
double counter = aTimer.Interval;
counter++;
if (counter >= 600000)
{
Butnupdate.Enabled = false;
MessageBox.Show("Time Up!");
}
}
code的更新按钮:
Code for Update Button:
保护无效Btnupdate_Click(对象发件人,EventArgs的发送)
{
protected void Btnupdate_Click(object sender, EventArgs e) {
string id = Id.Text.Trim();
string name = Name.Text;
string project = Project.Text;
string result = Total.Text;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CM_Connection"].ConnectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
try
{
//lets check that the user typed the first number
if (Viva.Text.Length > 1)
{
VivaLabel.Text = "Please enter a valid number to add.";
return;
}
//lets check that the user typed the second number
else if (Presentation.Text.Length > 1)
{
PresentationLabel.Text = "Please enter a valid number to add.";
return;
}
else if (Confidence.Text.Length > 1)
{
ConfidenceLabel.Text = "Please enter a valid number to add.";
return;
}
else if (System.Text.Length > 1)
{
SystemLabel.Text = "Please enter a valid number to add.";
return;
}
//Now we have valid inputs
//Lets put them into integer values
int number1 = int.Parse(Viva.Text);
int number2 = int.Parse(Presentation.Text);
int number3 = int.Parse(Confidence.Text);
int number4 = int.Parse(System.Text);
//Now lets add the numbers
int total = number1 + number2 + number3 + number4;
//lets place it into the TextBox3
Total.Text = total.ToString();
// cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = @"UPDATE Result SET Name = @name, Project = @project, Result = @result WHERE ID = @id";
con.Open();
cmd.Parameters.AddWithValue("@id", Id.Text.ToString());
cmd.Parameters.AddWithValue("@name ", Name.Text.ToString());
cmd.Parameters.AddWithValue("@project ", Project.Text.ToString());
cmd.Parameters.AddWithValue("@result ", Total.Text.ToString());
cmd.ExecuteNonQuery();
}
catch (Exception ex1)
{
//Report error to user in the bottom Label
MessageBox.Show(ex1.Message);
}
}
}
感谢
推荐答案
问题已经按照code解决:
Problem has been solved by following code:
创建计时器(默认可以从工具箱中使用)
设置计时器的时间间隔属性
Create the timer ( default can be used from toolbox)set the timer interval property
<asp:Timer ID="Timer2" runat="server" ontick="Timer2_Tick"
Interval="600000">
</asp:Timer>
信息参见:
定时器创建事件处理程序:
Create the event handler for timer :
protected void Timer2_Tick(object sender, EventArgs e)
{
if (timeLeft > 0)
{
// Display the new time left
// by updating the Time Left label.
timeLeft = timeLeft - 1;
Label1.Text = timeLeft + " seconds";
}
else
{
// If the user ran out of time, stop the timer, show
// a MessageBox, and fill in the answers.
Timer2.Enabled = false;
Label1.Text = "Time's up!";
MessageBox.Show("You didn't finish in time.", "Sorry");
Butnupdate.Enabled = false;
}
在使用上你希望你的计时器启动按钮,如下:
use the following in the button on which you want your timer to start:
timeLeft = 600;
Label1.Text = DateTime.Now.ToString("HH:mm:ss tt");
Timer2.Enabled = true;
如果你想显示当前系统时间或当前时间使用
If you want to show the current system time or current time use
Label1.Text = DateTime.Now.ToString("HH:mm:ss tt");
我宣布它为标签,如何做到这一点及以上,请看看这里:
<一href=\"http://msdn.microsoft.com/en-us/library/dd492144.aspx?cs-save-lang=1&cs-lang=csharp#$c$c-snippet-2\" rel=\"nofollow\">http://msdn.microsoft.com/en-us/library/dd492144.aspx?cs-save-lang=1&cs-lang=csharp#$c$c-snippet-2
在这里,我们去一个计时器,10分钟后关闭按钮:)
希望它可以帮助别人,以及:)
Here we go a timer to disable button after 10 minutes :)Hopes it helps somebody as well :)
这篇关于运行10分钟计时器,然后禁用按钮C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!