ProcessTable.cs 2.3 KB

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