DataGridView in Runspace Scrollbar error

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.
Locked
User avatar
r41z0r
Posts: 8
Last visit: Sun Mar 04, 2018 10:36 pm

DataGridView in Runspace Scrollbar error

Post by r41z0r »

The other post was closed to early: viewtopic.php?f=21&t=11528

Code: Select all

$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.

Greetings
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridView in Runspace Scrollbar error

Post by jvierra »

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

Re: DataGridView in Runspace Scrollbar error

Post by jvierra »

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.

Code: Select all

$button1_Click={
    $datagridview1.ScrollBars = 'None'
    $script:NewUser = [PowerShell]::Create().AddScript({
            foreach ($process in ((Get-Process).ProcessName)) {
                $datagridview1.Rows.Add($process) | Out-Null
            }
        })
    $NewUserRunspace = [RunspaceFactory]::CreateRunspace()
    $NewUserRunspace.ApartmentState = 'STA'
    $NewUserRunspace.Open()
    $NewUserRunspace.SessionStateProxy.SetVariable('datagridview1',$datagridview1)
    $NewUser.Runspace = $NewUserRunspace
    $script:asyncResuls = $NewUser.BeginInvoke()
}

$button2_Click={
    $NewUser.EndInvoke($script:asyncResuls)
    $datagridview1.ScrollBars = 'Vertical'
    $datagridview1.Refresh()
}
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.
Locked