Skip to content

Instantly share code, notes, and snippets.

@cwensley
Last active July 24, 2023 11:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwensley/5eb55557414a71e72693 to your computer and use it in GitHub Desktop.
Save cwensley/5eb55557414a71e72693 to your computer and use it in GitHub Desktop.
Shows how to use Eto.Forms with async/await and background tasks to perform long running work while keeping the UI responsive.
using System;
using Eto.Forms;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Eto.Drawing;
namespace RunEtoWithBackgroundTasks
{
class ProgressForm : Form
{
ProgressBar progressBar;
public float Progress {
get { return progressBar.Value / 1000f; }
set { progressBar.Value = (int)Math.Round (Math.Min (value, 1f) * 1000f); }
}
public ProgressForm ()
{
Title = "Progress!";
progressBar = new ProgressBar { MaxValue = 1000, Width = 200 };
Content = progressBar;
}
}
class ImageForm : Form
{
ImageView imageView;
public Image Image { get { return imageView.Image; } set { imageView.Image = value; } }
public ImageForm ()
{
Title = "Image!";
imageView = new ImageView { Size = new Size (200, 200) };
Content = imageView;
}
}
static class UI
{
static Dictionary<string, Form> forms = new Dictionary<string, Form> ();
static T CreateForm<T> (string name)
where T: Form, new()
{
Form form;
if (!forms.TryGetValue (name, out form)) {
form = new T ();
forms.Add (name, form);
form.Show ();
}
;
return (T)form;
}
static T GetForm<T> (string name)
where T: Form
{
Form form;
if (forms.TryGetValue (name, out form))
return (T)form;
return null;
}
public static void CloseWindow (string name)
{
Form form;
if (forms.TryGetValue (name, out form)) {
form.Close ();
forms.Remove (name);
}
}
public static void ShowProgress (string name, float progress)
{
var form = CreateForm<ProgressForm> (name);
form.Progress = progress;
}
public static void ShowImage (string name, Image image)
{
var form = CreateForm<ImageForm> (name);
form.Image = image;
}
}
public class Program
{
[STAThread]
public static void Main (string[] args)
{
var app = new Application ();
// app.Invoke shouldn't be required here, but it is as of Eto 2.1
app.Initialized += (sender, e) => app.Invoke (async () => {
for (int i = 0; i < 1000; i++) {
//process an i-th image
await Task.Run (() => {
// do actual background work here
Thread.Sleep (1); // simulate work
});
UI.ShowProgress ("progress1", (float)i / 1000); //(create a form if not already shown and set the progress)
}
UI.CloseWindow ("progress1");
for (int i = 0; i < 1000; i++) {
//take the-ith image
await Task.Run (() => {
// do actual background work here
Thread.Sleep (1); // simulate work
});
var img = new Bitmap (200, 200, PixelFormat.Format32bppRgb);
using (var g = new Graphics (img)) {
g.DrawText (SystemFonts.Default (), Brushes.White, 0, 0, "Image " + i);
}
UI.ShowImage ("imageWnd", img); //(create a form if not already shown and set the image)
}
UI.CloseWindow ("imageWnd");
app.Quit ();
});
app.Run ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment