How to setup a global stop condition

by tim91700 at 2012-12-11 04:43:12

Hello,

I want to do this :

I have two buttons, one to start an action, one to stop it.
If I click on start, the action is ok, if I click on stop, the action is stopped.

My code :
function do_start
{
$boutonLauchScan.Enabled = $false
$boutonStopScan.Enabled = $true
$GLOBAL:conditionStop = 1
while ($conditionStop -eq 1)
{
$("ok")
}
}
function do_stop
{
$boutonLaunchScan.Enabled = $true
$GLOBAL:conditionStop = 0

}


I only crash my script and not do what I want, any help ?
by nohandle at 2012-12-11 05:33:44
What is the error message on the crash?

seems like you are running one action and then intercept it when calling another function. But there is too little context and info to be sure.
by tim91700 at 2012-12-11 05:44:52
In fact it’s the script editor (Power GUI) which crash.

That’s it, I want to intercept and close the action lauch by another one. Maybe a thread (or something like) will be good ?
by nohandle at 2012-12-11 06:21:49
I am not sure, as I said there is too little info and context. Have you tried in console? Maybe STA mode is in your way.
[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace.ApartmentState -eq [Threading.ApartmentState]]

You [quote="tim91700"]That’s it, I want to intercept and close the action lauch by another one. Maybe a thread (or something like) will be good ?[/quote]
What about powershell Jobs?
by tim91700 at 2012-12-11 07:05:32
Actually, it’s for a script like an IP scanner, so I can’t use it as a job.

I’ll find documentation about Runspaces.
by nohandle at 2012-12-11 07:10:02
Would you mind sharing the reason why jobs can’t be used? :slight_smile:
by tim91700 at 2012-12-11 23:02:22
Because this project is for school, and I’ll execute it on Windows and not on a VM
by nohandle at 2012-12-12 02:00:29
[quote="tim91700"]Because this project is for school, and I’ll execute it on Windows and not on a VM[/quote]
I dont’t think we understand each other. I don’t see any relation btween running the script in virtual machine and beckground jobs.
There is some info on the topic:
http://msdn.microsoft.com/en-us/library … 88(v=vs.85).aspx