2022-10-04 13:21:05 +08:00
|
|
|
|
//----------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Copyright © yanghy. All Rights Reserved.
|
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
|
// Licensed under Apache License Version 2.0, January 2004
|
|
|
|
|
//
|
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2022-10-04 13:21:05 +08:00
|
|
|
|
//
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
2023-02-26 21:39:42 +08:00
|
|
|
|
// 在应用主线程中执行(非主线程使用)异步&同步执行包裹函数
|
2022-10-04 13:21:05 +08:00
|
|
|
|
package cef
|
|
|
|
|
|
|
|
|
|
import (
|
2023-02-13 21:25:35 +08:00
|
|
|
|
"github.com/energye/energy/common/imports"
|
2022-12-07 21:29:38 +08:00
|
|
|
|
"github.com/energye/golcl/lcl/api/dllimports"
|
2022-12-07 15:50:59 +08:00
|
|
|
|
"math"
|
|
|
|
|
"sync"
|
2022-10-04 13:21:05 +08:00
|
|
|
|
)
|
|
|
|
|
|
2022-12-07 15:50:59 +08:00
|
|
|
|
var (
|
2022-12-07 21:29:38 +08:00
|
|
|
|
qac = &queueAsyncCall{id: 0, calls: sync.Map{}}
|
|
|
|
|
applicationQueueAsyncCallFunc dllimports.ProcAddr
|
2022-12-07 15:50:59 +08:00
|
|
|
|
)
|
|
|
|
|
|
2022-12-07 21:29:38 +08:00
|
|
|
|
func applicationQueueAsyncCallInit() {
|
2023-02-13 21:25:35 +08:00
|
|
|
|
applicationQueueAsyncCallFunc = imports.Proc(internale_SetApplicationQueueAsyncCallFunc)
|
2022-12-07 21:29:38 +08:00
|
|
|
|
applicationQueueAsyncCallFunc.Call(applicationQueueAsyncCallEvent)
|
2022-12-07 15:50:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func applicationQueueAsyncCallProc(id uintptr) uintptr {
|
2022-12-07 21:29:38 +08:00
|
|
|
|
qac.call(id)
|
2022-12-07 15:50:59 +08:00
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 21:39:42 +08:00
|
|
|
|
// qacFn 队列异步调用函数 id:事件id
|
2022-12-07 21:29:38 +08:00
|
|
|
|
type qacFn func(id int)
|
2022-12-07 15:50:59 +08:00
|
|
|
|
|
2022-12-07 21:29:38 +08:00
|
|
|
|
type queueCall struct {
|
2022-12-07 15:50:59 +08:00
|
|
|
|
IsSync bool
|
2022-12-07 21:29:38 +08:00
|
|
|
|
Fn qacFn
|
2022-12-07 15:50:59 +08:00
|
|
|
|
Wg *sync.WaitGroup
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type queueAsyncCall struct {
|
|
|
|
|
id uintptr
|
|
|
|
|
calls sync.Map
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 21:39:42 +08:00
|
|
|
|
// QueueAsyncCall 仅LCL,在主进程中异步调用
|
2023-01-10 23:03:30 +08:00
|
|
|
|
//
|
2023-02-26 21:39:42 +08:00
|
|
|
|
// 在UI主进程中执行, 异步执行
|
2022-10-04 13:21:05 +08:00
|
|
|
|
//
|
2023-02-26 21:39:42 +08:00
|
|
|
|
// 非主进程的多线程操作可使用该函数包裹
|
2022-10-04 13:21:05 +08:00
|
|
|
|
//
|
2023-02-26 21:39:42 +08:00
|
|
|
|
// 在任何变更UI的操作都有可能因非主线程出现不一至, 而出现程序错误或程序崩溃, 可以尝试使用该回调函数解决.
|
2022-10-04 13:21:05 +08:00
|
|
|
|
//
|
2023-02-26 21:39:42 +08:00
|
|
|
|
// 提示: CEF事件或函数中不应使用该函数包裹
|
2022-12-07 21:29:38 +08:00
|
|
|
|
func QueueAsyncCall(fn qacFn) int {
|
|
|
|
|
id := qac.set(&queueCall{
|
2022-12-07 15:50:59 +08:00
|
|
|
|
IsSync: false,
|
|
|
|
|
Fn: fn,
|
|
|
|
|
})
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFApplication_QueueAsyncCall).Call(id)
|
2022-12-07 15:50:59 +08:00
|
|
|
|
return int(id)
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 21:39:42 +08:00
|
|
|
|
// QueueSyncCall 同 QueueAsyncCall
|
2023-01-10 23:03:30 +08:00
|
|
|
|
//
|
2023-02-26 21:39:42 +08:00
|
|
|
|
// 同步执行 - 阻塞UI
|
2022-12-07 21:29:38 +08:00
|
|
|
|
func QueueSyncCall(fn qacFn) int {
|
|
|
|
|
qc := &queueCall{
|
2022-12-07 15:50:59 +08:00
|
|
|
|
IsSync: true,
|
|
|
|
|
Fn: fn,
|
|
|
|
|
Wg: &sync.WaitGroup{},
|
|
|
|
|
}
|
|
|
|
|
qc.Wg.Add(1)
|
2022-12-07 21:29:38 +08:00
|
|
|
|
id := qac.set(qc)
|
2023-02-13 21:25:35 +08:00
|
|
|
|
imports.Proc(internale_CEFApplication_QueueAsyncCall).Call(id)
|
2022-12-07 15:50:59 +08:00
|
|
|
|
qc.Wg.Wait()
|
|
|
|
|
qc.Fn = nil
|
|
|
|
|
qc.Wg = nil
|
|
|
|
|
qc = nil
|
|
|
|
|
return int(id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *queueAsyncCall) call(id uintptr) {
|
|
|
|
|
if call, ok := m.calls.LoadAndDelete(id); ok {
|
2022-12-07 21:29:38 +08:00
|
|
|
|
qc := call.(*queueCall)
|
2022-12-07 15:50:59 +08:00
|
|
|
|
if qc.IsSync {
|
|
|
|
|
qc.Fn(int(id))
|
|
|
|
|
qc.Wg.Done()
|
|
|
|
|
} else {
|
|
|
|
|
qc.Fn(int(id))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-07 21:29:38 +08:00
|
|
|
|
func (m *queueAsyncCall) set(fn *queueCall) uintptr {
|
2022-12-07 15:50:59 +08:00
|
|
|
|
if m.id >= math.MaxUint {
|
|
|
|
|
m.id = 0
|
|
|
|
|
}
|
|
|
|
|
m.id++
|
|
|
|
|
m.calls.Store(m.id, fn)
|
|
|
|
|
return m.id
|
2022-10-04 13:21:05 +08:00
|
|
|
|
}
|