• 0

BackgroundWorker issue


Question

I am writing a little game (for school, hence the mandatory nonsensical design choices). The top-level component is a Winforms C# application. This is just supposed to provide a window, some basic menus and redirect input events to a C++ dll that does all the rendering and game logic processing.

I'm trying to see where I'm supposed to put my game loop in there. I assume the basic loop has to be in C#, and call native methods like update() and draw(). Since I don't want to stall the GUI, I created a background worker and passed it this method:

        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
            CycleTimer.TriggerUpdate += CycleTimer_TriggerUpdate;
            CycleTimer.TriggerDraw += CycleTimer_TriggerDraw;
            CycleTimer.Start(30);
            while (true) {
                CycleTimer.Update();
            }
        }

... where CycleTimer is my own class that I've used in a previous game entirely in C# and it was working really well. However I wasn't hosting it in a WinForms app. Now in this situation, I've noticed Update() is called every 25 milliseconds. This is completely unacceptable. It should be called at least every millisecond (and it should even be MUCH faster than that on my pc). Even when I comment out its body entirely so that it is merely called and exit, doing nothing, it is still called no more often than once every 25 milliseconds.

I suppose this is due to backgroundWorker being a somewhat low priority thread? Where is my approach wrong? I'm open to suggestions that deviate a lot from what I've did there. For instance if there was a way to manage the game loop in the C++ dll only, without stalling the WinForms host, that would be great.

Edited by Dr_Asik
Link to comment
https://www.neowin.net/forum/topic/934610-backgroundworker-issue/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Mk small update: I've replaced my background worker method with this:

        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
            var stopWatch = Stopwatch.StartNew();
            while (true) {
                Console.WriteLine(stopWatch.ElapsedTicks);
            }
        }

Here's a sample of the output:

6441918
6441929
6441941
6441952
6441964
6441977
6441989
6442002
6442015
6442027
7010387 WTF?!
7010427
7010444
7010461
7010478
7010494
7010510
7010527
7010544
7010561
7010577

So basically it is called super-often and then there are long random pauses every now and then.

  • 0

Ok nvm all this. The variability observed is normal and I can compensate for it with an intelligent game loop. I get very stable results using a compensating algorithm. Also, the long delays I had observed at the beginning stemmed from doing a Console.WriteLine every time through the loop, which would result in a deluge of output and create some delays.

All is well. I use a normal System.Threading.Thread now but I guess BackgroundWorker would have done the trick too.

  • 0

6441918
6441929
6441941
6441952
6441964
6441977
6441989
6442002
6442015
6442027
7010387 WTF?!
7010427
7010444
7010461
7010478
7010494
7010510
7010527
7010544
7010561
7010577

So basically it is called super-often and then there are long random pauses every now and then.

If this was Java, my first thought would be "garbage collection". Everyone I know that runs a production Java service has nothing but bad things to say about it. You're using .NET, but I can only assume it has a similar problem.

  • 0

If this was Java, my first thought would be "garbage collection". Everyone I know that runs a production Java service has nothing but bad things to say about it. You're using .NET, but I can only assume it has a similar problem.

It might also just be the OS deciding that my thread doesn't get CPU time for a few thousand ticks, and the same issue would happen in a non-gced environment.
  • 0

It might also just be the OS deciding that my thread doesn't get CPU time for a few thousand ticks, and the same issue would happen in a non-gced environment.

Yep, you'll notice the same thing in native code. Create 2 threads that do nothing other than increment an integer and you'll see marked variation in the progress of each thread.
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I have not verified if the fix mentioned on the below article actually works. Why Claude Code Eats All Your RAM (And How to Fix It) There are at least four separate things that can eat your RAM, and only one of them is your fault. The Four Culprits 1. Bloated Session Logs (The Silent Killer). For 2-4 and more inluding the suggested fix see link https://marceloretana.com/blog...aude-code-eats-all-your-ram
    • One of my PC's is Windows 11 Home Edition... and guess what? it supports Hyper-V Disks and Containers, as well as WSL2... which these desktop tools leverage on an as-needed basis. It does not need to be enabled system-wide; it merely needs to exist. Furthermore, I tried something yesterday with Hermes Desktop and their "installer" actually builds everything from source as part of its installer toolchain! The installer (and updater as it were) both go through a roughly 19-step process to prep the system for the tool to be used -- and connectivity to local/remote models, local/remote agent configs, and other steps are not included as part of this process (those are all user config steps post-install). ALSO, the "bug wasn't confirmed to exist" is the weakest argument against a bug. That's literally every flawed product's knee-jerk reaction! Even politicians, lawmakers, murderers, and other violent criminals start with that defense... "I deny everything I'm accused of." The bug is the (shared) experience. Whether it's truly Claude Code's fault, or user error, or some other software package being the ultimate culprit, is not the point. They are experiencing unpleasant side effects and don't like that.
    • Each generaton has its exciting times and I was fortunate enough to be right there at the beginning of the PC industry.   I don't think any of us at that time realized how large it would become I still remember working on a TRS-80 with a green screen monitor that kept overheating and dying.     Also used some TRS-80 PCs with tape cartridge memory.   They worked but boy were they slow with everything being a very slow sequential read or write.  But heh, they worked though it wasn't long before they were repaced by the HDD. 
    • One Drive should be a four letter word. First thing I disable on my computer. If I want to upload something to the cloud, I use a cloud base storage, NOT microsoft. Oh but what about backup. I have done backups since floppy disk eras...it just comes naturally.
    • It does just not OneCloud. 🤷🏻‍♂️
  • Recent Achievements

    • One Month Later
      Sopa flores earned a badge
      One Month Later
    • First Post
      StaticMatrix earned a badge
      First Post
    • Week One Done
      StaticMatrix earned a badge
      Week One Done
    • Rookie
      lamborghiniv10 went up a rank
      Rookie
    • One Month Later
      pinnclepd earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      515
    2. 2
      PsYcHoKiLLa
      208
    3. 3
      +Edouard
      160
    4. 4
      Steven P.
      96
    5. 5
      ATLien_0
      81
  • Tell a friend

    Love Neowin? Tell a friend!