Click here to Skip to main content
14,425,522 members

.NET Framework

 
GeneralRe: ERROR IN .NET FRAMEWORK - in Windows 10 - how to solve this? Pin
S3TH_201920-Oct-19 11:21
MemberS3TH_201920-Oct-19 11:21 
GeneralRe: ERROR IN .NET FRAMEWORK - in Windows 10 - how to solve this? Pin
Eddy Vluggen20-Oct-19 11:28
mveEddy Vluggen20-Oct-19 11:28 
GeneralRe: ERROR IN .NET FRAMEWORK - in Windows 10 - how to solve this? Pin
Dave Kreskowiak20-Oct-19 14:30
mveDave Kreskowiak20-Oct-19 14:30 
Question2D Arrays Pin
LilJokez_Gaming10-Oct-19 12:29
MemberLilJokez_Gaming10-Oct-19 12:29 
AnswerRe: 2D Arrays Pin
Dave Kreskowiak10-Oct-19 14:42
mveDave Kreskowiak10-Oct-19 14:42 
QuestiondotNET performance surprises Pin
Member 798912220-Sep-19 2:57
MemberMember 798912220-Sep-19 2:57 
AnswerRe: dotNET performance surprises Pin
Gerry Schmitz20-Sep-19 6:45
mveGerry Schmitz20-Sep-19 6:45 
QuestionPowerShell - FileSystemWatcher with GUI issue Pin
Damian Bz10-Sep-19 23:48
MemberDamian Bz10-Sep-19 23:48 
Hi, I'm writing a small app for my own use to watch a certain folder and back it up to another location when files are created or amended.

I wrote the script for the file system watcher first in just the Powershell console, and this worked nicely.
Once I added it into my GUI (winforms) I discovered it would not work when activated from a button, and after a bit of investigation I can to realise that I would need to run it via Runspace.
I've written a couple of GUI scripts with runspaces so was confident I could do it (i'm not a programmer by profession, it's just a personal interest)

I've got it to work by firing the runspace job from the "start" button, but the issue I now face is that the button to stop the monitoring doesn't work.
I'm assuming because the Filesystem monitor is running in a separate Powershell session.
I get :
Unregister-Event : Event subscription with identifier 'FileCreated' does not exist.
At D:\Programming\PowerShell\.Current Projects\TAB-File Watcher\out\TABFileWatcher.ps1:84 char:5
+     Unregister-Event FileCreated
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Unregister-Event], ArgumentException
    + FullyQualifiedErrorId : INVALID_SOURCE_IDENTIFIER,Microsoft.PowerShell.Commands.UnregisterEventCommand


And when I run Get-EventSubscriber from VS Code terminal after closing the GUI, It does not display any Event subscriptions, BUT the event is clearly running as if I manually add or edit a file it copies it to the destination folder.

So my question is .. how do I access \ stop the event from a button which is not within the same Runspace ?
Or is there another (better) way of doing this ?

The relevant code is :

$RSscriptblock = {
 
    $Output_RTB = $sync.RTB
    $sourceDirectory = $sync.SD
    $filter = $sync.SF
    $destination = $sync.DES

    $fsw = New-Object IO.FileSystemWatcher $sourceDirectory, $filter -Property @{
        IncludeSubdirectories = $false 
        NotifyFilter          = [IO.NotifyFilters]'FileName, LastWrite'
    }
    
    Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        $Output_RTB.SelectionColor = "Green"
        $Output_RTB.AppendText("The file '$name' was $changeType at $timeStamp`r`n") 
        Copy-item -Force -Recurse $sourceDirectory -Destination $destination }
    
    Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action {
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        $Output_RTB.SelectionColor = "Green"
        $Output_RTB.AppendText("The file '$name' was $changeType at $timeStamp`r`n") 
        Copy-item -Force -Recurse $sourceDirectory -Destination $destination }

        $Output_RTB.SelectionColor = "Green"
        $Output_RTB.AppendText("Monitoring has Started`r`n")
        $Output_RTB.Select($Output_RTB.Text.Length, 0)
        $Output_RTB.ScrollToCaret() 
}

function Start-RSJob {
    $sync = [Hashtable]::Synchronized(@{RTB = $Output_RTB; SD = $Script:sourceDirectory; SF = $Script:filter; DES = $Script:destination})
    $rsjob = [PowerShell]::Create().AddScript($RSscriptblock)
    $runspace = [RunspaceFactory]::CreateRunspace()
    $runspace.ApartmentState = "STA"
    $runspace.ThreadOptions = "ReuseThread"
    $runspace.Open()
    $runspace.SessionStateProxy.SetVariable("sync", $sync)
    $rsjob.Runspace = $runspace
    $rsjob.BeginInvoke()
}

