How can i create my own event?

Ask your 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.
This topic is 4 years and 5 months 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.
Locked
TolikTipaTut1
Posts: 8
Last visit: Fri Jan 27, 2023 6:53 am

How can i create my own event?

Post by TolikTipaTut1 »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How can i create my own event?

Post by jvierra »

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

$button.add_Click( $code ]/b])
TolikTipaTut1
Posts: 8
Last visit: Fri Jan 27, 2023 6:53 am

Re: How can i create my own event?

Post by TolikTipaTut1 »

And what should I do if I create my own class ?
How can I create events there?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How can i create my own event?

Post by jvierra »

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
Last visit: Fri Jan 27, 2023 6:53 am

Re: How can i create my own event?

Post by TolikTipaTut1 »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How can i create my own event?

Post by jvierra »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How can i create my own event?

Post by jvierra »

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

https://mcpmag.com/articles/2012/03/13/ ... art-2.aspx
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How can i create my own event?

Post by jvierra »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How can i create my own event?

Post by jvierra »

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 128 times
This topic is 4 years and 5 months 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.
Locked