Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 993857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:29:21+00:00 2026-05-16T06:29:21+00:00

Here is my code: #include <gtk/gtk.h> static int counter = 0; static PangoLayout* layout;

  • 0

Here is my code:

#include <gtk/gtk.h>

static int counter = 0;
static PangoLayout* layout;
static GdkGC* gc1;
static GdkGC* gc2;
//**/static GMutex* mu;

static gboolean on_expose_event(GtkWidget* widget, GdkEventExpose* event)
{
    gchar the_string[20];

    //**/g_mutex_lock(mu);
    gdk_draw_rectangle(GDK_DRAWABLE(widget->window), gc1, TRUE, 0, 0, widget->allocation.width, widget->allocation.height);
    snprintf(the_string, 20, "%d", counter);
    pango_layout_set_text(layout, the_string, -1);
    gdk_draw_layout(GDK_DRAWABLE(widget->window), gc2, 180, 120, layout);
    //**/g_mutex_unlock(mu);

    g_print (".");
    return FALSE;
}

gpointer func(gpointer data)
{
    //**/g_usleep(10000);
    GdkWindow* window = GDK_WINDOW(data);
    while(TRUE)
    {
        gdk_window_invalidate_rect(window, NULL, FALSE);
        //**/gdk_window_process_updates(window, FALSE);
        if(counter % 100 == 0) g_print("X");
        g_usleep(10);
        ++counter;
    }
    return FALSE;
}

int main(int argc, char** argv)
{
    GtkWidget* window;
    GtkWidget* drawer;
    GdkColormap* colormap;
    GdkColor color;

    g_thread_init(NULL);
    gdk_threads_init();
    gtk_init(&argc, &argv);

    //:Create widgets and 
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    drawer = gtk_drawing_area_new();
    gtk_widget_set_size_request(drawer, 400, 300);
    gtk_container_add(GTK_CONTAINER(window), drawer);
    //.

    gtk_widget_show_all(window);

    //:Initializing Graphic Contexts
    colormap = gtk_widget_get_colormap(GTK_WIDGET(drawer));
    layout = gtk_widget_create_pango_layout(GTK_WIDGET(drawer), NULL);
    gc1 = gdk_gc_new(GDK_DRAWABLE(GTK_WIDGET(drawer)->window));
    gc2 = gdk_gc_new(GDK_DRAWABLE(GTK_WIDGET(drawer)->window));
    gdk_color_parse("#000", &color);
    gdk_colormap_alloc_color(colormap, &color, FALSE, TRUE);
    gdk_gc_set_background(gc1, &color);
    gdk_color_parse("#fff", &color);
    gdk_colormap_alloc_color(colormap, &color, FALSE, TRUE);
    gdk_gc_set_foreground(gc2, &color);
    //.

    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
    g_signal_connect(G_OBJECT(drawer), "expose_event", G_CALLBACK(on_expose_event), NULL);

    //**/mu = g_mutex_new();
    //Run the problematic thread!
    g_thread_create(func, GTK_WIDGET(drawer)->window, TRUE, NULL);

    gdk_threads_enter();
    gtk_main();
    gdk_threads_leave();

    //**/g_mutex_free(mu);
    return 0;
}

It’s a multithreaded program, and did nothing so especial! It just force a GtkDrawingArea to redraw itself from the main thread (GTK+ main loop).

My problem (or GTK+ problem!) is that after a long time running (maybe 0.5, 1 or 4 minutes!), the expose event will not work (does not response to my requests until I’m resizing the whole window!)! Please, compile and run the code to check this situation. I added some print commands which may help! but i can’t detect the problem, because of my little experience.

I hoped this program can redraws its context for almost always, but it didn’t (or couldn’t). What can i do to solve this problem? Maybe i should report a bug to GTK+ developers?

I’m using Debian + GCC + GTK2.20

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:29:22+00:00Added an answer on May 16, 2026 at 6:29 am

    Most likely, you need to surround your gdk_window_invalidate_rect() call with gdk_threads_enter()/gdk_threads_leave() pair. Read the gdk threads for more information about gtk and threads. Look at the example there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.