Create maxscript threads by separating them
Written by JP.Lee
心动的 Technical Art team leader.
Max脚本通常表现为单线程。
因此,如果您有大量计算,则必须等待一会儿才能运行。
或者,您不能同时激活多个功能。
如果您通过分离线程来工作,则可以解决上述情况。
线程分开意味着您可以单独使用CPU的内核。
这是通过利用Windows支持的DotNet功能来完成的。
仅仅因为您可以分离线程并不意味着您可以对所有代码进行多线程处理。
但是,任何影响Max的区域都可能由于线程摄入它而引起问题。
这是一个冲突。
例如,我在线程A中创建了一个盒子,但线程B删除了它,但是线程A再次找到了该盒子
可能会发生这种情况,如果可能,最好单独使用这些功能。
它应用于与Max一起使用的其他功能,或用于完全独立的功能,例如使用Max-script连接到网络。
Max-script typically behaves as a single thread.
So if you have a lot of computation, you’ll have to wait a little while to run.
Or you can’t activate multiple functions at the same time.
If you work by separating threads, you can solve the above case.
Separating threads means that you can use the cores of the CPU individually.
This is done by taking advantage of the DotNet feature that Windows supports.
Just because you can separate threads doesn’t mean you can multithread all the code.
However, any area that affects Max can cause problems by thread ingesting it.
It’s a conflict.
E.g., I created a box in thread A, but thread B deleted it, but thread A found the box again
This can happen, and it’s a good idea to work with the features separately if possible.
It should be used for other functions that work with Max, or for completely separate features such as connecting to the network with Max-script.
Example code
(
global g_ThreadRollout
try (destroyDialog g_ThreadRollout) catch()
rollout g_ThreadRollout "Thread"
(
progressBar pb0 "Normal" height:25
button btn_Go0 "go" width:275 height:28
progressBar pb1 "Thread1" height:25
button btn_Go1 "Go(Thread)" width:137 height:28 across:2
button btn_Cancel1 "Cancel(Thread)" width:137 height:28
progressBar pb2 "Thread2" height:25
button btn_Go2 "Go(Thread)" width:137 height:28 across:2
button btn_Cancel2 "Cancel(Thread)" width:137 height:28
local dno_Thread1
local dno_Thread2
fn CreateThread =
(
dno_Thread1 = dotNetObject "System.ComponentModel.BackGroundWorker"
dno_Thread2 = dotNetObject "System.ComponentModel.BackGroundWorker"
dno_Thread1.WorkerSupportsCancel = true -- Enabled to use the cancel feature
dno_Thread2.WorkerSupportsCancellation = true
)
fn Action =
(
for i = 1 to 100 do
(
sleep 0.03
pb0.value = i
)
)
fn ActionThread1 sender e =
(
for i = 1 to 100 do
(
if not dno_Thread1.CancellationPending then -- it is filtered when it is canceled
(
sleep 0.03
pb1.value = i
)
else
return 0
)
)
fn ActionThread2 sender e =
(
for i = 1 to 100 do
(
if not dno_Thread2.CancellationPending then
(
sleep 0.03
pb2.value = i
)
else
return 0
)
)
on btn_Go0 pressed do
(
Action () -- The interface freezes while the progressive bar is in progress because it operates as a single thread.
)
on btn_Go1 pressed do
(
if not dno_Thread1.IsBusy then
dno_Thread1.RunWorkerAsync () -- If the thread is not working, run
)
on btn_Cancel1 pressed do
(
if dno_Thread1.IsBusy then
dno_Thread1.CancelAsync () -- Stop if the thread is working
)
on btn_Go2 pressed do
(
if not dno_Thread2.IsBusy then
dno_Thread2.RunWorkerAsync () -- If the thread is not working, run
)
on btn_Cancel2 pressed do
(
if dno_Thread2.IsBusy then
dno_Thread2.CancelAsync() -- Stop if the thread is working
)
on g_ThreadRollout open do
(
CreateThread()
dotNet.addHandlerdno_Thread1 "DoWork" ActionThread1 -- Register actionThread1 function on the first thread
dotNet.addHandlerdno_Thread2 "DoWork" ActionThread2 -- Register actionThread2 function in the second thread
)
on g_ThreadRollout close do
(
if dno_Thread1.IsBusy then
dno_Thread1.CancelAsync () -- Stop at shutdown if the thread is operating internally
if dno_Thread2.IsBusy then
dno_Thread2.CancelAsync()
gc()
)
)
createDialog g_ThreadRollout width:300 height:200
)
Code language: JavaScript (javascript)
在这里,每次使用卷展栏时,我都会创建一个线程,
注册为全局,即使定期执行,也不会对Max speed产生重大影响。
或者,对于大型脚本,如果编写面向对象的代码,则可以充分使用它。
Here, I created a thread every time I used rollout,
Register as global, and even if it is executed periodically, there will be no significant effect on Max speed.
Or, for large scripts, if you make the object-oriented code, you can use it sufficiently.
end of contents.