I've two Windows .NET libraries A and B. Library A has TimeBeginPeriod(1) and TimeEndPeriod(1) while Library B has TimeBeginPeriod(5) and TimeEndPeriod(5). I've notice lately (after their behavor change) that when the Library B will do TimeEndPeriod(5) Library A looses TimeBeginPeriod(1).
Can you have different time periods within the same application without the one affect the other? I mean I don't mind if the (5) will be affected by (1) but it is crucial that end(5) will not affect the begin(1)!
LibA TimeBeginPeriod(1)
.... LibB TimeBeginPeriod(5)
.... LibB TimeEndPeriod(5) <- Does this cancel TimeBeginPeriod(1)?
LibA TimeEndPeriod(1)
[Update]
Test Code
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
static long freq;
static void Main(string[] args)
{
QueryPerformanceFrequency(out freq);
Console.WriteLine("Begin");
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
TimeBeginPeriod(1);
Console.WriteLine("TimeBeginPeriod(1)");
Thread.Sleep(5000);
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
TimeBeginPeriod(15);
Console.WriteLine("TimeBeginPeriod(15)");
Thread.Sleep(5000);
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
Console.WriteLine("TimeEndPeriod(15)");
TimeEndPeriod(15);
Thread.Sleep(5000);
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
TimeEndPeriod(1);
Console.WriteLine("TimeEndPeriod(1)");
Console.WriteLine(CalcSleep1());
Console.WriteLine(CalcSleep1());
Console.WriteLine("End");
}
private static string CalcSleep1()
{
QueryPerformanceCounter(out long ticks1);
Thread.Sleep(1);
QueryPerformanceCounter(out long ticks2);
return (new TimeSpan((long)(1000 * 10000 * (ticks2 - ticks1) / (double)freq))).ToString();
}
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);
[DllImport("winmm.dll", EntryPoint = "timeBeginPeriod", SetLastError = true)]
public static extern uint TimeBeginPeriod(uint uMilliseconds);
[DllImport("winmm.dll", EntryPoint = "timeEndPeriod", SetLastError = true)]
public static extern uint TimeEndPeriod(uint uMilliseconds);
[DllImport("winmm.dll", ExactSpelling = true)]
private static extern int timeGetDevCaps(ref TIMECAPS ptc, int cbtc);
[StructLayout(LayoutKind.Sequential)]
private struct TIMECAPS
{
internal int wPeriodMin;
internal int wPeriodMax;
}
}
}
Output
Begin
00:00:00.0130730
00:00:00.0154164
TimeBeginPeriod(1)
00:00:00.0019935
00:00:00.0019148
00:00:00.0019145
00:00:00.0019203
00:00:00.0023354
TimeBeginPeriod(15)
00:00:00.0019916
00:00:00.0018920
00:00:00.0019412
00:00:00.0019411
00:00:00.0019336
TimeEndPeriod(15)
00:00:00.0019918
00:00:00.0019300
00:00:00.0019671
00:00:00.0019524
00:00:00.0019399
TimeEndPeriod(1)
00:00:00.0152576
00:00:00.0148129
End
It seems that the in-between TimeBeginPeriod(15) / TimeEndPeriod(15) does not affect TimeBeginPeriod(1). Except if that happens only on different assemblies (didn't tested yet)