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;
}