检测Silverlight应用程序的空闲状态的最佳方法是什么?我现在已经在网上阅读了不少文章,通常它们都是针对wpf /移动应用程序等.
我创建了一个DispatcherTimer,它在5分钟后锁定屏幕,似乎我必须在每个屏幕中转到每个小部件(我的应用程序有大约4-5个屏幕)并添加一个mousebuttondown或mouseenter eventhandler来重置此计时器.这似乎没有效率,但只是将处理程序添加到layroot也没有帮助.
任何有用的建议?
谢谢
做这样的事情是安全的:
private void MyFunction()
{
    DispatcherTimer timer = new DispatcherTimer();
    timer.Interval = new TimeSpan(0, 0, 1);
    timer.Tick += (object sender, object e) =>
    {
        timer.Stop();
        // Some code here
    };
    timer.Start();
}
我有一个使用的互联网广播应用程序BackgroundAudioPlayer.
我需要音频播放代理中的一个计时器,它将更新从Internet广播电台的API中提取的BAP当前播放曲目的曲目标题.
添加DispatcherTimer到音频播放代理程序给我一个跨线程异常,并使用:
Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                // Code
            });
没工作.
我需要这里的代码,因为如果我将更新代码放在应用程序本身,当用户导航离开应用程序时,更新会停止(与Windows 8的行为非常不同).
我不能使用预定代理,因为它们每30分钟只运行一次(IIRC).
这是可能的还是不能在Windows Phone上完成?
c# dispatchertimer background-agents background-audio windows-phone-8
我有一个WPF应用程序,它使用DispatcherTimer更新时钟滴答.
但是,在我的应用程序运行约6小时后,时钟指针角度不再变化.我已经验证DispatcherTimer仍在使用Debug触发,并且角度值仍在更新,但屏幕渲染不反映更改.
我还验证了使用WPFPerf工具Visual Profiler,未标记时间,Tick(时间管理器)和AnimatedRenderMessageHandler(媒体内容)都在逐渐增长,直到消耗了近80%的CPU,但内存运行稳定.
hHandRT.Angle是对RotateTransform的引用
hHandRT = new RotateTransform(_hAngle);
此代码可以完美地运行大约5个小时的直接运行但在此之后它会延迟并且角度变化不会呈现给屏幕.有关如何解决此问题的任何建议或您可能知道的任何可能的解决方案.
.NET 3.5,Windows Vista SP1或Windows XP SP3(两者都显示相同的行为)
编辑:添加时钟节拍功能
//In Constructor
...
_dt = new DispatcherTimer();
_dt.Interval = new TimeSpan(0, 0, 1);
_dt.Tick += new EventHandler(Clock_Tick);
...
 private void Clock_Tick(object sender, EventArgs e)
        {
            DateTime startTime = DateTime.UtcNow;
            TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById(_timeZoneId);
            _now = TimeZoneInfo.ConvertTime(startTime, TimeZoneInfo.Utc, tst);
            int hoursInMinutes = _now.Hour * 60 + _now.Minute;
            int minutesInSeconds = _now.Minute * 60 + _now.Second;
            _hAngle = (double)hoursInMinutes * 360 / 720;
            _mAngle = …我正在尝试使用调度计时器测量按键事件之间的经过时间(以毫秒为单位),但是当声明调度计时器的间隔为1毫秒然后建立tick事件时,它不会每隔1毫秒触发一次,而是像10- 100毫秒(猜测).如果此刻度事件未按时触发,如何准确测量时间(以毫秒为单位)?我在Silverlight中这样做,似乎无法访问System.Timers.System.Threading.Timer似乎也发生了同样的情况.
以下是代码的基础知识:
public void StartTimer(object o, RoutedEventArgs sender)
{
    System.Windows.Threading.DispatcherTimer myDispatcherTimer = new  
    System.Windows.Threading.DispatcherTimer();
    myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1); // 1 Milliseconds 
    myDispatcherTimer.Tick += new EventHandler(Each_Tick);
    myDispatcherTimer.Start();
}
// A variable to count with.
int i = 0;
public void Each_Tick(object o, EventArgs sender)
{
    i++;
}
public void keypress(object s, EventArgs args)
{
    label1.contents = i.ToString();
    i = 0;
}
有任何想法吗?
在Silverlight应用程序中,我有一段代码必须每500毫秒运行一次.我计划使用DispatcherTimer来实现这一点(参见下面的代码).
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 0, 0, 500); // 500 Milliseconds
dt.Tick += new EventHandler(dt_Tick);
dt.Start();
但是,可能会发生代码块执行时间超过500毫秒(代码块执行一些Web服务调用).如何确保当前正在进行呼叫,DispatcherTimer不会触发另一个事件?有什么选择,最好的方法是什么?使用锁?
我DispatcherTimer在我的代码中运行,每30秒触发一次,从服务器更新系统状态.即使我正在调试我的服务器代码,计时器也会在客户端中触发,因此如果我已经调试了5分钟,我可能会在客户端中进行十几次超时.最后决定我需要修复这个问题从而寻求使一个更加async/ await友好DispatcherTimer.
DispatcherTimer必须是可配置的,无论它是否是可重入的(即如果任务已经运行,则不应该再次运行它)await完成任务IsRunning为UI提供可绑定属性我最近重构了我的WPF代码,现在我的DispatcherTimer停止了解雇.我在这里检查了其他类似的帖子,但他们似乎都有错误的调度程序线程集的问题,我试过...
我的代码看起来像这样:
class MainWindow : Window
{
    private async void GoButton_Click(object sender, RoutedEventArgs e)
    {
        Hide();
        m_files = new CopyFilesWindow();
        m_files.Show();
        m_dispatcherTimer = new DispatcherTimer();
        m_dispatcherTimer.Tick += dispatcherTimer_Tick;
        m_dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 250);
        m_dispatcherTimer.Start();
        await SomeLongRunningTask();
        m_files.Hide();
        Show();
    }
