Cross-platform thread and GTK # not working (correct)?

I am trying to create a cross-platform C # application using C #, mono / GTK # on Linux, and .NET / GTK # on Windows, however the startup sequence should apparently be slightly different depending on the two platforms:

On Linux:

public static void Main (string[] args)
{
    Gdk.Threads.Init ();
    // etc...

On Windows:

public static void Main (string[] args)
{
    Glib.Thread.Init ();
    Gdk.Threads.Init ();
    // etc...

Both require this to be done like this: Windows complains that g_thread_init () is not being called with Linux code, and linux complains that it is already being called with Windows code. Other than that, it all works great.

My first attempt at a โ€œsolutionโ€ looked like this:

public static void Main (string[] args)
{
    try {
        Gdk.Threads.Init ();
    } catch (Exception) {
        GLib.Thread.Init ();
        Gdk.Threads.Init ();
    }
    // etc...

; GTK +, , . - ? , , , , ?

+5
4

Gtk + Win32 . , Gtk.Main().

, . , . GLib.Idle.Add() , , . false idle , .

, Gdk.Threads.Init().

+8

, Mono, Environment , .

public static void Main (string[] args)
{
    PlatformID platform = Environment.OSVersion.Platform;
    if (platform == PlatformID.Win32NT ||
        platform == PlatformID.Win32S ||
        platform == PlatformID.Win32Windows)
        Glib.Thread.Init();
    else if (platform != PlatformID.Unix)
        throw new NotSupportedException("The program is not supported on this platform");

    Gdk.Threads.Init();
    // etc...

PlatformID enum , Windows Unix, .

+2

, # GDK, GDK. gdk\_threads\_init(), g\_thread\_init() , GTK # :

GLib.Thread.Init() Gdk.Threads.Init().

Linux ( GDK 2.14.4) C, gdk\_threads\_init() g\_thread\_init(), . , GDK Linux, Windows, Linux ( Windows) g\_thread\_init(), -, . , , :

#include <gdk/gdk.h>

int main(int argc, char **argv) {
    gdk_threads_init();

    return 0;
}

gcc -o test `pkg-config --cflags --libs gdk-2.0` test.c
( test.c.)

If this program fails, it is an error in your GDK # library.
If not, this is a bug in your version of the GDK.

+1
source

Umm, have you tried decorating your Main method with the [STAThread] attribute?

eg.

#if !Mono //or whatever  
[STAThread]  
#endif  
public static void Main (string[] args)  
{  
    Gdk.Threads.Init ();  
    ...  
}  

Otherwise, you can use conditional compilation, for example ...

public static void Main (string[] args)  
{  
    Gdk.Threads.Init ();  
#if !Mono //or whatever  
    Gdk.Threads.Init ();  
#endif
    ...  
}  
0
source

All Articles