我一直在使用Powershell一段时间,它不是理想的编程环境,但却被我的程序困住了。跑马灯进度条在Powershell中冻结

我的程序是一个带有选取框进度条和搜索作业的GUI。

我的程序执行的操作:使用Powershell运行脚本后,如果模式为MTA,它将在STA模式下重新启动Powershell。之后,它会要求提供一个文件夹位置。输入文件夹位置后,它将开始搜索作业,并将搜索文件的位置。每个文件都将被存储到一个数组中。该数组将打印到将保存在桌面上的tempfile.txt中。同时,该作业正在搜索GUI将使用滚动进度条显示表单的文件。

我的程序需要做什么:在作业完成搜索和存储文件后,它必须关闭表单。

我已经尝试过使用$ formSearchingFiles.Close()命令,但我注意到乔布斯无法关闭他们的“父”线程,所以这项工作将无法关闭窗体。

我也尝试使用Wait-Job cmdlet解决问题,但然后“选框进度”栏会冻结,否则表单根本不会显示。

我已经看过很多互联网的解决方案,但我找不到适合这个问题的。我正在考虑多处理,但我不知道这是否可能在PowerShell 2.0中(我限制在2.0或更低)。

我也不知道Search-Job是否可以通知主线程完成任务,以便主线程可以继续执行程序,而不冻结进度条。

我希望我已经解释了有关程序和我的问题。

# Get the path of the script

