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

  • SEARCH
  • Home
  • 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 8019123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:17:23+00:00 2026-06-04T21:17:23+00:00

I want to perform asynchronous file IO operations. Here is a simplified version of

  • 0

I want to perform asynchronous file IO operations. Here is a simplified version of what I am trying to do in Vala:

void main(string[] args) {
    store_async();

    while(true)
        ;
}

async void store_async() {
    File file = File.new_for_path("settings.ini");

    stderr.printf("Checking if file exists...\n");
    if (!file.query_exists()) {
        stderr.printf("About to yield file.create_async...\n");
        try {
            yield file.create_async(FileCreateFlags.REPLACE_DESTINATION);
        } catch (Error err) {
            error("Error creating file: %s\n", err.message);
        }
        stderr.printf("Returned from file.create_async.\n");
    }

    string data = "hello\n";
    string new_etag;
    stderr.printf("About to yield file.replace_contents_async...\n");
    try {
        yield file.replace_contents_async(
            data.data,
            null,
            false,
            FileCreateFlags.NONE,
            null,
            out new_etag);
    } catch (Error err) {
        error("Error replacing contents: %s\n", err.message);
    }

    stderr.printf("Returned from file.replace_contents_async.\n");
}

When I run this program, and no file settings.ini exists, settings.ini is created and I see this output:

Checking if file exists...
About to yield file.create_async...
(hangs)

If settings.ini exists, nothing is written to it, and I see this output:

Checking if file exists...
About to yield file.create_async...
(hangs)

I have a similar problem if I try to reproduce the issue in C. Here is my C code (it doesn’t replicate the entire vala example above, just the part that creates the file):

#include <glib.h>
#include <gio/gio.h>
#include <stdio.h>

void create_callback(GObject *source_object, GAsyncResult *res, gpointer user_data);
void write_contents();

GFile* file;

void main(int argc, char** argv) {
    g_type_init();

    fprintf(stderr, "Before file_new_for_path\n");
    file = g_file_new_for_path("settings.ini");

    fprintf(stderr, "Before file_query_exists\n");
    if (!g_file_query_exists(file, NULL)) {
        fprintf(stderr, "Before file_create_async\n");
        g_file_create_async(
                file,
                G_FILE_CREATE_REPLACE_DESTINATION,
                G_PRIORITY_DEFAULT,
                NULL,
                create_callback,
                NULL);
        fprintf(stderr, "After file_create_async\n");
    } else {
        fprintf(stderr, "File already exists. Before write_contents\n");
        write_contents();
        fprintf(stderr, "File already exists. After write_contents\n");
    }

    while(TRUE)
        ;
}

void create_callback(GObject *source_object, GAsyncResult *res, gpointer user_data) {
    fprintf(stderr, "In create_callback. Before write_contents.\n");
    write_contents();
    fprintf(stderr, "In create_callback. After write_contents.\n");
}

void write_contents() {
    fprintf(stderr, "In write_contents\n");
}

When I run this example, I see this output (assuming settings.ini does not exist):

Before file_new_for_path
Before file_query_exists
Before file_create_async
After file_create_async
(hangs)

In other words, create_callback is never called.

What am I doing wrong? Why don’t g_file_create_async and g_file_replace_contents_async ever complete when I call them?

  • 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-06-04T21:17:24+00:00Added an answer on June 4, 2026 at 9:17 pm

    The async implementations in glib require that you run a message loop. If not, how and when would your callbacks be called?

    So in Vala just add:

    var loop = new MainLoop();
    loop.run();
    

    And in C:

    GMainLoop *loop = g_main_loop_new(NULL, FALSE);
    g_main_loop_run(loop);
    g_main_loop_unref(loop)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to perform a case insensitive sub-string first appearance replacement. I've tried this
I have a controller class that uses asynchronous calls to perform some network operations.
I want to perform an advanced query for the Task model (simplified versio: class
I want to perform animation on main thread (cause UIKit objects are not thread-safe),
I want perform a couple of basic operations on windows registry. I wrote a
I want perform a easy task in android, which when receiving a call the
I want to perform validations for my editfields.so I am writing the validations on
I want to perform some logic in JavaScript when users click on the span
I want to perform a full vacuum and reindex on my database for my
I want to perform search on a LONGBLOB field containing Arabic text data. For

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.