C# High CPU Usage

I’m a developer for a Lua ESX Server, and I just found out that using Exports you can call ESX events from C#, so I tried to switch to Csharp because that’s the language I’m best in. But to my surprise, after creating a basic resource that spawns a bodyguard following you. It uses 0.02 CPU msec or an average time of 3.00%.

using System;
using System.Collections.Generic;
using CitizenFX.Core;
using static CitizenFX.Core.Native.API;

namespace Client
{
    public class Main : BaseScript
    { 
        // Runs On Program Start
        public Main()
        {
            EventHandlers["onClientResourceStart"] += new Action<string>(OnResourceStart);
        }

        public void OnResourceStart(string resourceName)
        {
            // -- Due to it being an event, Each resource on the client with C# will call on "onClientResourceStart"
            // This just makes sure that each and every other resource wont restart all its settings.
            if (GetCurrentResourceName() != resourceName) { return; }


            RegisterCommand("bodyguard", new Action<int, List<object>, string>(async (source, args, rawCommand) =>
            {
                // -- Gets Play Information
                var player = Game.Player.Character;
                var pedgroup = Game.PlayerPed.PedGroup;

                // -- Requests for the body guard model to laod
                RequestModel((uint)PedHash.Blackops02SMY);

                // -- While the body guard model hasnt loaded wait.
                while (!HasModelLoaded((uint)PedHash.Blackops02SMY))
                {
                    await BaseScript.Delay(100);
                }

                // -- Creating the model
                Ped bodyguard = await World.CreatePed(PedHash.Blackops02SMY, player.Position + (player.ForwardVector * 2));
                bodyguard.Task.LookAt(player);
                SetPedAsGroupMember(bodyguard.Handle, GetPedGroupIndex(player.Handle));
                // -- SetCurrentPedWeapon(bodyguard.Handle, (uint)WeaponHash.Pistol50, true);
                SetPedCombatAbility(bodyguard.Handle, 2);
                SetPedCombatAttributes(bodyguard.Handle, 78, true);
                GiveWeaponToPed(bodyguard.Handle, (uint)WeaponHash.Pistol50, -1, false, true);
            }), false);
        }
    }
}

That is my Csharp code. It shouldn’t be using 3% CPU time. I also have another resource that only registers the command “/testcsharp”. The command uses an export from ESX. “showNotifcation” to be exact. And I use that function to print “success” indicating that the script worked.
And that resource also uses around the same CPU time, and msec as the bodyguard resource does.

My fxmanifest.lua for the script above looks like this:

fx_version 'cerulean'
game 'gta5'

client_script 'Client.net.dll'

If anyone has any idea why this uses so much CPU compared to what it’s during and possibly how to fix it, Please tell me, thanks.

You’re just looking at the base overhead, which can just be ignored. If you’re extremely anal about that 0.02 microseconds you can try out the mono_v2 runtime

1 Like

Thanks a lot! Also, I’m not really the one caring a lot about it, but the guy that hosts the server has some kind of OCD about it being 0.00. Either way thanks a lot! This was just what I was looking for

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.