2

http://forum.codecall.net/topic/65434-c-working-with-timers/ (デクリメントされたカウンターが使用されていますが、私のアプリでは機能していません)

いくつかのテキスト フィールドと、送信と更新の 2 つのボタンがあります。ツールバーから更新ボタンまでのタイマーを実装しました。

このタイマーを 10 分間実行してから、更新ボタンを無効にしたかったのです。しかし、現在はわずか 2 分間しか実行されていません。

ボタンコード:

<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" />

タイマーのコードは次のとおりです。

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!");
   }
}

更新ボタンのコード:

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);
        }
    }
}
4

5 に答える 5

5

このコードをガイドとして使用してください

public class Timer1
{
    private static System.Timers.Timer aTimer;

    public static void Main()
    {
        // Create a timer with a ten second interval.
        aTimer = new System.Timers.Timer(600000);

        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        // Set the Interval to 10 minutes 
        aTimer.Interval = 600000;
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program.");
        Console.ReadLine();

        // If the timer is declared in a long-running method, use 
        // KeepAlive to prevent garbage collection from occurring 
        // before the method ends. 
        GC.KeepAlive(aTimer);
    }

    // Specify what you want to happen when the Elapsed event is  
    // raised. 
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Butnupdate.Enabled = false;
        MessageBox.Show("Time Up!");
    }
}

から取られた例

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

于 2013-08-16T21:26:41.753 に答える
3

asp:Timer の Interval プロパティを設定すると、ページがポストバックされ、ontick ハンドラーが呼び出されます。

<asp:Timer ID="Timer2" runat="server" ontick="Timer2_Tick" Interval="60000">
        </asp:Timer>

http://msdn.microsoft.com/en-us/library/system.web.ui.timer.interval.aspx

System.Timers.Timerページ処理に非常に長い時間がかからない限り、サーバーコードで何をしようとしているのかわかりません。

于 2013-08-16T21:51:33.157 に答える
1

タイマーがクラスの一部ではなく長時間実行されるメソッドである場合、タイマーがガベージコレクションを取得している可能性があります。投稿されたコードではわかりません。

タイマーを実装する方法のサンプルもあるTimer.Interval プロパティのドキュメントを参照してください。

于 2013-08-16T21:24:23.950 に答える
1

System.Timers.Timer クラスのドキュメント によると、定義された時間が経過すると非同期でイベントが発生します。

セットアップ コードは次のようになります。

        var aTimer = new System.Timers.Timer(10 * 60 * 1000);
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        aTimer.Enabled = true;

そして、イベント自体は次のようになります。

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        //Do your event management here.
    }

それがうまくいくことを願っています。

于 2013-08-16T21:26:26.753 に答える
0

問題は次のコードで解決されました:

タイマーを作成します (デフォルトはツールボックスから使用できます) タイマー間隔プロパティを設定します

<asp:Timer ID="Timer2" runat="server" ontick="Timer2_Tick" 
            Interval="600000">
        </asp:Timer>

参照については、http: //msdn.microsoft.com/en-us/library/system.web.ui.timer.interval.aspxを参照してください。

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;
            }

タイマーを開始するボタンで次を使用します。

timeLeft = 600;
                    Label1.Text = DateTime.Now.ToString("HH:mm:ss tt");
                    Timer2.Enabled = true;

現在のシステム時刻または現在の時刻を表示する場合は、

Label1.Text = DateTime.Now.ToString("HH:mm:ss tt");

私はそれをラベルに宣言し、その方法と上記をご覧ください: http://msdn.microsoft.com/en-us/library/dd492144.aspx?cs-save-lang=1&cs-lang=csharp#コード スニペット 2

ここで、10分後にボタンを無効にするタイマーに行きます:)それが誰かにも役立つことを願っています:)

于 2013-08-17T11:42:09.350 に答える