Click here to Skip to main content
14,303,648 members

.NET Framework

 
PinnedHOW TO ANSWER A QUESTION Pin
Chris Maunder16-Jul-09 3:09
adminChris Maunder16-Jul-09 3:09 
PinnedHow to get an answer to your question Pin
Chris Maunder16-Jul-09 3:05
adminChris Maunder16-Jul-09 3:05 
QuestiondotNET performance surprises Pin
Member 798912220-Sep-19 1:57
memberMember 798912220-Sep-19 1:57 
AnswerRe: dotNET performance surprises Pin
Gerry Schmitz20-Sep-19 5:45
mveGerry Schmitz20-Sep-19 5:45 
QuestionPowerShell - FileSystemWatcher with GUI issue Pin
Damian Bz10-Sep-19 22:48
memberDamian Bz10-Sep-19 22: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 16:26
mveGerry Schmitz11-Sep-19 16:26 
GeneralRe: PowerShell - FileSystemWatcher with GUI issue Pin
Damian Bz11-Sep-19 22:23
memberDamian Bz11-Sep-19 22:23 
QuestionForm appears hidden behind google maps in expanded mode. Pin
BibiStars6-Sep-19 4:11
memberBibiStars6-Sep-19 4:11 
AnswerRe: Form appears hidden behind google maps in expanded mode. Pin
lw@zi 6-Sep-19 4:44
professional lw@zi 6-Sep-19 4:44 
GeneralRe: Form appears hidden behind google maps in expanded mode. Pin
BibiStars7-Sep-19 3:28
memberBibiStars7-Sep-19 3:28 
Questionsqlconnection Pin
Nithya MCA25-Aug-19 5:10
memberNithya MCA25-Aug-19 5:10 
AnswerRe: sqlconnection Pin
Gerry Schmitz25-Aug-19 11:37
mveGerry Schmitz25-Aug-19 11:37 
QuestionOptimize Directory Parsing and TreeView Population Pin
Harley Burton13-Aug-19 3:16
memberHarley Burton13-Aug-19 3:16 
AnswerRe: Optimize Directory Parsing and TreeView Population Pin
Richard Deeming13-Aug-19 3:51
mveRichard Deeming13-Aug-19 3:51 
GeneralRe: Optimize Directory Parsing and TreeView Population Pin
Harley Burton13-Aug-19 10:41
memberHarley Burton13-Aug-19 10:41 
GeneralRe: Optimize Directory Parsing and TreeView Population Pin
Richard Deeming13-Aug-19 22:35
mveRichard Deeming13-Aug-19 22:35 
GeneralRe: Optimize Directory Parsing and TreeView Population Pin
Harley Burton14-Aug-19 4:52
memberHarley Burton14-Aug-19 4:52 
AnswerMessage Closed Pin
20-Aug-19 23:35
professionalColith20-Aug-19 23:35 
AnswerMessage Closed Pin
20-Aug-19 23:41
professionalColith20-Aug-19 23:41 
QuestionWindows Service Installer Problem Pin
Kevin Marois7-Aug-19 6:14
professionalKevin Marois7-Aug-19 6:14 
AnswerRe: Windows Service Installer Problem Pin
Richard Andrew x647-Aug-19 11:47
professionalRichard Andrew x647-Aug-19 11:47 
GeneralRe: Windows Service Installer Problem Pin
Kevin Marois7-Aug-19 12:13
professionalKevin Marois7-Aug-19 12:13 
Question.Net Core web tiff vieawer Pin
Member 145516927-Aug-19 3:55
memberMember 145516927-Aug-19 3:55 
AnswerRe: .Net Core web tiff vieawer Pin
Richard MacCutchan7-Aug-19 4:14
protectorRichard MacCutchan7-Aug-19 4:14 
GeneralRe: .Net Core web tiff vieawer Pin
Member 145516927-Aug-19 4:31
memberMember 145516927-Aug-19 4:31 

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.