Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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 6 years and 4 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.
$colVTScanner = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
$colVTScanner.AutoSizeMode = 'DisplayedCells'
$colVTScanner.HeaderText = 'Process'
$colVTScanner.Name = 'colVTScanner'
$colVTScanner.ReadOnly = $True
$colVTScanner.Resizable = 'False'
$colVTScanner.Width = 93
$datagridview = New-Object System.Windows.Forms.DataGridView
$datagridview.AllowUserToAddRows = $False
$datagridview.AllowUserToDeleteRows = $False
$datagridview.ColumnHeadersHeightSizeMode = 'AutoSize'
[void]$datagridview.Columns.Add($colVTScanner)
$datagridview.Dock = 'Top'
$datagridview.Location = '0, 0'
$datagridview.Name = 'datagridview'
$datagridview.ReadOnly = $True
$datagridview.RowHeadersVisible = $False
$datagridview.RowTemplate.Resizable = 'False'
$datagridview.Size = '300, 300'
$datagridview.TabIndex = 1
#$dataGridView.ScrollBars = 'None'
$NewUser = [PowerShell]::Create().AddScript(
{
foreach($process in (Get-Process).ProcessName)
{
$datagridview.Rows.Add($process) | Out-Null
}
#$dataGridView.ScrollBars = 'Vertical'
})
# Created a new runspace
$NewUserRunspace = [RunspaceFactory]::CreateRunspace()
$NewUserRunspace.ApartmentState = "STA"
# The command below only allowed 1 click to be executed at a time, even if you clicked the hell out of the button while it was in progress. I commented it out for the ability to have more than 1 thread created in this runspace.
# $NewUserRunspace.ThreadOptions = "ReuseThread"
$NewUserRunspace.Open()
$NewUserRunspace.SessionStateProxy.SetVariable("datagridview", $datagridview)
# Assigned my new Powershell Instance to the runspace I just created.
$NewUser.Runspace = $NewUserRunspace
# Invoked the Powershell Instance/Script.
#Problem:
$NewUser.BeginInvoke()
#Noproblem but GUI freezes
#$NewUser.Invoke()
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(600,600)
$form.Controls.Add($datagridview)
$form.ShowDialog()
I have posted a little sample Script, if you switch between:
#Problem:
$NewUser.BeginInvoke()
#Noproblem but GUI freezes
#$NewUser.Invoke()
you will see the result of the scrollbar.
I need to use it with BeginInvoke() (asynchron) but that gives me that bad behavior of the scrollbar, is there any workaround or solution to fix this?
The solution from the other post was using Invoke, but that is not possible because of GUI-Freeze.
This would be easier to manage if you used a "JobTracker" control set. Runspaces run on a separate thread and can have conflicts when trying to modify objects in another thread. The sync hash can create issues when trying to rapidly update a control on another thread.
It is also hard to understand what your exact issue is. The scrollbar does not update automatically. What do you mean by "scrollbar error"? What is a scrollbar error?
Here is a workaround for the scrollbar issue. Turn off the scrollbars before creating the runspace. Add button to the form and use it to end the async invoke and turn on the scrollbars.
This topic is 6 years and 4 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.