因为懒得去配置async 和await的环境了,wx.getStorageSync是同步的就
直接了在这个基础上了一个重入锁的js来实现同步
/***本类主要用来实现同步*/
const lockerKey = "wxlocker"
var lockerContainer={}
/**
* 加锁函数
* 参数:锁名
*
*/
function lock(lockName){
var obj= wx.getStorageSync(lockerKey)
console.log(111)
console.log(obj)
if (typeof obj === "undefined" || obj==null||obj==''){
lockerContainer.lockName = 1
wx.setStorageSync(lockerKey, lockerContainer)
}else{
lockerContainer = obj
if (typeof lockerContainer.lockName !== "undefined" &&lockerContainer.lockName != null &&lockerContainer.lockName != ''){
lockerContainer.lockName = lockerContainer.lockName +1
}else{
lockerContainer.lockName = 1
}
//更新锁
wx.setStorageSync(lockerKey, lockerContainer)
}
}
/**
* 解锁函数
* 参数:锁名
*
*/
function unlock(lockName){
var obj = wx.getStorageSync(lockerKey)
console.log(222)
console.log(obj)
if (typeof obj === "undefined" || obj == null || obj == '') {//没有锁过
return
} else {
lockerContainer = obj
if (typeof lockerContainer.lockName !== "undefined" &&lockerContainer.lockName != null &&lockerContainer.lockName != '') {
if (lockerContainer.lockName <=0){
lockerContainer.lockName=0
return
}else{
lockerContainer.lockName = lockerContainer.lockName - 1
}
} else {
return
}
//更新锁
wx.setStorageSync(lockerKey, lockerContainer)
obj = wx.getStorageSync(lockerKey)
console.log(obj)
}
}
/**
* 是否被锁函数
* 参数:锁名
* 返回值:boolean
*/
function isLockedBy(lockName){
var obj = wx.getStorageSync(lockerKey)
if (typeof obj === "undefined" || obj == null || obj == '') {//没有锁过
return false
} else {
lockerContainer = obj
if (typeof lockerContainer.lockName !== "undefined" &&lockerContainer.lockName != null &&lockerContainer.lockName != '') {
if (lockerContainer.lockName >0) {
return true
} else {
return false
}
} else {
return false
}
}
}
/**
* 等待函数(等待100*millseconds后自动解除)
* 参数:锁名,间隔毫秒,等待解锁完成后的回调函数
*
*/
function wait(lockName, millseconds,callback){
//100次监控后清空锁
var expireTime = 0
var timer =setInterval(function () {
console.log("wait " + millseconds)
if (!isLockedBy(lockName)){
clearInterval(timer)
callback()
}else{//被锁住了
expireTime++
if (expireTime >100) {
lockerContainer.lockName=null
//清空锁
wx.setStorageSync(lockerKey, lockerContainer)
clearInterval(timer)
}
}
}, millseconds)
}
module.exports = {
wait: wait,
unlock: unlock,
lock: lock
}
用法,加锁多少次就要解锁多少次
lock("mylock")
lock("mylock")
unlock("mylock")
unlock("mylock")
wait("mylock",1000,callback)
谁说不能DisableX的?Private
Declare
Function
GetSystemMenu
Lib
"User32"
(ByVal
hwnd
As
Long,
ByVal
bRevert
As
Long)
As
Long
Private
Declare
Function
RemoveMenu
Lib
"User32"
(ByVal
hMenu
As
Long,
ByVal
nPosition
As
Long,
ByVal
wFlags
As
Long)
As
Long
Private
Declare
Function
DrawMenuBar
Lib
"User32"
(ByVal
hwnd
As
Long)
As
Long
Private
Declare
Function
GetMenuItemCount
Lib
"User32"
(ByVal
hMenu
As
Long)
As
Long
Private
Const
MF_BYPOSITION
=
&H400&
Private
Const
MF_DISABLED
=
&H2&
Private
Sub
Form_Load()
Call
DisableX(Me)
End
Sub
Private
Sub
DisableX(Frm
As
Form)
Dim
hMenu
As
Long,
nCount
As
Long
hMenu
=
GetSystemMenu(Frm.hwnd,
0)
nCount
=
GetMenuItemCount(hMenu)
Call
RemoveMenu(hMenu,
nCount
-
1,
MF_DISABLED
Or
MF_BYPOSITION)
DrawMenuBar
Frm.hwnd
End
Sub
运行DisableX(Me)就可以把这个窗体的关闭按钮锁定住。
状态栏不可见:
App.ShowInTaskBar
=
False
即可
另外如果你要直接让程序不能关闭,直接加入以下代码:
Private
sub
Form_unload(cancel
as
integer)
cancel
=
1
end
sub
即可!正常是关不掉的现在!除了结束进程!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)