$scriptPath = ((Split-Path $script:MyInvocation.MyCommand.Path) + "\")

$scriptName = $MyInvocation.MyCommand.Name

$script = $scriptPath + $scriptName

# Check if powershell is running in STA(Single Threaded Apartment) or MTA(Multi Threaded Apartment) mode.

# If it is running in MTA mode then restart Powershell in STA mode.

if ([threading.thread]::CurrentThread.GetApartmentState() -eq "MTA")

{

Write-Host Restarting Powershell in STA mode

& $env:SystemRoot\system32\WindowsPowerShell\v1.0\powershell.exe -sta "& {&'$script'}"

}

else

{

$folderPath = $currentFolderLocation.Text

$tempFile = $currentStagingLocation.Text

$tempFile += "\fileArray.txt"

function OnApplicationLoad {

return $true #return true for success or false for failure

}

function OnApplicationExit {

$script:ExitCode = 0 #Set the exit code for the Packager

}

function Call-Searching_pff {

[void][reflection.assembly]::Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

[void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

[void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

[void][reflection.assembly]::Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

[void][reflection.assembly]::Load("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

[void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

[void][reflection.assembly]::Load("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

[void][reflection.assembly]::Load("System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

[System.Windows.Forms.Application]::EnableVisualStyles()

$formSearchingFiles = New-Object 'System.Windows.Forms.Form'

$label = New-Object 'System.Windows.Forms.Label'

$progressbar = New-Object 'System.Windows.Forms.ProgressBar'

$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'

$FormEvent_Load={

$folderPath = &read-host "Enter path"

$tempFile = (([Environment]::GetFolderPath("Desktop")) + "\tempfile.txt")

$SearchJob = Start-Job -scriptblock {

param ($folderPath, $tempFile)

$fileArray = @()

# Get all files and folders under the specified path

$items = Get-ChildItem -Path $folderPath -Recurse

foreach ($item in $items)

{

# Check if the item is a file or a folder

if (!($item.PSIsContainer))

{

# Extract path of file with path of entered folder

$extractedPath = $item.FullName

$extractedPath = $extractedPath.Replace($folderPath, "")

$fileArray += $extractedPath

}

}

# Save array in temporary file

$fileArray | out-file $tempFile

$formSearchingFiles.Close() #Does not work inside job :(

} -ArgumentList @($folderPath, $tempFile)

}

$Form_StateCorrection_Load=

{

#Correct the initial state of the form to prevent the .Net maximized form issue

$formSearchingFiles.WindowState = $InitialFormWindowState

}

$Form_Cleanup_FormClosed=

{

#Remove all event handlers from the controls

try

{

$formSearchingFiles.remove_Load($FormEvent_Load)

$formSearchingFiles.remove_Load($Form_StateCorrection_Load)

$formSearchingFiles.remove_FormClosed($Form_Cleanup_FormClosed)

}

catch [Exception]{ }

}

# formSearchingFiles

$formSearchingFiles.Controls.Add($label)

$formSearchingFiles.Controls.Add($progressbar)

$formSearchingFiles.ClientSize = '394, 122'

$formSearchingFiles.FormBorderStyle = 'FixedDialog'

$formSearchingFiles.MaximizeBox = $False

$formSearchingFiles.Name = "formSearchingFiles"

$formSearchingFiles.StartPosition = 'CenterScreen'

$formSearchingFiles.Text = "Compatibility Checker"

$formSearchingFiles.add_Load($FormEvent_Load)

# label

$label.Location = '12, 27'

$label.Name = "label"

$label.Size = '368, 26'

$label.TabIndex = 1

$label.Text = "Searching for files, please wait.."

$label.TextAlign = 'MiddleCenter'

# progressbar

$progressbar.Location = '12, 68'

$progressbar.MarqueeAnimationSpeed = 40

$progressbar.Name = "progressbar"

$progressbar.Size = '370, 30'

$progressbar.Style = 'Marquee'

$progressbar.TabIndex = 0

#Save the initial state of the form

$InitialFormWindowState = $formSearchingFiles.WindowState

#Init the OnLoad event to correct the initial state of the form

$formSearchingFiles.add_Load($Form_StateCorrection_Load)

#Clean up the control events

$formSearchingFiles.add_FormClosed($Form_Cleanup_FormClosed)

#Show the Form

return $formSearchingFiles.ShowDialog()

} #End Function

#Call OnApplicationLoad to initialize

if((OnApplicationLoad) -eq $true)

{

#Call the form

Call-Searching_pff | Out-Null

#Perform cleanup

OnApplicationExit

}

}

+0

为什么要使用一个WinForms进度条什么特别的原因?你见过write-progress cmdlet吗?它可以在控制台中为你呈现一个进度条,而不必担心所有这些STA垃圾。 –

+0

这个程序只是我的完整程序的一部分,我使用Windows窗体来制作一个漂亮的GUI,它比控制台中的文本更加用户友好。我刚刚发布了这篇文章,因为只有在这篇文章中我遇到了一个问题。 –

+0

我可能有解决方案。通过使用同步哈希表作为2个线程之间的“通信链接”。还没有测试过。 –

Android跑马灯进度条,跑马灯进度条在Powershell中冻结相关推荐

  1. android跑马灯有时候不跑

    android跑马灯有时候不跑的解决方法 按照下面的设置顺序进行设置,否则在有的手机上可能不会有效果,比如三星GT i9300 @Override public boolean isFocused() ...

  2. Android自定义半圆进度条 半圆渐变色进度条带指示 半圆开口大小可自由修改

    Android自定义半圆进度条 半圆渐变色进度条带指示 半圆开口大小可自由修改 首先我们来看下效果图 不同的开口大小只需要修改一个参数即可 半圆1: 半圆2: 半圆3: 如果是你想要的效果,就直接滑动 ...

  3. Android BGradualProgress 多种渐变、直角or弧角、进度条、加载条

    可实现多种渐变.直角or弧角.进度条.加载条 (Various gradient, right or arc angle, progress bar and loading bar can be re ...

  4. 精通Android自定义View(十二)绘制圆形进度条

    1 绘图基础简析 1 精通Android自定义View(一)View的绘制流程简述 2 精通Android自定义View(二)View绘制三部曲 3 精通Android自定义View(三)View绘制 ...

  5. Android可触摸圆形进度条,Android 可滚动圆形进度条 滑块和进度在进度条上面跟着滚动...

    Android 可滚动圆形进度条 滑块和进度在进度条上面跟着滚动.package com.example.test; import android.content.Context; import an ...

  6. Android仿虾米音乐播放器之自定义进度条seekbar

    先上图吧,仿照写的进度条 很明显不是系统的自带的进度条,所以我们需要自定义来实现这个效果,先看看官方给的例子 <layer-list xmlns:android="http://sch ...

  7. Android的进度条(ProgressBar)、拖动条(SeekBar)

    Android的进度条与拖动条 一.ProgressBar(进度条) 进度条,ProgressBar,分为环形和水平条行, 首先看下效果图,Progress进度加载. 进度条XML中的属性       ...

  8. 【Android FFMPEG 开发】FFMPEG 视频播放进度控制 ( 显示播放进度 | 拖动进度条播放 )

    文章目录 I . FFMPEG 播放进度控制 II . FFMPEG 播放视频 ( 效果展示 ) III . FFMPEG 获取视频时长 IV . FFMPEG 视频播放进度获取 V . FFMPEG ...

  9. android qq语音按钮,科技教程:如何在手机QQ中使用新版QQ语音进度条功能?

    如今越来越多的小伙伴对于如何在手机QQ中使用新版QQ语音进度条功能?这方面的问题开始感兴趣,看似平静的每一天,在每个人身上都在发生着各种各样的故事,因为大家现在都是想要了解到此类的信息,那么既然现在大 ...

  10. android一格一格向上的进度条,如何 使用 ProgressBar 进度条

    满意答案 dfsgvcx 2015.03.11 采纳率:52%    等级:9 已帮助:1019人 ProgressBar 控件通过从左到右用一些方块填充矩形来表示一个较长操作的进度.语法Progre ...

最新文章

  1. Python的WeRoBot框架开发公众号
  2. koa中间件mysql写法_koa-mysql(三)
  3. python-生成器函数进阶和各种推导式day15
  4. java 登录牵手_Java: HttpURLConnection 模拟登录方法 (带cookie 的Post/Get)_20160908_七侠镇莫尛貝...
  5. 反思本地与测试服一个接口不同的响应:本地正常测试服不行500错误,原因php版本新语法不一样
  6. html运用以及工具
  7. 【Boost】boost库中的小工具enable_shared_from_this
  8. linux之文件类型
  9. socket通信和异常处理札记
  10. c winform mysql类_C#连接MySQL数据库操作类
  11. Qt工作笔记-对QItemDelegate自定义委托的理解
  12. angularjs的三种注入方式
  13. Apache 虚拟主机
  14. idam oracle_oracle中的wm_concat对应达梦的是什么?
  15. editor.md实现拖拽剪切复制粘贴上传图片,文件插件
  16. Python网络爬虫与信息提取 - requests库入门
  17. 数量乘单价秋金额的计算机公式,在电子表格中数量乘以单价等于金额用什么样的公式啊?怎么算呢?...
  18. 查学籍网站报服务器错误,学籍系统常见问题
  19. sublime text 3143 授权码
  20. C# 判断正负数个数

热门文章

  1. Java基础之==与equal()的区别
  2. sg11解密 php解密 SourceGuardian解密sg_load解密去除域名IP授权
  3. 一个字等于多少个字节
  4. 利用python分析微信聊天记录
  5. 解决jinjia2 for循环变量作用域问题
  6. 西藏拉姆拉错:蓝蓝的湖水
  7. PyCharm小技巧分享—主菜单消失再显示
  8. sublime报错信息乱码_Sublime如何解决中文乱码问题
  9. vs code查找内容(当前文件查找/全局查找)
  10. 第一章 广告系统架构