Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6002855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:02:17+00:00 2026-05-23T01:02:17+00:00

I have a WPF application, i created a new class named Agent . On

  • 0

I have a WPF application, i created a new class named Agent. On my WPF application window i have ListBox. I call Agent from the MainWIndow.xaml.cs .

Agent class runs a FileSystemWatcher and now when OnChanged event is raised i want to add the message to the ListBox that the event was raised.

MainWindow.xaml

<Window x:Class="MachineLogAgentGUI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="411" Width="515">
<Grid Background="#0A000000">
    <Button Content="Run Agent" Height="26" HorizontalAlignment="Left" Margin="397,12,0,0" Name="runAgent" VerticalAlignment="Top" Width="86" Click="runAgent_Click" />
    <ListBox Margin="12,66,12,12" Name="messageBox" />
    <TextBox Height="26" HorizontalAlignment="Left" Margin="12,12,0,0" Name="observedDirectory" VerticalAlignment="Top" Width="261" />
    <Button Content="Browse" Height="26" HorizontalAlignment="Left" Margin="271,12,0,0" Name="browse" VerticalAlignment="Top" Width="75" Click="browse_Click" />
    <CheckBox Content="Include Subfolders" Height="16" HorizontalAlignment="Left" Margin="12,44,0,0" Name="includeSubfolders" VerticalAlignment="Top" />
</Grid>

In MainWindow.xaml.cs i have:

    Agent agent = null;

    private void runAgent_Click(object sender, RoutedEventArgs e)
    {
        if (agent == null || !agent._running)
        {
            agent = new Agent(@"W:\MindWare.SVN\CardMax2\trunk\ProcessEngine\TSPHelper.Producer\bin\Debug\GeneratedLogs");
            runAgent.Content = "Stop Agent";
        }
        else if (agent._running)
        {
            agent.StopAgent();
            runAgent.Content = "Run Agent";
        }
    }

And Agent class:

public class Agent
{
    private string Path { get; set; }
    public bool _running { get; set; }

    FileSystemWatcher watcher = new FileSystemWatcher();

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public Agent(string path)
    {
        Path = path;
        watcher.Path = path;
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        watcher.Filter = "*.txt";
        watcher.IncludeSubdirectories = true;

        watcher.Changed += new FileSystemEventHandler(OnChanged);

        watcher.EnableRaisingEvents = true;
        _running = true;
    }

    // Stop Agent
    public void StopAgent()
    {
        watcher.EnableRaisingEvents = false;
        _running = false;
    }

    // Define the event handlers.
    private void OnChanged(object source, FileSystemEventArgs e)
    {        
        try
        {
            watcher.EnableRaisingEvents = false;
            _running = false;

            // DO SOMETHING HERE
            // Add item to ListBox on MainWindow somehow
        }
        finally
        {
            watcher.EnableRaisingEvents = true;
            _running = true;
        }
    }

How i could do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:02:18+00:00Added an answer on May 23, 2026 at 1:02 am

    I think I understand what you mean.

    You could create a static MainWindow object in the Agent class:

    public class Agent
    {
    public static MainWindow mainWindow;
    
    ....
    }
    

    Then inside the MainWindow.cs you can add your window to the variable

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      Agent.mainWindow = this;
      ....
    }
    

    Now when ever you need to access this you can reference mainWindow:

    public class Agent
    {
    public static MainWindow mainWindow;
    
    public void AddToList(string value)
    {
       mainWindow.listBox1.items.add(value);
    }
    

    Edit: Update for Threading error

    Sounds like the FileSystemWatcher runs on a seperate thread to the UI. you can manage this by getting passing the work to the UI thread

    // DO SOMETHING HERE
    // Add item to ListBox on MainWindow somehow
    if (!mainWindow.Dispatcher.CheckAccess())
        {
           mainWindow.Dispatcher.BeginInvoke(
               System.Windows.Threading.DispatcherPriority.Normal,
               new Action(
                  delegate()
                  {
                   //Code to make changes to the mainWindow if the thread does not have access:
                    mainWindow.listBox1.Items.Add("hello");                                
                  }));
        }
        else
        {
            //Access allowed make changes normally.
            mainWindow.listBox1.Items.Add("hello");
        }
    

    Martyn

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF application that runs fine under XP as an administrator. When
I have a FlowDocument in a standard WPF application window where I have some
I have wrote a WPF application that reads a list of movies from a
I have a WPF application in VS 2008 with some web service references. For
I have a WPF application using Aero Glass. When using the application under a
Please help! Background info I have a WPF application which accesses a SQL Server
I have a simple WPF application which I am trying to start. I am
I have written a WPF application that I want to port to Silverlight 2.
I have a simple WPF application with a menu. I need to add menu
I have a very simple WPF application in which I am using data binding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.