mainloop.html 9.8 KB

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