Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# System.ArgumentException: 'Value does not fall within the expected range.'

TableFlipGod

Bronze Coder
So I made a dll that gets the fps of an application. Whenever I put the code inside a console application it works just fine. But I put it in a dll it says
System.ArgumentException: 'Value does not fall within the expected range.'

Here is the entire class

[CODE lang="csharp" title="Class1"]using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using Microsoft.Diagnostics.Tracing.Session;
using System.Windows.Forms;
using System.Drawing;

namespace ConsoleApp1
{

public class Vars
{
public static double FrameRate;

public static string ProcessToHook;
}

//helper class to store frame timestamps
public class TimestampCollection
{
const int MAXNUM = 1000;

public string Name { get; set; }

List<long> timestamps = new List<long>(MAXNUM + 1);
object sync = new object();

//add value to the collection
public void Add(long timestamp)
{
lock (sync)
{
timestamps.Add(timestamp);
if (timestamps.Count > MAXNUM) timestamps.RemoveAt(0);
}
}

//get the number of timestamps withing interval
public int QueryCount(long from, long to)
{
int c = 0;

lock (sync)
{
foreach (var ts in timestamps)
{
if (ts >= from && ts <= to) c++;
}
}
return c;
}
}

public class WeirdStuff
{
//event codes (https://github.com/GameTechDev/PresentMon/blob/40ee99f437bc1061a27a2fc16a8993ee8ce4ebb5/PresentData/PresentMonTraceConsumer.cpp)
public const int EventID_D3D9PresentStart = 1;
public const int EventID_DxgiPresentStart = 42;

//ETW provider codes
public static readonly Guid DXGI_provider = Guid.Parse("{CA11C036-0102-4A2D-A6AD-F03CFED5D3C9}");
public static readonly Guid D3D9_provider = Guid.Parse("{783ACA0A-790E-4D7F-8451-AA850511C6B9}");

static TraceEventSession m_EtwSession;
static Dictionary<int, TimestampCollection> frames = new Dictionary<int, TimestampCollection>();
static Stopwatch watch = null;
static object sync = new object();

static void EtwThreadProc()
{
//start tracing
m_EtwSession.Source.Process();
}

static void OutputThreadProc()
{
while(Boolean.TrueString)
{

long t1, t2;
long dt = 2000;

lock (sync)
{
t2 = watch.ElapsedMilliseconds;
t1 = t2 - dt;

foreach (var x in frames.Values)
{
if (x.Name == Vars.ProcessToHook)
{
//get the number of frames
int count = x.QueryCount(t1, t2);
Vars.FrameRate = (double)count / dt * 1000.0;

}
}
}

Console.WriteLine();
Console.WriteLine("Press any key to stop tracing...");
Thread.Sleep(100);
}
}

public static void Main(string[] argv)
{
//create ETW session and register providers
m_EtwSession = new TraceEventSession("mysess");
m_EtwSession.StopOnDispose = true;
m_EtwSession.EnableProvider("Microsoft-Windows-D3D9");
m_EtwSession.EnableProvider("Microsoft-Windows-DXGI");

//handle event
m_EtwSession.Source.AllEvents += data =>
{
//filter out frame presentation events
if (((int)data.ID == EventID_D3D9PresentStart && data.ProviderGuid == D3D9_provider) ||
((int)data.ID == EventID_DxgiPresentStart && data.ProviderGuid == DXGI_provider))
{
int pid = data.ProcessID;
long t;

lock (sync)
{
t = watch.ElapsedMilliseconds;

//if process is not yet in Dictionary, add it
if (!frames.ContainsKey(pid))
{
frames[pid] = new TimestampCollection();

string name = "";
var proc = Process.GetProcessById(pid);
if (proc != null)
{
using (proc)
{
name = proc.ProcessName;
}
}
else name = pid.ToString();

frames[pid].Name = name;
}

//store frame timestamp in collection
frames[pid].Add(t);
}
}
};

watch = new Stopwatch();
watch.Start();

Thread thETW = new Thread(EtwThreadProc);
thETW.IsBackground = true;
thETW.Start();

Thread thOutput = new Thread(OutputThreadProc);
thOutput.IsBackground = true;
thOutput.Start();

Console.ReadLine();

m_EtwSession.Dispose();
}
}
}[/CODE]

I would love to hear any possible answers that could help with this!

It errors out at line 83.







