Navigation
Browse the developer docs

FH2 Mod Platform for .NET

Threads

Which thread each FH2 class must be used from, and how to get there.

By Bartholomew Ho (qiangqiang101) · MIT license · v0.12.0 · 15 Nov 2026 · repository · original page

On this page

The game runs several threads at once. Most FH2 classes must be used from one particular thread, because the game is in the middle of changing things on the others. Get it wrong and you get an exception that says where to call from, rather than a crash.

The threads

ThreadWhere your code runs on itWhat to use there
The game's threadGame.Tick, Game.Run, Game.PhysicsStep, ANNA entriesGame, Player, Vehicle, World, UI, Audio and most others
The UI threadMenus.Frame, settings' Changed, pages, title-screen entries, a hotkey's ReboundMenus and UiElement, SettingsTile
The platform's threadOnInit, a hotkey's Pressedsetup only: add settings, hotkeys, ANNA entries, handlers
Your own threadsnew Thread(...), Task.Run, timerswaiting, files, the network; nothing in the game

Each class page says which thread it's for.

Getting onto the game's thread

From anywhere else, wrap the work in Game.Run:

// A hotkey runs on a platform thread, so hand the work to the game.
Pressed = () => Game.Run(() => Player.Car?.Repair()),
Pressed = Sub() Game.Run(Sub() Player.Car?.Repair())

It runs at the next game frame. Inside Game.Tick it runs straight away.

To get a value back, use Game.RunAsync and await it from your own thread:

float speed = await Game.RunAsync(() => Player.Car?.SpeedKmh ?? 0);

Never wait for RunAsync on the game's thread itself. It would wait for itself forever and freeze the game.

Game.IsGameThread tells you where you are.

Keep it short

Game.Tick and Menus.Frame run every frame, and the game waits for them. Do the slow work (files, the network, Thread.Sleep) on a thread of your own, and only hand the result to the game.

OnInit is the same: the game is waiting to start. Start a thread for anything slow:

protected override void OnInit()
{
    new Thread(() =>
    {
        ModPlatform.WaitForGame();
        // ...slow work...
    }) { IsBackground = true }.Start();
}

Exceptions

An exception thrown in Game.Tick, a setting's Changed, a command or any other callback is caught and written to platform.log. It never reaches the game, and your handler stays on.

Copied from the wiki page Threads at commit 7396a07 · MIT license
MIT License

Copyright (c) 2026 Bartholomew Ho

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.