ProcessTable.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios;
  6. [ScenarioMetadata ("ProcessTable", "Demonstrates TableView with the currently running processes.")]
  7. [ScenarioCategory ("TableView")]
  8. public class ProcessTable : Scenario
  9. {
  10. private TableView tableView;
  11. public override void Main ()
  12. {
  13. Application.Init ();
  14. var win = new Window
  15. {
  16. Title = GetName (),
  17. Y = 1, // menu
  18. Height = Dim.Fill (1) // status bar
  19. };
  20. tableView = new TableView { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill (1) };
  21. // First time
  22. CreateProcessTable ();
  23. // Then every second
  24. Application.AddTimeout (
  25. TimeSpan.FromSeconds (1),
  26. () =>
  27. {
  28. CreateProcessTable ();
  29. return true;
  30. }
  31. );
  32. win.Add (tableView);
  33. Application.Run (win);
  34. win.Dispose ();
  35. Application.Shutdown ();
  36. }
  37. private void CreateProcessTable ()
  38. {
  39. int ro = tableView.RowOffset;
  40. int co = tableView.ColumnOffset;
  41. tableView.Table = new EnumerableTableSource<Process> (
  42. Process.GetProcesses (),
  43. new Dictionary<string, Func<Process, object>>
  44. {
  45. { "ID", p => p.Id },
  46. { "Name", p => p.ProcessName },
  47. { "Threads", p => p.Threads.Count },
  48. { "Virtual Memory", p => p.VirtualMemorySize64 },
  49. { "Working Memory", p => p.WorkingSet64 }
  50. }
  51. );
  52. tableView.RowOffset = ro;
  53. tableView.ColumnOffset = co;
  54. tableView.EnsureValidScrollOffsets ();
  55. }
  56. }