mainloop.html 10 KB

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