---
# System prepended metadata

title: Main & Threading
tags: [Speaker]

---

# Main & Threading
###### tags: `Speaker`

## Rule

### 必須符合

1. 檔名必須跟 class 名稱一樣

### 需要使用的話 (如果需要用到 Job 的功能的話)

1. feature class 裡需要需要一個變數指向他自己的 thread
2. feature class 裡需要一個 method 叫 import_thread 把 thread import 進來

## How to use

```python=
class feature:
    def __init__(self, ...):
        self.thread = None
        ...
    
    def import_thread(self, thread):
        self.thread = thread
        
    def work(self):
        ...
        self.thread.add_thread({
            'class': 'test',
            'func': 'stop',
        })
        
    def wait(self):
        self.thread.wait_for_exec()
```

## Open another thread

```python=
# in one program block
    ...
    self.thread.add_thread({
        # type: str, feature class 的名稱
        "class": "class name",
        # type: str, function 的名稱，注意不支援巢狀 function 使用
        "func": "function name",
        # type: tuple, 傳遞參數，如果沒有可以不代
        "args": (some arguments, ...),
    }
    ...
```

## Pause thread

```python=
class feature:
    def work(self, ...):
        while True:
            self.thread.wait_for_exec()
            ...
```

## Wishing board

1. 