status dialog / spinner #1119
-
I'd like to pop up a modal while a background process is running, and then close that model when the background process completes...but I can't quite seem the right combination of dialog/window/etc. to make that happen. What's the best way to show something, and then programmatically hide it once an operation completes? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Maybe something like this? static class Program {
public static ListView lstView;
public static List<string> logList = new ();
public static Dialog dlg;
static void Main ()
{
Application.Init ();
var top = Application.Top;
var btnStart = new Button ("Start Process");
btnStart.Clicked += () => {
RegisterLog ("Starting logging...");
Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), PrepareBackgroundProcess);
StartBackgroundProcessDialog ();
RegisterLog ("Finished logging...");
};
lstView = new ListView (logList) {
Y = Pos.Y (btnStart) + 1,
Width = Dim.Fill (),
Height = Dim.Fill ()
};
var win = new Window ("Background Process Sample");
win.Add (btnStart, lstView);
top.Add (win);
bool PrepareBackgroundProcess (MainLoop _)
{
if (dlg != null) {
Application.MainLoop.Invoke (async () => {
await RunBackgroundProcessAsync ();
});
}
return dlg == null;
}
Application.Run ();
}
static async Task RunBackgroundProcessAsync ()
{
RegisterLog ("Background Process is running...");
await Task.Delay (1000);
Application.RequestStop ();
RegisterLog ("Background Process is finished...");
}
static void StartBackgroundProcessDialog ()
{
RegisterLog ("Starting the modal dialog...");
dlg = new Dialog ("This will be closed after the process is finished.");
Application.Run (dlg);
RegisterLog ("Stopping the modal dialog...");
}
static void RegisterLog (string msg)
{
logList.Add (msg);
lstView.MoveDown ();
}
} |
Beta Was this translation helpful? Give feedback.
-
I did some changes and added a static class Program {
public static ListView lstView;
public static List<string> logList = new ();
public static Dialog dlg;
static void Main ()
{
Application.Init ();
var top = Application.Top;
var btnStart = new Button ("Start Process");
btnStart.Clicked += () => {
RegisterLog ("Starting logging...");
Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), PrepareBackgroundProcess);
StartBackgroundProcessDialog ();
RegisterLog ("Finished logging...");
};
lstView = new ListView (logList) {
Y = Pos.Y (btnStart) + 1,
Width = Dim.Fill (),
Height = Dim.Fill (),
ColorScheme = Colors.Menu
};
var win = new Window ("Background Process Sample");
win.Add (btnStart, lstView);
top.Add (win);
bool PrepareBackgroundProcess (MainLoop _)
{
if (dlg != null) {
Application.MainLoop.Invoke (async () => {
await RunBackgroundProcessAsync ();
});
}
return dlg == null;
}
Application.Run ();
}
static async Task RunBackgroundProcessAsync ()
{
RegisterLog ("Background Process is running...");
await Task.Delay (5000);
Application.RequestStop ();
RegisterLog ("Background Process is finished...");
}
static void StartBackgroundProcessDialog ()
{
RegisterLog ("Starting the modal dialog...");
dlg = new Dialog ("This will be closed after the process is finished.");
dlg.Add (new Label ("Typing in the text view while this dialog is open:"));
var txtView = new TextView () {
Y = 1,
Width = Dim.Fill (),
Height = Dim.Fill (),
ColorScheme = Colors.TopLevel
};
bool firstPress = false;
txtView.KeyPress += (e) => {
if (!firstPress) {
firstPress = true;
RegisterLog ("Starting typing in text view...");
}
};
dlg.Add (txtView);
Application.Run (dlg);
RegisterLog ("Stopping the modal dialog...");
}
static void RegisterLog (string msg)
{
logList.Add (msg);
lstView.MoveDown ();
}
} |
Beta Was this translation helpful? Give feedback.
Maybe something like this?