2

I am creating a real-time action game in GTKSharp (I know, not a good idea). I have a method that I need to constantly call that updates my game window (the images in some buttons) but I can't figure out how.

I tried putting the method in a loop and running the loop in another thread, besides the application thread.

  • Tried it with Parallel.Invoke (although this just runs one thread after another, it fails to run them simultaneously)

    public static void Main(string[] args) {
        Parallel.Invoke(() => RunGameLoop(), () => RunApp());
    }

    public void RunGameLoop(){
        while(runGame) { //runGame is a bool, I change it to false when I click a button in MyGameWindow
            MyGameWindow.UpdateButtons(); //MyGameWindow is a GTK.Window
        }
    }

    public void RunApp(){
        MyGameWindow.Show();
        Application.Run();
    }

  • Tried a couple of different ways to separate things in threads (which resulted in this error

Gdk:ERROR:/build/gtk+2.0-AoeliP/gtk+2.0-2.24.32/gdk/gdkregion-generic.c:1110:miUnionNonO: assertion failed: (y1 < y2)

that is followed by a bunch of text)


    public static void Main(string[] args) {
        Thread i = new Thread(new ThreadStart(RunGameLoop));
        Thread l = new Thread(new ThreadStart(RunApp));

        //I also tried with this
        //Thread i = new Thread(RunGameLoop);
        //Thread l = new Thread(RunApp);

        i.Start();
        l.Start();
    }

    public void RunGameLoop(){
        while(runGame) {
            MyGameWindow.UpdateButtons();
        }
    }

    public void RunApp(){
        MyGameWindow.Show();
        Application.Run();
    }

  • I also tried to use tasks rather than threads (also doesn't work)

    public static void Main(string[] args) {
        Task[] tasks = new Task[2];

        tasks[0] = Task.Run(() => RunGameLoop());
        tasks[1] = Task.Run(() => RunApp());

        Task.WaitAll(tasks);
    }

    public void RunGameLoop(){
        while(runGame) {
            MyGameWindow.UpdateButtons();
        }
    }

    public void RunApp(){
        MyGameWindow.Show();
        Application.Run();
    }


    public static void Main(string[] args) {
        Task t1 = Task.Factory.StartNew(() => RunGameLoop());
        Task t2 = Task.Factory.StartNew(() => RunApp());
        Task.WaitAll(tasks);
    }

    public void RunGameLoop(){
        while(runGame) {
            MyGameWindow.UpdateButtons();
        }
    }

    public void RunApp(){
        MyGameWindow.Show();
        Application.Run();
    }

I have wasted a couple of hours on this issue and I honestly don't know, I could be misunderstanding something about how threading works or how GTKSharp works. I have thought of transferring the project to something proper like Unity but I've wasted a lot of time figuring out GTKSharp.

I am using Monodevelop 7.8.4 (build 2) and the project is a GTK# 2.0 Project.

Syndamia
  • 41
  • 2
  • 9

1 Answers1

0

After a good amount of searching for solutions, I came across this: How do I access GUI (GTK) from multi threads?. So, what I was trying to do was doomed from the beginning. After a bit more time, I found this: Winforms - Invoking a method in another thread which is for WinForms, the GTK equivalent is supposedly GLib.Timeout which I also couldn't get to work.

I guess it is completely impossible in GTK#. I will have to change how my program works.

Syndamia
  • 41
  • 2
  • 9