Why can't I run my exe built?

Use this forum to ask questions after your subscription maintenance expires or before you buy. Need information on licensing or pricing? Questions about a trial version? This is the right place for you. No scripting questions, please.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
torbjorn_l
Posts: 1
Last visit: Mon Feb 22, 2016 11:54 am

Why can't I run my exe built?

Post by torbjorn_l »

Product, version and build: 4.2.99.0
32 or 64 bit version of product: 64
Operating system: Win 10

Hi,

I have Powershell script like this:

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Temp\test\"
$watcher.Filter = "Dataport.dpl"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true

$changed = Register-ObjectEvent $watcher "Changed" -Action {
$datestamp = get-date -format "yyyyMMdd_HHmmssff"
copy-item $eventArgs.FullPath "$($sender.Path)Dataport-$datestamp.dpl"
}

Build an exe from this...

The config file is like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />
<supportedRuntime version="v2.0" />
</startup>
</configuration>

When I try to run the exe it just shows a exe window for a second then closes...
And it does not do what it should do whatching for file changes and create a time stamped copy.

The ps1 script works fine when I run it interactivelly from Power Shell.

Test syntax errors:
test.ps1: No syntactical errors found

Testing script says:
test.ps1: -----------------------------------------------
test.ps1: Verify Script - test.ps1
test.ps1: -----------------------------------------------
test.ps1: Profile: Local Machine
test.ps1: PowerShell V5 (64 Bit)
test.ps1:
test.ps1: -----------------------------------------------
test.ps1: Missing Cmdlets:
test.ps1: -----------------------------------------------
test.ps1:
test.ps1: -- No Missing Cmdlets --
test.ps1:

So what is causing this...

I downloaded this test version and this is an important test that this works...
It says it supports Windows 10 64bit, that is what I have.
Also say it requires .NET Framework 4.5 (that I have installed).

But I am still suspect it is some compatibility issue...
I notice it says supported versions v.4.0 & v2.0 in config file. Is that refering to PowerShell version...

Would be very glad if you can help me with this.

Thanks

Torbjörn Lindström
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Why can't I run my exe built?

Post by Alexander Riedel »

Alright. So the good news is that your script is just fine. It works well as you have established by running it as a script in the PowerShell console.
The bad news is that you need to learn about the concept of processes and runspaces. Don't worry, it is not that hard.

If you run your script in a console, the script adds the event handler to the console's runspace and then exits. Since the console process is still running, the registered event will trigger and cause whatever action you intended.

If you package that script as an exe, it adds the event and its handler to the runspace in the executable's host. Then the process exists after your script exits. The runspace, the event handler and the process are all terminated. So nothing will happen thereafter.

So basically if you want that as an exe, you need to add a loop to the bottom of your script that keeps the process alive until some type of exit condition is reached. That will prevent your process from exiting and the event handler from going to la-la land.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.