mainloop.html 9.9 KB

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