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

##### Inherited Members

System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()

###### Namespace: Terminal.Gui

###### 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(IMainLoopDriver)

Creates a new Mainloop.

##### Declaration

    public MainLoop(IMainLoopDriver driver)

##### Parameters

Type | Name | Description
--- | --- | ---
IMainLoopDriver | driver | Should match the ConsoleDriver (one of the implementations UnixMainLoop, NetMainLoop or WindowsMainLoop).

### Properties

#### Driver

The current IMainLoopDriver in use.

##### Declaration

    public IMainLoopDriver Driver { get; }

##### Property Value

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

### Methods

#### AddIdle(Func<Boolean>)

Adds specified idle handler function to mainloop processing. The handler function will be called once per iteration of the main loop after other events have been handled.

##### Declaration

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

##### Parameters

Type | Name | Description
--- | --- | ---
System.Func<System.Boolean> | idleHandler | Token that can be used to remove the idle handler with RemoveIdle(Func<Boolean>) .

##### Returns

Type | Description
--- | ---
System.Func<System.Boolean> |

##### Remarks

Remove an idle hander by calling RemoveIdle(Func<Boolean>) with the token this method returns.

If the `idleHandler` returns `false` it will be removed and not called subsequently.

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

Adds a timeout to the mainloop.

##### Declaration

    public object AddTimeout(TimeSpan time, Func<MainLoop, bool> callback)

##### Parameters

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

##### Returns

Type | Description
--- | ---
System.Object |

##### 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 and be removed. The returned value is a token that can be used to stop the timeout by calling RemoveTimeout(Object).

#### 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 |

##### Returns

Type | Description
--- | ---
System.Boolean |

##### 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 | the action to be invoked on the main processing thread.

#### 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 an idle handler added with AddIdle(Func<Boolean>) from processing.

##### Declaration

    public void RemoveIdle(Func<bool> token)

##### Parameters

Type | Name | Description
--- | --- | ---
System.Func<System.Boolean> | token | A token returned by AddIdle(Func<Boolean>)

#### RemoveTimeout(Object)

Removes a previously scheduled timeout

##### Declaration

    public void RemoveTimeout(object token)

##### Parameters

Type | Name | Description
--- | --- | ---
System.Object | token |

##### Remarks

The token parameter is the value returned by AddTimeout.

#### Run()

Runs the mainloop.

##### Declaration

    public void Run()

#### Stop()

Stops the mainloop.

##### Declaration

    public void Stop()