Show / Hide Table of Contents

# Class MainLoop

Simple main loop implementation that can be used to monitor file descriptor, run timers and idle handlers.

##### Inheritance

System.Object
MainLoop

###### Namespace: Mono.Terminal

###### Assembly: Terminal.Gui.dll

##### Syntax

    public class MainLoop

##### Remarks

Monitoring of file descriptors is only available on Unix, there does not seem to be a way of supporting this on Windows.

### Constructors

#### MainLoop()

Default constructor

##### Declaration

    public MainLoop ();

#### MainLoop(IMainLoopDriver)

Creates a new Mainloop, to run it you must provide a driver, and choose one of the implementations UnixMainLoop, NetMainLoop or WindowsMainLoop.

##### Declaration

    public MainLoop (Mono.Terminal.IMainLoopDriver driver);

##### Parameters

Type | Name | Description
--- | --- | ---
IMainLoopDriver | driver | To be added.

### Fields

#### WindowsKeyPressed

This event is raised when a key is pressed when using the Windows driver.

##### Declaration

    public Action<ConsoleKeyInfo> WindowsKeyPressed;

##### Field Value

Type | Description
--- | ---
System.Action<System.ConsoleKeyInfo> | To be added.

### Properties

#### Driver

The current IMainLoopDriver in use.

##### Declaration

    public Mono.Terminal.IMainLoopDriver Driver { get; }

##### Property Value

Type | Description
--- | ---
IMainLoopDriver | The driver.

### Methods

#### AddIdle(Func<Boolean>)

Executes the specified @idleHandler on the idle loop. The return value is a token to remove it.

##### Declaration

    public Func<bool> AddIdle (Func<bool> idleHandler);

##### Parameters

Type | Name | Description
--- | --- | ---
System.Func<System.Boolean> | idleHandler | To be added.

##### Returns

Type | Description
--- | ---
System.Func<System.Boolean> | To be added.

#### AddTimeout(TimeSpan, Func<MainLoop,Boolean>)

Adds a timeout to the mainloop.

##### Declaration

    public object AddTimeout (TimeSpan time, Func<Mono.Terminal.MainLoop,bool> callback);

##### Parameters

Type | Name | Description
--- | --- | ---
System.TimeSpan | time | To be added.
System.Func<MainLoop,System.Boolean> | callback | To be added.

##### Returns

Type | Description
--- | ---
System.Object | To be added.

##### Remarks

When time time specified passes, the callback will be invoked. If the callback returns true, the timeout will be reset, repeating the invocation. If it returns false, the timeout will stop.

               The returned value is a token that can be used to stop the timeout
               by calling RemoveTimeout.

#### AddWatch(Int32, MainLoop+Condition, Func<MainLoop,Boolean>)

Watches a file descriptor for activity.

##### Declaration

    public object AddWatch (int fileDescriptor, Mono.Terminal.MainLoop.Condition condition, Func<Mono.Terminal.MainLoop,bool> callback);

##### Parameters

Type | Name | Description
--- | --- | ---
System.Int32 | fileDescriptor | To be added.
| condition | To be added.
System.Func<MainLoop,System.Boolean> | callback | To be added.

##### Returns

Type | Description
--- | ---
System.Object | To be added.

##### Remarks

When the condition is met, the provided callback is invoked. If the callback returns false, the watch is automatically removed.

              The return value is a token that represents this watch, you can
              use this token to remove the watch by calling RemoveWatch.

#### EventsPending(Boolean)

Determines whether there are pending events to be processed.

##### Declaration

    public bool EventsPending (bool wait = false);

##### Parameters

Type | Name | Description
--- | --- | ---
System.Boolean | wait | To be added.

##### Returns

Type | Description
--- | ---
System.Boolean | To be added.

##### Remarks

You can use this method if you want to probe if events are pending. Typically used if you need to flush the input queue while still running some of your own code in your main thread.

#### Invoke(Action)

Runs @action on the thread that is processing events

##### Declaration

    public void Invoke (Action action);

##### Parameters

Type | Name | Description
--- | --- | ---
System.Action | action | To be added.

#### MainIteration()

Runs one iteration of timers and file watches

##### Declaration

    public void MainIteration ();

##### Remarks

You use this to process all pending events (timers, idle handlers and file watches).

               You can use it like this:
                 while (main.EvensPending ()) MainIteration ();

#### RemoveIdle(Func<Boolean>)

Removes the specified idleHandler from processing.

##### Declaration

    public void RemoveIdle (Func<bool> idleHandler);

##### Parameters

Type | Name | Description
--- | --- | ---
System.Func<System.Boolean> | idleHandler | To be added.

#### RemoveTimeout(Object)

Removes a previously scheduled timeout

##### Declaration

    public void RemoveTimeout (object token);

##### Parameters

Type | Name | Description
--- | --- | ---
System.Object | token | To be added.

##### Remarks

The token parameter is the value returned by AddTimeout.

#### RemoveWatch(Object)

Removes an active watch from the mainloop.

##### Declaration

    public void RemoveWatch (object token);

##### Parameters

Type | Name | Description
--- | --- | ---
System.Object | token | To be added.

##### Remarks

The token parameter is the value returned from AddWatch

#### Run()

Runs the mainloop.

##### Declaration

    public void Run ();

#### Stop()

Stops the mainloop.

##### Declaration

    public void Stop ();