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 8197001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:38:50+00:00 2026-06-07T05:38:50+00:00

I am having trouble creating a simple app in Visual C#, that does the

  • 0

I am having trouble creating a simple app in Visual C#, that does the following:

  1. Insistently seek for a specific process.
  2. When that process starts running, copy a resource to a folder.
  3. When the process ends, copy another resource to that folder.

I tried using timers+threads, with no success. I think that a BackgroundWorker would help here, but the documentation is rather unclear. Here is my current code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ResourceCopyApp
{
    public partial class MainWindow : Window {
        BackgroundWorker bw;
        bool pcRunning = false;
        public MainWindow() {
            InitializeComponent();
            bw = new BackgroundWorker();
            bw.DoWork += new DoWorkEventHandler(Loop);
            bw.RunWorkerAsync();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            cfgInstall.Text = Properties.Settings.Default.pcLocation;
            cfgLag.Value = Properties.Settings.Default.lag;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
            Properties.Settings.Default.pcLocation = cfgInstall.Text;
            Properties.Settings.Default.lag = (int)cfgLag.Value;
            Properties.Settings.Default.Save();
        }

        public void Loop(object sender, DoWorkEventArgs e) {
            pcRunning = false;
            bool pcWasRunning = pcRunning;
            if (pcRunning && !pcWasRunning) {
                MessageBox.Show("Process found");
                pcWasRunning = true;
                Swap(true);
            }
            foreach (Process p in Process.GetProcesses()) {
                if (p.ProcessName.Equals("Process")) {
                    pcRunning = true;
                }
            }
            if (!pcRunning && pcWasRunning) {
                MessageBox.Show("Process lost");
                Swap(false);
            }
        }
        public void Swap(bool v) {
            byte[] file;
            Thread.Sleep(new TimeSpan((long)cfgLag.Value));
            if (v) {
                file = Properties.Resources.NewRes;
            } else {
                file = Properties.Resources.BackupRes;
            }
            string path = cfgInstall.Text;
            if (!cfgInstall.Text.EndsWith("/") && !cfgInstall.Text.EndsWith("\\")) {
                path = cfgInstall.Text + "/test.txt";
            }
            File.WriteAllBytes(path, file);

        }
    }
}
  • 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-06-07T05:38:51+00:00Added an answer on June 7, 2026 at 5:38 am

    As I mentioned in the comments, your current code only runs the check once and then the BackgroundWorker finishes. You could either:

    • use a timer to look for the process intermittently (say every minute), along the lines of the code you currently have in Loop. When the process has started, you can then use Process.WaitForExit() to pause your code until the process finishes. To continue to have your program be responsive, this part should be in a BackgroundWorker. Of course, there is a risk with this method that the process starts and finishes quickly, between timer ticks.

    • Use ManagementEventWatcher within WMI. This article on CodeProject defines a class that simplifies using WMI to be notified when a process starts and stops:

      notePad = new ProcessInfo("notepad.exe");
      notePad.Started +=
          new Win32Process.ProcessInfo.StartedEventHandler(this.NotepadStarted);
      notePad.Terminated +=
          new Win32Process.ProcessInfo.TerminatedEventHandler(this.NotepadTerminated);
      

    The underlying code they are using is below:

      string queryString =
        "SELECT *" +
        "  FROM __InstanceOperationEvent " +
        "WITHIN  " + pol +
        " WHERE TargetInstance ISA 'Win32_Process' " +
        "   AND TargetInstance.Name = '" + appName + "'";
    
      // You could replace the dot by a machine name to watch to that machine
      string scope = @"\\.\root\CIMV2";
    
      // create the watcher and start to listen
      watcher = new ManagementEventWatcher(scope, queryString);
      watcher.EventArrived +=
              new EventArrivedEventHandler(this.OnEventArrived);
      watcher.Start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble creating a simple mixin that i plan to use on
I am having trouble creating an XML document that contains a default namespace and
I am having trouble creating a Web Service in Java that contains two methods
I am having a bit trouble creating some simple ajax rating based on these
I'm having a bit of trouble with an iPad app I'm creating. There is
Hi I'm having trouble compiling a simple piece of code. I am creating a
Having trouble creating a static library in XCode4. I have been following this helpful
I'm having troubles with creating a simple list (some expandable lists are already working).
I am having trouble creating a layout for a dialog I want to display
I'm having trouble creating an instance of a class using Python's re module. Here's

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.