[SOLVED][C#]System.Timer not working | Set system reference correctly

Just a hint for setting system reference correctly if using C# & Visual Studio 2017.
Please check reply in this post for suitable solution.

— Original Post —
It seems System.Timer doesn’t work properly in fivem.
Sample code (just declare Timer)

public Timer missionTimer;

Exception output when loading resource:

[   2997938] Failed to instantiate instance of script ScriptAClient.ScriptAClient: System.TypeLoadException: Could not load type of field 'ScriptAClient.ScriptA:missionTimer' (2) due to: Could not resolve type with token 0100001a assembly:System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 type:System.Timers.Timer member:<none>

[   2997985]   at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00018] in <f0e9de1592254c6a8c7c298c474f20de>:0 

[   2998031]   at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) [0x000a8] in <f0e9de1592254c6a8c7c298c474f20de>:0 

[   2998078]   at System.RuntimeType.CreateInstanceSlow (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x00009] in <f0e9de1592254c6a8c7c298c474f20de>:0 

[   2998125]   at System.RuntimeType.CreateInstanceDefaultCtor (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x00027] in <f0e9de1592254c6a8c7c298c474f20de>:0 

[   2998172]   at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00020] in <f0e9de1592254c6a8c7c298c474f20de>:0 

[   2998219]   at System.Activator.CreateInstance (System.Type type) [0x00000] in <f0e9de1592254c6a8c7c298c474f20de>:0 

[   2998266]   at CitizenFX.Core.InternalManager.CreateAssemblyInternal (System.String assemblyFile, System.Byte[] assemblyData, System.Byte[] symbolData) [0x000bb] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\InternalManager.cs:103 

Does anyone know how to use timer in c#?
Thanks in advanced.

You can use Stopwatch or make use of GetGameTimer() (call when you start it, store it and at the end take the difference of current GetGameTimer() and stored one)

Stopwatch doesn’t exist on the client either. You have to use GetGameTimer.

Thanks, I’m trying to use GetGameTImer() instead

How do you known/check whether specific namespace/class could be used in fivem C# or not?
It’s somewhat annoying because I can only test in-game, however code are complied without problems.

I haven’t tried but you should be able to swap out the System.dll reference for the System.dll in citizen/clr2/lib/mono/4.5. Otherwise I tend to browse the assemblies using ILSpy.

1 Like

Thanks for hint. I haven’t realized fivem uses its own assembles.

BTW It cannot simply change System reference by removing and adding new ref accross browsing file in vs studio 2017, because it forces to use system directory instead.
I’ll try to figure out how to do that

Ya, I knew with mscorlib you would have to modify the csproj but wasn’t sure about System. This should work for both references https://stackoverflow.com/questions/36679211/how-to-reference-a-different-mscorlib-dll-from-within-vs2015

Thanks, It could also be changed via Project Property Settings -> Reference Path in VS 2017:


I’d prefer GUI way :slight_smile: