How can i create my own event?

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
TolikTipaTut1
Posts: 8
Joined: Mon Oct 07, 2019 10:44 am

How can i create my own event?

Post by TolikTipaTut1 » Tue Oct 08, 2019 11:38 am

Hello!
Help me please.
I`m trying to create my own event, for example:
A user presses a button for 5 times and several methods start up.

How can i solve it ?

jvierra
Posts: 13990
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: How can i create my own event?

Post by jvierra » Tue Oct 08, 2019 12:03 pm

Use the "add_<event>" method for the event on the button.

$button.add_Click( $code ]/b])

TolikTipaTut1
Posts: 8
Joined: Mon Oct 07, 2019 10:44 am

Re: How can i create my own event?

Post by TolikTipaTut1 » Tue Oct 08, 2019 12:12 pm

And what should I do if I create my own class ?
How can I create events there?

jvierra
Posts: 13990
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: How can i create my own event?

Post by jvierra » Tue Oct 08, 2019 12:23 pm

Classes do not have events. Forms have events.

You are way over-complicating this project. PowerShell aggravates this and Forms just add more layers of complexity.

Why do you think you need to run a form in a runspace? That is almost never needed. You have to have a clear statement of requirements starting with you Use cases and then choose the execution tools and method. You are starting at the end and trying to build back to what you need. That is almost always a formula for disaster.

TolikTipaTut1
Posts: 8
Joined: Mon Oct 07, 2019 10:44 am

Re: How can i create my own event?

Post by TolikTipaTut1 » Tue Oct 08, 2019 12:29 pm

Sorry, I put it wrong.
I want to create a class counter.
It has a method that counts to 16 and then "calls" an event.

Can I create my own events in powershell?

jvierra
Posts: 13990
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: How can i create my own event?

Post by jvierra » Tue Oct 08, 2019 12:45 pm

No but you can add code to any object that raises an event,

Look for powershell examples of how to crate a timer and add an event.

jvierra
Posts: 13990
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: How can i create my own event?

Post by jvierra » Tue Oct 08, 2019 12:47 pm

Here is an article with a fairly good example and discussion:

https://mcpmag.com/articles/2012/03/13/ ... art-2.aspx

jvierra
Posts: 13990
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: How can i create my own event?

Post by jvierra » Tue Oct 08, 2019 1:10 pm

Here is a simple example of a timer in a form. Copy and paste at a command prompt to execute.

Code: Select all

Add-Type -AssemblyName System.Windows.Forms
$form = [System.Windows.Forms.Form]::New()

$buttonStop = [System.Windows.Forms.Button]::New()
$buttonStart = [System.Windows.Forms.Button]::New()

$form.StartPosition = 'CenterScreen'
$form.Controls.Add($buttonStop)
$form.Controls.Add($buttonStart)

$form1_Load={
	$script:timer = [System.Windows.Forms.Timer]::New()
	$timer.Interval = 1000
	$timer.add_Tick({
		Write-Host Timer ticked! -Fore Green
    })
}
$form.add_Load($form1_Load)

$buttonStart.Location = '83, 46'
$buttonStart.Name = 'buttonStart'
$buttonStart.Size = '75, 23'
$buttonStart.Text = 'Start'
$buttonStart_Click={
	$timer.Start()
}
$buttonStart.add_Click($buttonStart_Click)

$buttonStop.Location = '83, 86'
$buttonStop.Name = 'buttonStop'
$buttonStop.Size = '75, 23'
$buttonStop.Text = 'Stop'
$buttonStop_Click={
	$timer.Stop()
}
$buttonStop.add_Click($buttonStop_Click)

$form.ShowDialog()

jvierra
Posts: 13990
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: How can i create my own event?

Post by jvierra » Tue Oct 08, 2019 2:37 pm

Here is an example of the easy way to create code that runs in a new runspace. See attached file.

Code: Select all

$buttonStart_Click={
	$this.Enabled = $false
	$psScript = {
		Get-ChildItem c:\ 
        }
	$script:ps = [powershell]::Create()
	$ps.AddScript($psScript)
	$script:asyncResult = $ps.BeginInvoke()
	$buttonStop.Enabled = $true
}
This runs the code in $psScript in the background until it is finished. DO not try to use forms in a runspace called from a form. It will stop functioning as soon as you try to access the runspace or any of its components. To run a form and access data takes quite a bit more code that is carefully designed.

Here is how to et the results:

Code: Select all

$buttonStop_Click={
	$this.Enabled = $false
	$output = $script:ps.EndInvoke($asyncResult)
	$textbox1.Lines = $output | Out-String
	$buttonStart.Enabled = $true
}
We can actually use a timer to poll the runspace(s) until they are completed then extract the data safely.

Almost all of the code examples I have seen on the Internet are old and from very early versions of PS. PS now creates its own new runspace when we get a copy and it defaults to the best configuration available. It only takes three lines to launch a script in a runspace.


$script:ps =
PowerShell Code
Double-click the code block to select all.
::Create()
	$ps.AddScript($psScript)
	$script:asyncResult = $ps.BeginInvoke()


That's all that is required.  PS automatically creates a new runspace unless optioned to not do so.

Run the attached script to see how this works.
Attachments
Demo-Runspace.ps1
(4.27 KiB) Downloaded 10 times

Locked