Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add article on threading #2568

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions docfx/articles/threading.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Threading
It is common for a developer to run one or more background tasks on a separate
`Thread` (or `Task`). For example to periodically fetch data. If you want to
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the "infinitive form" in API docs instead of the "impersonal you". The infinitive form of a verb is its basic, uninflected form, typically preceded by the word "to" in English. For example, "to eat," "to play," or "to learn." By using the infinitive form, the sentence becomes more concise and focuses on the action itself.

This would be rewritten as "To make changes to a View from a background task, issue the change on the main Terminal.Gui Thread thread."

Miguel tends to write using the "impersonal you" form, so a lot of the old docs still use this. But I've worked hard to rid most API docs of it. Thanks.

make changes to any `View` from this background task, you must issue the change
on the main Terminal.Gui UI thread.

**Updates to the user interface should only be performed on the main UI thread.**

This is required to prevent errors, for example if Terminal.Gui is trying to
render a `View` while its contents are actively being changed from another Thread.

## How to issue call to main thread
To run an operation on the main thread from a background task use:

```csharp
Application.MainLoop.Invoke(()=>{/* Your Code Here*/});
```

For example:

```csharp
using Terminal.Gui;

Application.Init();

var w = new Window();

// Start a background task
Task.Run(() =>
{
while(true)
{
// Do some computation
Task.Delay(30).Wait();

// Issue a call to the main thread
Application.MainLoop.Invoke(() =>
{
//This code resolves in the main UI thread
w.RemoveAll();
w.Add(new Label("Time is:" + DateTime.Now));

w.SetNeedsDisplay();
});
}
});

Application.Run(w);

Application.Shutdown();
```
1 change: 1 addition & 0 deletions docfx/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ A toolkit for building rich console apps for .NET, .NET Core, and Mono that work

* [API Reference](~/api/Terminal.Gui/Terminal.Gui.yml)
* [Views and controls built into the Terminal.Gui library](~/articles/views.md)
* [Threading](~/articles/threading.md)
* [Terminal.Gui API Overview](~/articles/overview.md)
* [Keyboard Event Processing](~/articles/keyboard.md)
* [Event Processing and the Application Main Loop](~/articles/mainloop.md)
Expand Down