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 ?
How can i create my own event?
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.
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.
-
- Posts: 8
- Last visit: Fri Jan 27, 2023 6:53 am
Re: How can i create my own event?
And what should I do if I create my own class ?
How can I create events there?
How can I create events there?
Re: How can i create my own event?
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.
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.
-
- Posts: 8
- Last visit: Fri Jan 27, 2023 6:53 am
Re: How can i create my own event?
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?
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?
Re: How can i create my own event?
Here is an article with a fairly good example and discussion:
https://mcpmag.com/articles/2012/03/13/ ... art-2.aspx
https://mcpmag.com/articles/2012/03/13/ ... art-2.aspx
Re: How can i create my own event?
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()
Re: How can i create my own event?
Here is an example of the easy way to create code that runs in a new runspace. See attached file.
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:
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 =
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
}
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
}
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 129 times