I solved this (yay). So if anyone of you have an issue with this type, make sure you check if the session exists if so then kill it else just run it as normal. Here is my new code.

[CODE lang="csharp" title="Modified Class1"]using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Microsoft.Diagnostics.Tracing.Session;

namespace FramerateLibrary
{

public class Vars
{
public static double FrameRate;

public static string ProcessToHook;

public static bool HasStarted = false;
}

//helper class to store frame timestamps
public class TimestampCollection
{
const int MAXNUM = 1000;

public string Name { get; set; }

List<long> timestamps = new List<long>(MAXNUM + 1);
object sync = new object();

//add value to the collection
public void Add(long timestamp)
{
lock (sync)
{
timestamps.Add(timestamp);
if (timestamps.Count > MAXNUM) timestamps.RemoveAt(0);
}
}

//get the number of timestamps withing interval
public int QueryCount(long from, long to)
{
int c = 0;

lock (sync)
{
foreach (var ts in timestamps)
{
if (ts >= from && ts <= to) c++;
}
}
return c;
}
}

public class WeirdStuff
{
//event codes (https://github.com/GameTechDev/PresentMon/blob/40ee99f437bc1061a27a2fc16a8993ee8ce4ebb5/PresentData/PresentMonTraceConsumer.cpp)
public const int EventID_D3D9PresentStart = 1;
public const int EventID_DxgiPresentStart = 42;

//ETW provider codes
public static readonly Guid DXGI_provider = Guid.Parse("{CA11C036-0102-4A2D-A6AD-F03CFED5D3C9}");
public static readonly Guid D3D9_provider = Guid.Parse("{783ACA0A-790E-4D7F-8451-AA850511C6B9}");

static TraceEventSession m_EtwSession;
static Dictionary<int, TimestampCollection> frames = new Dictionary<int, TimestampCollection>();
static Stopwatch watch = null;
static object sync = new object();

static void EtwThreadProc()
{
//start tracing
m_EtwSession.Source.Process();
}

static void OutputThreadProc()
{
while (true == true)
{

long t1, t2;
long dt = 2000;

lock (sync)
{
t2 = watch.ElapsedMilliseconds;
t1 = t2 - dt;

foreach (var x in frames.Values)
{
if (x.Name == Vars.ProcessToHook)
{
//get the number of frames
int count = x.QueryCount(t1, t2);
Vars.FrameRate = (double)count / dt * 1000.0;
}
}
}
Thread.Sleep(100);
}
}

public static void Main()
{
if(Vars.HasStarted == true)
{
Killses();
}
else
{
Vars.HasStarted = true;
//create ETW session and register providers
m_EtwSession = new TraceEventSession("mysess");
m_EtwSession.StopOnDispose = true;
m_EtwSession.EnableProvider("Microsoft-Windows-D3D9");
m_EtwSession.EnableProvider("Microsoft-Windows-DXGI");

//handle event
m_EtwSession.Source.AllEvents += data =>
{
//filter out frame presentation events
if (((int)data.ID == EventID_D3D9PresentStart && data.ProviderGuid == D3D9_provider) ||
((int)data.ID == EventID_DxgiPresentStart && data.ProviderGuid == DXGI_provider))
{
int pid = data.ProcessID;
long t;

lock (sync)
{
t = watch.ElapsedMilliseconds;

//if process is not yet in Dictionary, add it
if (!frames.ContainsKey(pid))
{
frames[pid] = new TimestampCollection();

string name = "";
var proc = Process.GetProcessById(pid);
if (proc != null)
{
using (proc)
{
name = proc.ProcessName;
}
}
else name = pid.ToString();

frames[pid].Name = name;
}

//store frame timestamp in collection
frames[pid].Add(t);
}
}
};

watch = new Stopwatch();
watch.Start();

Thread thETW = new Thread(EtwThreadProc);
thETW.IsBackground = true;
if (thETW.IsAlive)
{
thETW.Abort();
}
else
{
thETW.Start();
}

Thread thOutput = new Thread(OutputThreadProc);
thOutput.IsBackground = true;
if (thOutput.IsAlive)
{
thOutput.Abort();
}
else
{
thOutput.Start();
}
m_EtwSession.Dispose();
}

}
public static void Killses()
{
m_EtwSession.Flush();
m_EtwSession.Stop();

}
}
}[/CODE]
 
Last edited:

Buy us a coffee!

Back
Top Bottom