0

I'm trying to develop a WPF application that runs other process in background when a button is pressed (and stop it when the same button is pressed again).

Well, the point here is that the process that I'm calling it's going to be monitorizing a folder, so, it doesn't ends at any point.

I tried with Threads, but when I create a new thread object when I press the button, I can't access to it when I press it again because there're in different codeblocks.

I rode that the better way to do this is using BackgroundWorker, but I don't understand how to use it.

This is the code that I have right now. mon is the object created that have the function that I want to run in background (mon.MonitoriceDirectory)

if (this.monitoring)
{
    var dialog = new System.Windows.Forms.FolderBrowserDialog();
    dialog.ShowNewFolderButton = false;
    System.Windows.Forms.DialogResult result = dialog.ShowDialog();
    if (dialog.SelectedPath != "")
    {
         monitorizeButton.Content = "Stop";
         textBlockMonitorize.Text = "Monitoring...";
         this.monitorizando = false;
         mon.monitorizePath = dialog.SelectedPath;
         Thread newThread = new Thread(mon.MonitorizeDirectory);
         newThread.Start();
    }
}
else
{
    newThread.Abort(); // Here is the problem, I can't access to that cuz
                      // it's in another codeblock.
    monitorizeButton.Content = "Monitorice";
    textBlockMonitorize.Text = "Ready";
    this.monitorizando = true;
}
  • 3
    You answered your own question. Set the value outside the method (inside the class). – kevintjuh93 Oct 07 '15 at 10:12
  • Have you looked at Task Cancellation with the Task Parallel Library? It is the current gold standard in cancelling asynchronous work https://msdn.microsoft.com/en-us/library/dd997396%28v=vs.110%29.aspx – Gusdor Oct 07 '15 at 10:14
  • @Polyfun Better now? The program is in spanish, so excuse me 4 my english. – Fernando Gallego Fernández Oct 07 '15 at 10:29
  • No offence meant, your English is much better than my Spanish ;-). – Polyfun Oct 07 '15 at 10:35
  • It's off topic, but isn't [FileSystemWatcher](https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx) what you need to monitorize you folder? :) – Liero Oct 07 '15 at 11:01
  • @Liero Yes, `FileSystemWatcher` is used by the `mon` object at the `mon.MonitoriceDirectory` function using this sentence `FileSystemWatcher watcher = new FileSystemWatcher();` and filling the parameters of the event `onCreated` – Fernando Gallego Fernández Oct 07 '15 at 11:23
  • 1
    but as far as I know, FileSystemWatcher doesn't need extra thread. If you want to stop it, just set EnableRaisingEvents to false or dispose it. – Liero Oct 08 '15 at 09:58
  • @Liero Yep, but the thread isn't only watching for changes in a directory. It also process the contents of the files added to that directory and updating one DDBB with the data. Thanks for the help! – Fernando Gallego Fernández Oct 08 '15 at 10:54
  • Events raised by FileSystemWatcher runs on separate thread, so anything you do in the eventhandlers like onCreated won't block your main thread. http://stackoverflow.com/questions/11492720/does-filesystemwatcher-create-its-own-thread – Liero Oct 08 '15 at 11:00

1 Answers1

0

By declaring newThread out side the if block help you to extend is scope to the else part also; so You can try this,

  Thread newThread;
  if (this.monitorizing)
  {
    var dialog = new System.Windows.Forms.FolderBrowserDialog();
    //rest of code here 
    newThread = new Thread(mon.MonitorizeDirectory);
    //Rest of code
  }
 else
  {
    newThread.Abort();
    //Rest of code here 
  }
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88