我正在尝试取消然后释放暂停的计时器但是当我调用'dispatch_release'时,我立即得到EXC_BAD_INSTRUCTION.
这不是一组有效的计时器吗?
定时器创建和暂停:
@interface SomeClass: NSObject { }
@property (nonatomic, assign) dispatch_source_t timer;
@end
// Class implementation
@implementation SomeClass
@synthesize timer = _timer;
- (void)startTimer
{
dispatch_queue_t globalQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0, 0, globalQ);
dispatch_time_t startWhen = dispatch_walltime(DISPATCH_TIME_NOW, NSEC_PER_SEC * 1);
dispatch_source_set_timer(_timer, startWhen, 1 * NSEC_PER_SEC, 5000ull);
dispatch_source_set_event_handler(_timer, ^{
// Perform a task
// If a particular amount of time has elapsed, kill this timer
if (timeConstraintReached)
{
// Can I suspend this timer within it's own …
Run Code Online (Sandbox Code Playgroud) 我试图理解为什么包含在其中的DispatcherTimer SingletonWithTimer
未在以下WPF应用程序中触发.我已经研究了这几天了,似乎无法深究它.此应用程序是我正在尝试修复的现有应用程序的简化部分.这个项目的Startup对象是WPFApplication5TimerTest.Program
.
控制台中的输出列表如下,问题很明显,因为输出中未显示" TimerTick " 字样:
Timer is initialized
'WpfApplication5TimerTest.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.Aero\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.Aero.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Sample thread
Sample thread
Sample thread
Sample thread
Sample thread
Sample thread
The thread '<No Name>' (0x10b0) has exited with code 0 (0x0).
Sample thread exiting!
Run Code Online (Sandbox Code Playgroud)
这是Program.cs:
using System;
namespace WpfApplication5TimerTest
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
AppObject = new App();
AppObject.Run();
} …
Run Code Online (Sandbox Code Playgroud) 我有一个DispatcherTimer我已初始化如下:
static DispatcherTimer _timer = new DispatcherTimer();
static void Main()
{
_timer.Interval = new TimeSpan(0, 0, 5);
_timer.Tick += new EventHandler(_timer_Tick);
_timer.Start();
}
static void _timer_Tick(object sender, EventArgs e)
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
_timer_Tick事件永远不会被解雇,我错过了什么吗?
我正在考虑使用Task.Delay()
不间断计时器,因为它更简单,更易读.
由于我是.NET的新手,我发现两个代码之间没有显着差异.你能告诉我他们之间的区别(如果有的话)吗?
// Create variable at some place
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(5);
timer.Tick += timer_Elapsed;
timer.Start();
// Function other place
void timer_Elapsed(object sender, EventArgs e)
{
//Do stuff
}
Run Code Online (Sandbox Code Playgroud)
VS
// Every thing inside a function
async void TaskTimer()
{
while (true)
{
await Task.Delay(5000);
// Do stuff
}
}
Run Code Online (Sandbox Code Playgroud) 我没有考虑具体的情况,但是当我想到可能想要在DispatcherTimer上使用Timer的场景时,这个问题在我脑海中浮现.
在我必须执行计算密集型任务时,每当计时器事件触发,然后对UI 进行微小修改时,在性能方面会更好:
我的猜测是尽可能长时间地保持UI线程不被阻塞将增强用户体验.如果这是可取的,在这种情况下是否有任何我应该注意的捕获量?
编辑:
我觉得我的问题不够明确,所以我将尝试添加一个具体的,虽然是一个简单的例子.
假设我必须每2分钟读取一个大文件,当我完成后,我必须将一个项目添加到ListBox.假设读取/处理文件需要10-15秒,在此期间我没有UI工作.对于这样的事情,最好的方法是什么?
我再次为一个对你们所有人都很简单的问题道歉.我对Silverlight幕后的内容知之甚少.
我有一个图表应用程序(Visiblox),我用它作为滚动范围每20ms更新一次,添加和删除一个点.在伪代码中:
List<Point> datapoints= new List<Point>();
Series series = new Series(datapoints);
void timer_tick(){
datapoints.Add(new Point);
datapoints.RemoveAt(0);
// no need to refresh chart, it does refresh automatically
}
Run Code Online (Sandbox Code Playgroud)
在此图表工具中运行6系列时,它开始显示有点迟缓.将刻度线更改为10毫秒没有任何区别,图表以相同的速度更新,因此似乎20ms是速度限制(UI或图表?).
我试过CompositionTarget.Rendering
并得到了相同的结果:在20ms以下,速度没有差别.
然后我意外地启用了两个并且速度加倍.所以我测试了多个线程(2,3,4),速度加倍,三倍和四倍.这还没有锁,因为我甚至不知道生成锁定需要什么进程,但没有数据损坏和内存泄漏.
我的问题是为什么20ms的低速图表不能在10ms运行,但在多线程时速度非常快?UI刷新过程是否运行得更快?图表计算加倍了吗?或者单个DispatcherTimer的执行速度是否有限制?
谢谢!
编辑:我有嵌入式编码的背景,所以当我想到线程和时序时,我立即想到在硬件中切换一个引脚并连接一个范围来测量进程长度.我是C#中的线程新手,没有用于连接范围的引脚.有没有办法以图形方式查看线程时序?
考虑一下C#中的这对函数:
void func1() {
DispatcherTimer tmr = new DispatcherTimer();
tmr.Interval = TimeSpan.FromSeconds(5);
tmr.Tick += func2;
tmr.Start();
}
void func2(object a, EventArgs b) {
// Called every 5 seconds once func1() is called
}
Run Code Online (Sandbox Code Playgroud)
在调用func1()一次后,func2()从那时起每隔5秒被调用一次,即使我丢失了对我的计时器的引用,因为它的作用域仅限于func1().这意味着计时器显然仍在内存中,在调用func1()之后很长时间.我的问题是,如果我将它添加到func2():
void func2(object a, EventArgs b) {
// Called every 5 seconds once func1() is called
((DispatcherTimer)a).Stop()
}
Run Code Online (Sandbox Code Playgroud)
很快就会通过垃圾收集来获取计时器,还是会继续留在内存中直到程序退出?如果它留在内存中,我如何手动标记它(或做类似的事情)?
我有一个第二个问题(如果你愿意回答)是在这种情况下,如果一个普通的Timer会有完全相同的行为,或者我应该知道一个显着的差异.
谢谢!
在我的WPF应用程序中,用户按下按钮以启动平滑旋转的3D模型,然后让按钮停止旋转.
为此,我创建了一个DispatcherTimer:
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += new EventHandler( timer_Tick );
timer.Interval = new TimeSpan( 0, 0, 0, 0, 30 );
Run Code Online (Sandbox Code Playgroud)
当按下按钮时,我打电话timer.Start()
,当按钮松开时我打电话timer.Stop()
.
该timer_Tick
函数更改模型的旋转:
void timer_Tick( object sender, EventArgs e )
{
spin = ( spin + 2 ) % 360;
AxisAngleRotation3D rotation = new AxisAngleRotation3D( new Vector3D( 0, 1, 0 ), spin );
Transform3D rotate = new RotateTransform3D( rotation );
model2.Transform = rotate;
}
Run Code Online (Sandbox Code Playgroud)
我注意到的是,模型在大多数情况下平滑旋转,但经常冻结和口吃,暂停不同的持续时间,有时高达1/4秒.
有没有办法让这更顺畅?我理解通过使用DispatcherTimer(而不是System.Timers.Timer),回调发生在UI线程上.但是为了运行这条线,我必须处于UI威胁之中
model2.Transform = rotate;
Run Code Online (Sandbox Code Playgroud)
我已经阅读了有关在其他线程上获取计时器回调的各种方法.但似乎最终我必须与UI线程同步才能调用该行.如果我使用Invoke()从System.Timers.Timer回调线程编组到UI线程,那么会给整体更平滑的动画吗?它似乎不应该,因为它必须与UI线程同步,就像DispatcherTimer可能做的那样.对于这个问题,似乎任何设置 …
我的问题是如何在Dispatchertimer中发送一些参数.这里的事件是代码:我想要的是在dispatcheTimer_Tick上接收一个整数值
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
//.Text = DateTime.Now.Second.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我想做的就是这样的
private void dispatcherTimer_Tick(object sender, EventArgs e,int a)
{
//.Text = DateTime.Now.Second.ToString();
}
Run Code Online (Sandbox Code Playgroud)
如何从调用点发送值?
问题1:嗨,我想知道有没有办法可以处理或终止调度程序计时器的对象并创建一个同名的新对象?
Qurstion 2:如果将其设置为Public,我可以访问其他类中的调度程序计时器对象
dispatchertimer ×10
c# ×8
wpf ×5
silverlight ×2
.net ×1
async-await ×1
delay ×1
ios ×1
ios5 ×1
iphone ×1
smooth ×1
task ×1
timer ×1
visiblox ×1