在C#中停止System.Timers

问题描述

我正在使用System.Timers,而计时器正在完美地工作,例如在Second 4,8,12,16上拍照。

但是,我希望它在Second 17上导航,但不会。

代码

public partial class CustomCameraPage : ContentPage
{
    private static Timer timer_click;
    int Seconds = 0;

    public CustomCameraPage()
    {
        timer_click = new Timer();
        timer_click.Interval = 1000;
        timer_click.Elapsed += OnTimedEvent;
        timer_click.Enabled = true;
        timer_click.AutoReset = true;
        timer_click.Start();

        InitializeComponent();
    }
        
    public void OnTimedEvent(object source,ElapsedEventArgs e)
    {
        Seconds++;

        if (Seconds == 4 || Seconds == 8 || Seconds == 12 || Seconds == 16) MessagingCenter.Send<object>(this,"A");
        if (Seconds == 17) Navigation.PushModalAsync(new FormPage());
    }   
}

解决方法

MainThread

上执行导航
return Transaction.aggregate([ { $match : { userId : userId,type:"Income",isActive:true} },{
        $group: {
            _id: "$type",amount: {
            $sum: "$amount"
             }
            }
            
} 
]).then((totalIncome,err)=> {
    // return (err) ? false : totalIncome
    console.log(totalIncome)
    if (err) {
        return false
    } else {
        User.findByIdAndUpdate(userId,{"$set" : {"totalIncome": totalIncome[0].amount }},{ "new": true })
        .then((transaction,err ) => {
            return (err) ? false : true
        })
    }
})