Click here to Skip to main content
15,915,513 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: ERROR IN .NET FRAMEWORK - in Windows 10 - how to solve this? Pin
S3TH_201920-Oct-19 10:21
S3TH_201920-Oct-19 10:21 
GeneralRe: ERROR IN .NET FRAMEWORK - in Windows 10 - how to solve this? Pin
Eddy Vluggen20-Oct-19 10:28
professionalEddy Vluggen20-Oct-19 10:28 
GeneralRe: ERROR IN .NET FRAMEWORK - in Windows 10 - how to solve this? Pin
Dave Kreskowiak20-Oct-19 13:30
mveDave Kreskowiak20-Oct-19 13:30 
Question2D Arrays Pin
LilJokez_Gaming10-Oct-19 11:29
LilJokez_Gaming10-Oct-19 11:29 
AnswerRe: 2D Arrays Pin
Dave Kreskowiak10-Oct-19 13:42
mveDave Kreskowiak10-Oct-19 13:42 
QuestiondotNET performance surprises Pin
kalberts20-Sep-19 1:57
kalberts20-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
Damian 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 :
C#
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 :

C#
$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
Damian Bz11-Sep-19 22:23 
AnswerRe: PowerShell - FileSystemWatcher with GUI issue Pin
shreya313-Oct-19 2:17
shreya313-Oct-19 2:17 
QuestionForm appears hidden behind google maps in expanded mode. Pin
BibiStars6-Sep-19 4:11
BibiStars6-Sep-19 4:11 
AnswerRe: Form appears hidden behind google maps in expanded mode. Pin
dan!sh 6-Sep-19 4:44
professional dan!sh 6-Sep-19 4:44 
GeneralRe: Form appears hidden behind google maps in expanded mode. Pin
BibiStars7-Sep-19 3:28
BibiStars7-Sep-19 3:28 
GeneralRe: Form appears hidden behind google maps in expanded mode. Pin
Ryan Elias22-Oct-19 2:35
Ryan Elias22-Oct-19 2:35 
Questionsqlconnection Pin
Nithya MCA25-Aug-19 5:10
Nithya 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
Harley 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
Harley 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
Harley Burton14-Aug-19 4:52 
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 

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.