mainloop.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <!DOCTYPE html>
  2. <!--[if IE]><![endif]-->
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  7. <title>Event Processing and the Application Main Loop </title>
  8. <meta name="viewport" content="width=device-width">
  9. <meta name="title" content="Event Processing and the Application Main Loop ">
  10. <meta name="generator" content="docfx 2.18.2.0">
  11. <link rel="shortcut icon" href="../favicon.ico">
  12. <link rel="stylesheet" href="../styles/docfx.vendor.css">
  13. <link rel="stylesheet" href="../styles/docfx.css">
  14. <link rel="stylesheet" href="../styles/main.css">
  15. <meta property="docfx:navrel" content="">
  16. <meta property="docfx:tocrel" content="">
  17. </head>
  18. <body data-spy="scroll" data-target="#affix">
  19. <div id="wrapper">
  20. <header>
  21. <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
  22. <div class="container">
  23. <div class="navbar-header">
  24. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
  25. <span class="sr-only">Toggle navigation</span>
  26. <span class="icon-bar"></span>
  27. <span class="icon-bar"></span>
  28. <span class="icon-bar"></span>
  29. </button>
  30. <a class="navbar-brand" href="../index.html">
  31. <img id="logo" class="svg" src="../logo.svg" alt="">
  32. </a>
  33. </div>
  34. <div class="collapse navbar-collapse" id="navbar">
  35. <form class="navbar-form navbar-right" role="search" id="search">
  36. <div class="form-group">
  37. <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
  38. </div>
  39. </form>
  40. </div>
  41. </div>
  42. </nav>
  43. <div class="subnav navbar navbar-default">
  44. <div class="container hide-when-search" id="breadcrumb">
  45. <ul class="breadcrumb">
  46. <li></li>
  47. </ul>
  48. </div>
  49. </div>
  50. </header>
  51. <div role="main" class="container body-content hide-when-search">
  52. <div class="article row grid">
  53. <div class="col-md-10">
  54. <article class="content wrap" id="_content" data-uid="">
  55. <h1 id="event-processing-and-the-application-main-loop">Event Processing and the Application Main Loop</h1>
  56. <p>The method <code>Application.Run</code> that we covered before will wait for
  57. events from either the keyboard or mouse and route those events to the
  58. proper view.</p>
  59. <p>The job of waiting for events and dispatching them in the
  60. <code>Application</code> is implemented by an instance of the
  61. <a href="docs/api/Mono.Terminal/Mono.Terminal.MainLoop.html"><code>MainLoop</code></a>
  62. class.</p>
  63. <p>Mainloops are a common idiom in many user interface toolkits so many
  64. of the concepts will be familiar to you if you have used other
  65. toolkits before. </p>
  66. <p>This class provides the following capabilities:</p>
  67. <ul>
  68. <li>Keyboard and mouse processing</li>
  69. <li>.NET Async support</li>
  70. <li>Timers processing</li>
  71. <li>Invoking of UI code from a background thread</li>
  72. <li>Idle processing handlers</li>
  73. <li>Possibility of integration with other mainloops.</li>
  74. <li>On Unix systems, it can monitor file descriptors for readability or writability.</li>
  75. </ul>
  76. <p>The <code>MainLoop</code> property in the the
  77. <a href="../api/Terminal.Gui/Terminal.Gui.Application.html"><code>Application</code></a>
  78. provides access to these functions.</p>
  79. <p>When your code invokes <code>Application.Run (Toplevel)</code>, the application
  80. will prepare the current
  81. <code>Toplevel</code>[../api/Terminal.Gui/Terminal.Gui.Toplevel.html] instance by
  82. redrawing the screen appropriately and then calling the mainloop to
  83. run. </p>
  84. <p>You can configure the Mainloop before calling Application.Run, or you
  85. can configure the MainLoop in response to events during the execution.</p>
  86. <p>The keyboard inputs is dispatched by the application class to the
  87. current TopLevel window this is covered in more detail in the
  88. <a href="keyboard.html">Keyboard Event Processing</a> document.</p>
  89. <h2 id="async-execution">Async Execution</h2>
  90. <p>On startup, the <code>Application</code> class configured the .NET Asynchronous
  91. machinery to allow you to use the <code>await</code> keyword to run tasks in the
  92. background and have the execution of those tasks resume on the context
  93. of the main thread running the main loop.</p>
  94. <p>Once you invoke <code>Application.Main</code> the async machinery will be ready
  95. to use, and you can merely call methods using <code>await</code> from your main
  96. thread, and the awaited code will resume execution on the main
  97. thread. </p>
  98. <h2 id="timers-processing">Timers Processing</h2>
  99. <p>You can register timers to be executed at specified intervals by
  100. calling the <a href="https://migueldeicaza.github.io/gui.cs/api/Mono.Terminal/Mono.Terminal.MainLoop.html#Mono_Terminal_MainLoop_AddTimeout_System_TimeSpan_System_Func_Mono_Terminal_MainLoop_System_Boolean__"><code>AddTimeout</code></a> method, like this:</p>
  101. <pre><code class="lang-csharp">void UpdateTimer ()
  102. {
  103. time.Text = DateTime.Now.ToString ();
  104. }
  105. var token = Application.MainLoop.AddTimeout (TimeSpan.FromSeconds (20), UpdateTimer);
  106. </code></pre><p>The return value from AddTimeout is a token value that you can use if
  107. you desire to cancel the timer before it runs:</p>
  108. <pre><code class="lang-csharup">Application.MainLoop.RemoveTimeout (token);
  109. </code></pre><h2 id="idle-handlers">Idle Handlers</h2>
  110. <p>You can register code to be executed when the application is idling
  111. and there are no events to process by calling the
  112. <a href="https://migueldeicaza.github.io/gui.cs/api/Mono.Terminal/Mono.Terminal.MainLoop.html#Mono_Terminal_MainLoop_AddIdle_System_Func_System_Boolean__"><code>AddIdle</code></a>
  113. method. This method takes as a parameter a function that will be
  114. invoked when the application is idling. </p>
  115. <p>Idle functions should return <code>true</code> if they should be invoked again,
  116. and <code>false</code> if the idle invocations should stop.</p>
  117. <p>Like the timer APIs, the return value is a token that can be used to
  118. cancel the scheduled idle function from being executed.</p>
  119. <h2 id="threading">Threading</h2>
  120. <p>Like other UI toolkits, Terminal.Gui is generally not thread safe.
  121. You should avoid calling methods in the UI classes from a background
  122. thread as there is no guarantee that they will not corrupt the state
  123. of the UI application. </p>
  124. <p>Generally, as there is not much state, you will get lucky, but the
  125. application will not behave properly.</p>
  126. <p>You will be served better off by using C# async machinery and the
  127. various APIs in the <code>System.Threading.Tasks.Task</code> APIs. But if you
  128. absolutely must work with threads on your own you should only invoke
  129. APIs in Terminal.Gui from the main thread.</p>
  130. <p>To make this simple, you can use the <code>Application.MainLoop.Invoke</code>
  131. method and pass an <code>Action</code>. This action will be queued for execution
  132. on the main thread at an appropriate time and will run your code
  133. there.</p>
  134. <p>For example, the following shows how to properly update a label from a
  135. background thread:</p>
  136. <pre><code>void BackgroundThreadUpdateProgress ()
  137. {
  138. Application.MainLoop.Invoke (() =&gt; {
  139. progress.Text = $&quot;Progress: {bytesDownloaded/totalBytes}&quot;;
  140. });
  141. }
  142. </code></pre><h2 id="integration-with-other-main-loop-drivers">Integration With Other Main Loop Drivers</h2>
  143. <p>It is possible to run the main loop in a way that it does not take
  144. over control of your application, but rather in a cooperative way.</p>
  145. <p>To do this, you must use the lower-level APIs in <code>Application</code>: the
  146. <code>Begin</code> method to prepare a toplevel for execution, followed by calls
  147. to <code>MainLoop.EventsPending</code> to determine whether the events must be
  148. processed, and in that case, calling <code>RunLoop</code> method and finally
  149. completing the process by calling <code>End</code>.</p>
  150. <p>The method <code>Run</code> is implemented like this:</p>
  151. <pre><code>void Run (Toplevel top)
  152. {
  153. var runToken = Begin (view);
  154. RunLoop (runToken);
  155. End (runToken);
  156. }
  157. </code></pre><h2 id="unix-file-descriptor-monitoring">Unix File Descriptor Monitoring</h2>
  158. <p>On Unix, it is possible to monitor file descriptors for input being
  159. available, or for the file descriptor being available for data to be
  160. written without blocking the application.</p>
  161. <p>To do this, you on Unix, you can cast the <code>MainLoop</code> instance to a
  162. <a href="docs/api/Mono.Terminal/Mono.Terminal.UnixMainLoop.html"><code>UnixMainLoop</code></a>
  163. and use the <code>AddWatch</code> method to register an interest on a particular
  164. condition.</p>
  165. </article>
  166. </div>
  167. <div class="hidden-sm col-md-2" role="complementary">
  168. <div class="sideaffix">
  169. <div class="contribution">
  170. <ul class="nav">
  171. <li>
  172. <a href="https://github.com/migueldeicaza/gui.cs/blob/master/docfx/articles/mainloop.md/#L1" class="contribution-link">Improve this Doc</a>
  173. </li>
  174. </ul>
  175. </div>
  176. <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
  177. <!-- <p><a class="back-to-top" href="#top">Back to top</a><p> -->
  178. </nav>
  179. </div>
  180. </div>
  181. </div>
  182. </div>
  183. <footer>
  184. <div class="grad-bottom"></div>
  185. <div class="footer">
  186. <div class="container">
  187. <span class="pull-right">
  188. <a href="#top">Back to top</a>
  189. </span>
  190. <span>Copyright © 2015-2017 Microsoft<br>Generated by <strong>DocFX</strong></span>
  191. </div>
  192. </div>
  193. </footer>
  194. </div>
  195. <script type="text/javascript" src="../styles/docfx.vendor.js"></script>
  196. <script type="text/javascript" src="../styles/docfx.js"></script>
  197. <script type="text/javascript" src="../styles/main.js"></script>
  198. </body>
  199. </html>