簡體   English   中英

如何通過Xamarin按鈕的Click事件停止C#計時器?

[英]How do I stop my C# timer with my Click event from a Xamarin button?

我有一個有效的計時器方法,該方法調用API函數以每3秒獲取新的股價數據。 該方法運行良好,並且每3秒鍾不斷更新Xamarin表單中的數據。 此方法稱為“ RefreshData()”,我從MainPage()類調用它。

我一直在嘗試尋找一種語法,以在Xamarin Button對象調用Click處理程序(“ Handle_Clicked”)時正確地停止計時器。

我嘗試了myTimer.Change方法,myTimer.Dispose方法和Timeout.Infinite方法。 它們似乎都很容易,但是我的語法一定是錯誤的,因為在Visual Studio中用紅色下划線表示的方法無法識別這些方法,或者它們會產生其他錯誤。

我正在尋找有關獲取正確的有效語法以關閉此計時器和/或可能再次將其重新打開的指南。

這是我在所有其他方面都可以使用的代碼片段...

謝謝你的幫助 :)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using Xamarin.Forms;
    using System.Timers;

    namespace MyTimerTest
    {

        [System.ComponentModel.DesignTimeVisible(false)]
        public partial class MainPage : ContentPage
        {
            public MainPage()
            {
                InitializeComponent();
                LoadData(); // Loads the initial data when the page loads
                RefreshData(); // Calls the timer method which refreshes the data
            }

            void RefreshData() // Sets the timer to call the UpdateData method which calls the LoadData method to get fresh data and update the UI with it
            {
                int seconds = 3 * 1000;
                var myTimer = new System.Threading.Timer(UpdateData, null, 0, seconds);
            }
            public void UpdateData(object o)
            {
                LoadData(); // <-- call for JSON API data, works fine and updates accordingly to the timer
            }

            void Handle_Clicked(object sender, EventArgs e)
            {
                myTimer.Dispose(); // <-- Error: myTimer doesn't exist in current context
                // myTimer.Change(Timeout.Infinite, Timeout.Infinite)
            }
      }

以這種方式聲明計時器,使其作用域僅限於LoadData方法

void RefreshData() // Sets the timer to call the UpdateData method which calls the LoadData method to get fresh data and update the UI with it
            {
                int seconds = 3 * 1000;
                var myTimer = new System.Threading.Timer(UpdateData, null, 0, seconds);
            }

而是在類級別(在特定方法之外)聲明它,以便可以在類中的任何位置訪問它

System.Threading.Timer myTimer;

scope是一個通用的C#概念(實際上是一個通用的編程概念),與Xamarin無關

而且,正如@enigmativity所提到的, System.Timers.Timer更加靈活。 但是,范圍界定問題仍然很重要。

謝謝大家……按照@Jason和@enigmativity,我采用了System.Timers.Timer方法,並使其正常工作,包括一個Xamarin按鈕,該按鈕將計時器(myTimer)的AutoReset屬性切換為true / false,有效地關閉刷新,然后重新打開。

這是我要工作的代碼...

     public MainPage()
            {

                InitializeComponent();
                LoadData(); // Loads the initial data when the page loads
                RefreshData(); // Calls the timer method which refreshes the data
            }

            System.Timers.Timer myTimer;

            void RefreshData() // Sets the timer to call the UpdateData method which calls the LoadData method to get fresh data and update the UI with it
            {
                // Create a timer with a three second interval.
                myTimer = new System.Timers.Timer(3000);
                // Hook up the Elapsed event for the timer. 
                myTimer.Elapsed += UpdateData;
                myTimer.AutoReset = true;
                myTimer.Enabled = true;
            }
            public void UpdateData(Object source, ElapsedEventArgs e)
            {
                LoadData(); // <-- call for JSON API data, works fine and updates accordingly to the timer
            }

            void Handle_Clicked(object sender, EventArgs e) // <-- toggles the UpdateData method call to ON/OFF
            {
                if(myTimer.AutoReset == true)
                {
                    myTimer.AutoReset = false;
                }
                else
                {
                    myTimer.AutoReset = true;
                }
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM