Trigger callback at specific times in Bass - c#

I'm new to the Bass library but is it possible to pass in a list of times that trigger an event when the song has reached that time? For example, if I set a trigger at 30 seconds, when the song reaches 30 seconds, can I call an event?

I do that using BASS_ChannelSetSync with BASS_SYNC_POS, I use VB not C# but perhaps may help you... and excuse my english
I define a SYNPROC pointing to the the sub that manage the event "time reached"
Private M_SyncMarca As Un4seen.Bass.SYNCPROC = New Un4seen.Bass.SYNCPROC(AddressOf sync_marcas)
Private M_HandlerMarcas As Integer
then, add the reference to your channel
M_HandlerMarcas = Bass.BASS_ChannelSetSync(pista_actual, BASSSync.BASS_SYNC_POS, marca_fin, M_SyncMarca, IntPtr.Zero)
and finaly, the subrutine to manage wath you want when time is reached.
Private Sub sync_marcas()
Me.Invoke(New System.Windows.Forms.MethodInvoker(AddressOf manejar_marca))
End Sub
there where may be painful with threads, that's why I point to another sub rutine.
I hope this can help you

Related

coded ui control polling instead of wait for control exists

I have coded ui tests with a lot of WaitForControlExists in them. This causes my tests to run slow.
Basically if Playback.PlaybackSettings.SearchTimeout = 30000; and I have uicontrol.WaitForControlExists() it takes 30 seconds to get feedback from the method, even if the control is shown after 1 second.
Now I want to find out if there's a way to exit the WaitForControlExist as soon as the control exists? Say, I "Poll the Control Exists" instead of "Wait For Control Exists".
I will set the poll timer to 1 second. Meaning I check every second if the control exists. If it SHOWS after 2 seconds (or whatever less than 30 seconds) return true and stop Polling, if not keep trying every second when its been 30 seconds, quit and return false.
You can set it up as follows:
var exists = uiControl.WaitForControlExists(100);
var counter = 0;
while(!exists)
{
Playback.Wait(1000);
counter++;
exists = uiControl.WaitForControlExists(100);
if(counter>30)
break;
}
Also you can try using WaitForControlReady
use uiControl.WaitForControlCondition(control => control.Condition,timeout);
With this you can mix and match the control conditions that you want to be met before proceeding with the execution.
The most useful one for me is the State of the control and the styles.
have fun :)

.net reactive extensions - block an event for a time after each occurrence

I have an event that normally should not come more than 1/second.
But it may happen from time to time that is comes 2 o 3 times in one second.
So I want to do something like this with reactive extensions:
If an event comes -> process it.
If any other come in the next x seconds ignore them.
Than if after the x seconds comes a another event -> process it
and so on.
Id do not want to use the Throttle function since I want to respond as fast as possible if the events come in normal timing.
Is there any built in function in the Rx that does this?

C# Calling method every minute at 55 second mark

I've been given a task to write a program to count how many page views are requested from our site. My current approach is to get data from google analytics Real Time API, which works to my suprise.
My problem is that to get pageviews every minute I need to poll data from google API twice (cause it returns sum of last 29 minutes + a value from a timer that resets every minute). After I set up 'the point of reset', lets just say, on a 55th second every minute, I poll data on 56th and later on at 53th second, which gives me relatively good estimation of new users / page views requested.
So this is my current approach:
static System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.AutoReset = false;
myTimer.Interval = interval();
myTimer.Elapsed += myTimer_Elapsed2;
myTimer.Start();
static double interval()
{
return 1000 - DateTime.Now.Millisecond;
}
static void myTimer_Elapsed2(object sender, System.Timers.ElapsedEventArgs e)
{
if (DateTime.Now.Second == (resetPoint.Second - 1) % 60 && warden)
{
DoStuff(); //mostly inserting google API data to database
}
else if (DateTime.Now.Second == (resetPoint.Second + 1) % 60) //so we dont get riddiculous 60 and above
{
//I get some data here, to later use it in DoStuff - mostly to calculate the gap between later
}
myTimer.Interval = interval(); //Because DoStuff() takes about 0.5 sec to execute, i need to recalibrate
myTimer.Start();
}
And it works really well, until it stops after about 2 hours, for now I have no idea why (program runs, just timer doesn't do its work anymore).
How do I make it stable for long periods of time? Best case scenario would be to run it for months without intervention.
# I edited to give a better sense what I'm actually doing
#END CREDITS
I ended up using two timers, each running in a one minute circle. And a database writing sometimes crashed and I didn't handle the corresponding exception properly. Log told me that google API functions from time to time tend to retrieve data a bit longer, which led to multiple Threading.Event calls and made my database data handling throw an exception hence stopping the timer.
I tried to use Quartz approach but its lack of human-friendly howto made me abandon this library.
You should really look into using Quartz.net for scheduling events on a reliable basis. Using a timer for scheduling is asking for stuff like race conditions, event skips and database deadlocks.
http://www.quartz-scheduler.net/ allows you to schedule events at precise intervals, independant of when your code starts or stops.
An example on how you use it: This will build a trigger that will fire at the top of the next hour, then repeat every 2 hours, forever:
trigger = TriggerBuilder.Create()
.WithIdentity("trigger8") // because group is not specified, "trigger8" will be in the default group
.StartAt(DateBuilder.EvenHourDate(null)) // get the next even-hour (minutes and seconds zero ("00:00"))
.WithSimpleSchedule(x => x
.WithIntervalInHours(2)
.RepeatForever())
// note that in this example, 'forJob(..)' is not called
// - which is valid if the trigger is passed to the scheduler along with the job
.Build();
scheduler.scheduleJob(trigger, job);
http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/simpletriggers.html has a few examples. I really URGE you to use it, since it will severely simplify development.
The .NET timer is reliable. That is, it won't just stop working randomly for no apparent reason.
Most likely, something in your timer event handler is throwing an exception, which is not surfaced because System.Timers.Timer squashes exceptions. As the documentation states:
The Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event. This behavior is subject to change in future releases of the .NET Framework.
That bit about the behavior being "subject to change" has been there since at least .NET 2.0.
What I think is happening is that the timer calls your event handler. The event handler or one of the methods it calls throws an exception, and the timer just drops it on the floor because you don't handle it.
You need to put an exception handler in your myTimer_Elapsed2 method so that you can at least log any exceptions that crop up. With the information provided from the exception log, you can probably identify what the problem is.
Better yet, stop using System.Timers.Timer. Use System.Threading.Timer instead.
Finally, there's no way that your code as written will reliably give you a timer tick at exactly 55 seconds past the minute, every minute. The timer isn't exact. It will be off by a few milliseconds each minute. Over time, it's going to start ticking at 54 seconds (or maybe 56), and then 53 (or 57), etc. If you really need this to tick reliably at 55 seconds past the minute, then you'll need to reset the timer after every minute, taking into account the current time.
I suspect that your need to check every minute at exactly the 55 second mark is overkill. Just set your timer to tick every minute, and then determine the exact elapsed time since the last tick. So one "minute" might be 61 or 62 seconds, and another might be 58 or 59 seconds. If you store the number of requests and the elapsed time, subsequent processing can smooth the bumps and give you a reliable requests-per-minute number. Trying to gather the data on exact one-minute boundaries is going to be exceedingly difficult, if even possible with a non-real-time operating system like Windows.

Timer Control in C# windows Form

I am a beginner with windows forms (visual studio 2010) , and wanted to know is there any way to get the value of a timer at any instant of time ?
For starters i want to lets say display the value of a timer in a text box e.g if my timer is from 0 to 100, i want to show these values 0 to 100 all the way in a textbox.
Secondly, is there a timer variable which may give the number of times my timer has ticked since it was started?
It sounds like you want a Stopwatch, as mentioned in a comment.
You can use the Start command whenever you start the Timer (if you find you even still need that), then check Elapsed when you want to know how long has passed, for instance in the Timer.Tick event.
As for your second question, you'll need a private field to track your tick count, but that's easy enough. Just increment the number on each tick and forget about it. No big deal.
There are multiple ways to solve your problem, partly because it is so open ended.
System.Windows.Forms.Timer
You would need to write an event handler which would be executed whenever the timer ticks. This timer must be hosted on a form.
System.Threading.Timer
You would need to specify a callback delegate which executes when the timer ticks. This doesn't need a form to run.
System.Diagnostics.Stopwatch
This is a class which can be started and stopped, and can tell you the amount of time elapsed since it was started.
System.Threading.Timer example to make a simple counter
Private t As System.Threading.Timer
Private counter As Long = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
t = New System.Threading.Timer(Sub() counter += 1)
t.Change(1000, 1000) ' 1000 (ms): start after 1 second, 1000 (ms): 1 second interval
End Sub
Public ReadOnly Property Counts As Long
Get
Return counter
End Get
End Property
Public Sub ResetCounter()
t.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite)
counter = 0
t.Change(1000, 1000)
End Sub

Dequeueing an class and individual values c#

This is a follow up from yesterday's question
I have 2 queue, they contain 5 elements each called player 1 and player 2.
They were queued using something like the following
player2.Enqueue(chuckcards[i]);
Chuck cards is a class that has several different data members including 6 ints and one string. Now I wish to dequeue one item give the values to a labels (Multipul one per data member) and the string to a picture box (I don't need help with that). But I would like to know how I get the values from the dequeue. I would also like to be able to say re queue on the other players queue. So I might use the peak operation to get the values then if the outcome is successful just move it off the end of queue 1 and onto queue 2.
Hope that's clear.
Any help would be appreciated. I have searched around but found no real answer yet.
Queue<Foo> firstQueue = new Queue<Foo>();
Queue<Foo> secondQueue = new Queue<Foo>();
//todo populate
var item = firstQueue.Dequeue();
item.DoStuff();
secondQueue.Enqueue(item);

Categories

Resources