(当前类是我的主要Window对象,我在文件复制期间隐藏它.CopyFilesWindow是一个简单的Xaml窗口,其中包含我修改的控件... CopyFilesWindow本身绝对没有.)
基本上,我等待一个长时间运行的任务(复制一堆大文件),我的DispatcherTimer应该更新dispatcherTimer_Tick中的进度.但是,我在该函数上设置了一个断点,它没有被击中.
我也尝试使用构造函数设置Dispatcher,如下所示:
        m_dispatcherTimer = new DispatcherTimer(DispatcherPriority.Normal, m_files.Dispatcher);
        m_dispatcherTimer = new DispatcherTimer(DispatcherPriority.Normal, this.Dispatcher);
但这些都不会改变这种行为......它仍然不会触发.
我在这做错了什么?
我很好奇为什么调度程序计时器在控制台模式下不起作用。我创建了一个简单的警报,它在计时器达到极限时会执行某些操作。
您可以将调度程序计时器与UnitTest一起使用还是在控制台模式下使用?当我以某种形式运行它时,DailyAlarm起作用。
这是我调用计时器的代码
[TestClass]
public class UnitTest1
{
    bool runTest = true;
    [TestMethod]
    public void TestDailyAlarm()
    {
        DateTime alarmTime = new DateTime();
        alarmTime= DateTime.Now;
        alarmTime = alarmTime.AddSeconds(5);
        // MessageBox.Show(alarmTime.ToString());
        DailyAlarm alarm = new DailyAlarm(alarmTime);
        alarm.DailyAlarmEvent += alarm_DailyAlarmEvent;
        alarm.Start();
        while (runTest)
        {
            System.Threading.Thread.Sleep(1000);
        }
    }
    void alarm_DailyAlarmEvent(EventArgs e)
    {            
        MessageBox.Show("Alarm On");
        runTest = false;
    }
}
这是我的计时器代码
public class DailyAlarm
{
    #region Timer
    DispatcherTimer timer;
    #endregion
    #region AlarmTime
    DateTime _alarmTime;
    #endregion
    #region Event
    public delegate void DailyAlarmHandler(EventArgs e);
    public event DailyAlarmHandler …我发现了几个“堆栈溢出”问题以及一些已经涉及该主题的博客文章,但是不幸的是,这些问题都无法满足我的需求。我将从一些示例代码开始,以显示我想要完成的工作。
using System;
using System.Security.Permissions;
using System.Threading.Tasks;
using System.Windows.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MyApp
{
    [TestClass]
    public class MyTests
    {
        private int _value;
        [TestMethod]
        public async Task TimerTest()
        {
            _value = 0;
            var timer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(10)};
            timer.Tick += IncrementValue;
            timer.Start();
            await Task.Delay(15);
            DispatcherUtils.DoEvents();
            Assert.AreNotEqual(0, _value);
        }
        private void IncrementValue(object sender, EventArgs e)
        {
            _value++;
        } 
    }
    internal class DispatcherUtils
    {
        [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        public static void DoEvents()
        {
            var frame = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), …dispatchertimer ×10
c# ×9
wpf ×5
silverlight ×3
.net ×1
.net-4.5 ×1
asynchronous ×1
cpu ×1
dispatcher ×1
python-idle ×1
timer ×1
unit-testing ×1