Skip to content

How to run main loop cooperatively #3871

Answered by tznind
elfalem asked this question in Q&A
Discussion options

You must be logged in to vote

If you want to use Terminal.Gui in a seperate thread from your main task e.g. to provide the user updates then it could look something like this (see below).

Just make sure to use invoke so that any UI operation happens on the same thread you are running Terminal.Gui.

Also don't have your main thread running Console writeline while terminal gui is running in another thread!

using delme;
using Terminal.Gui;

// This task simulates your main script
var loadingTask = Task.Delay(TimeSpan.FromSeconds(3));
var semaphore = new SemaphoreSlim(0, 1); // Initialize a semaphore with 0 count.

// Run Terminal.Gui in a seperate thread
Task.Run(() =>
{
    Application.Init();
    var w = new MyWin();
    

Replies: 5 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@tznind
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@elfalem
Comment options

@tznind
Comment options

@elfalem
Comment options

@elfalem
Comment options

Answer selected by elfalem
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants