如何在线程中调用外部定义的函数?


PowerShell交流中心分类: 变量-函数-脚本-条件-循环-参数如何在线程中调用外部定义的函数?
0
Q1501389392 asked 7年 ago

脚本如下所示,我想在异步执行一个任务,这个任务就是调用外部一个方法,但没能成功。请问:
1,原因是啥?
2,怎样才能达到目的?

function test-task ([string] $message) {
$outstr=((Get-Date).ToString()+$message)
$outstr
$outstr|Out-File-FilePath “D:\test-task.txt”
}

# test-task task1

$task1 = { test-task task1 }
$thread1 = [PowerShell]::Create()
$job1 = $thread1.AddScript($task1).BeginInvoke()
 

1 Answers
0
Mooser Lee 管理员 answered 7年 ago

这个是不可能的,你只能把外部函数的定义作为参数(字符串)传进来,然后再运行。

lightexpensive replied 5年 ago

请问有例子吗?在下不是很明白

Curious replied 3年 ago

意思是不是在新开启的线程里外部函数是未定义的

Curious replied 3年 ago

意思是不是在新开启的线程里外部函数是未定义的

Curious replied 3年 ago

意思是不是在新开启的线程里外部函数是未定义的