function StopMonitoring {
    Unregister-Event FileCreated
    Unregister-Event FileChanged
    $Output_RTB.SelectionColor = "Red"
    $Output_RTB.AppendText("Monitoring has Stopped`r`n")
    $Output_RTB.Select($Output_RTB.Text.Length, 0)
    $Output_RTB.ScrollToCaret()
}

    #
    #Start_Button
    #
    $Start_Button.BackColor = [System.Drawing.SystemColors]::Control
    $Start_Button.FlatAppearance.BorderColor = [System.Drawing.Color]::Blue
    $Start_Button.FlatAppearance.MouseOverBackColor = [System.Drawing.SystemColors]::GradientActiveCaption
    $Start_Button.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
    $Start_Button.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]12, [System.Int32]54))
    $Start_Button.Name = [System.String]'Start_Button'
    $Start_Button.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]130, [System.Int32]23))
    $Start_Button.TabIndex = [System.Int32]1
    $Start_Button.Text = [System.String]'Start Monitoring'
    $Start_Button.UseCompatibleTextRendering = $true
    $Start_Button.UseVisualStyleBackColor = $false
    $Start_Button.add_Click( {Start-RSJob})
    #
    #Stop_Button
    #
    $Stop_Button.BackColor = [System.Drawing.SystemColors]::Control
    $Stop_Button.FlatAppearance.BorderColor = [System.Drawing.Color]::Blue
    $Stop_Button.FlatAppearance.MouseOverBackColor = [System.Drawing.SystemColors]::GradientActiveCaption
    $Stop_Button.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
    $Stop_Button.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]12, [System.Int32]94))
    $Stop_Button.Name = [System.String]'Stop_Button'
    $Stop_Button.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]130, [System.Int32]23))
    $Stop_Button.TabIndex = [System.Int32]2
    $Stop_Button.Text = [System.String]'Stop Monitoring'
    $Stop_Button.UseCompatibleTextRendering = $true
    $Stop_Button.UseVisualStyleBackColor = $false
    $Stop_Button.add_MouseClick( { StopMonitoring })


Help greatly appreciated
Thanks
Damian
AnswerRe: PowerShell - FileSystemWatcher with GUI issue Pin
Gerry Schmitz11-Sep-19 17:26
mveGerry Schmitz11-Sep-19 17:26 
GeneralRe: PowerShell - FileSystemWatcher with GUI issue Pin
Damian Bz11-Sep-19 23:23
MemberDamian Bz11-Sep-19 23:23 
AnswerRe: PowerShell - FileSystemWatcher with GUI issue Pin
shreya313-Oct-19 3:17
Membershreya313-Oct-19 3:17 
QuestionForm appears hidden behind google maps in expanded mode. Pin
BibiStars6-Sep-19 5:11
MemberBibiStars6-Sep-19 5:11 
AnswerRe: Form appears hidden behind google maps in expanded mode. Pin
lw@zi 6-Sep-19 5:44
professional lw@zi 6-Sep-19 5:44 
GeneralRe: Form appears hidden behind google maps in expanded mode. Pin
BibiStars7-Sep-19 4:28
MemberBibiStars7-Sep-19 4:28 
GeneralRe: Form appears hidden behind google maps in expanded mode. Pin
Ryan Elias22-Oct-19 3:35
MemberRyan Elias22-Oct-19 3:35 
Questionsqlconnection Pin
Nithya MCA25-Aug-19 6:10
MemberNithya MCA25-Aug-19 6:10 
AnswerRe: sqlconnection Pin
Gerry Schmitz25-Aug-19 12:37
mveGerry Schmitz25-Aug-19 12:37 
QuestionOptimize Directory Parsing and TreeView Population Pin
Harley Burton13-Aug-19 4:16
MemberHarley Burton13-Aug-19 4:16 
AnswerRe: Optimize Directory Parsing and TreeView Population Pin
Richard Deeming13-Aug-19 4:51
communityengineerRichard Deeming13-Aug-19 4:51 
GeneralRe: Optimize Directory Parsing and TreeView Population Pin
Harley Burton13-Aug-19 11:41
MemberHarley Burton13-Aug-19 11:41 
GeneralRe: Optimize Directory Parsing and TreeView Population Pin
Richard Deeming13-Aug-19 23:35
communityengineerRichard Deeming13-Aug-19 23:35 
GeneralRe: Optimize Directory Parsing and TreeView Population Pin
Harley Burton14-Aug-19 5:52
MemberHarley Burton14-Aug-19 5:52 
AnswerMessage Closed Pin
21-Aug-19 0:35
professionalColith21-Aug-19 0:35 
AnswerMessage Closed Pin
21-Aug-19 0:41
professionalColith21-Aug-19 0:41 
QuestionWindows Service Installer Problem Pin
Kevin Marois7-Aug-19 7:14
professionalKevin Marois7-Aug-19 7:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.