I am creating an application that has a thread which constantly moves the contents of one folder to another (Using Visual Studio, Compact Framework)
I start the thread like this
public static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///
[MTAThread]
public static void Main()
{
SendXML ThreadToSendXML = new SendXML();
Thread2 SecondaryThread = new Thread2(ThreadToSendXML.Run);
SecondaryThread.Start();
Application.Run(new frmLogin());
}
}
This immediately executes the thread when the program compiles. I then have the thread defined within a class called "Send XML".
public class SendXML
{
/// <summary>
/// Variable to hold the temporary directory
/// </summary>
public string TempDirectory = @"\Program Files\smartdeviceproject1\TempXML\IntoContainer\";
/// <summary>
/// Variable to hold the send folder filepath
/// </summary>
public string SendDirectory = @"\Program Files\smartdeviceproject1\SendXML";
/// <summary>
/// Lock covering stopping and stopped
/// </summary>
readonly object stopLock = new object();
/// <summary>
/// Whether or not the worker thread has been asked to stop
/// </summary>
public static bool stopping = false;
/// <summary>
/// Whether or not the worker thread has stopped
/// </summary>
public static bool stopped = false;
/// <summary>
/// Returns whether the worker thread has been asked to stop.
/// This continues to return true even after the thread has stopped.
/// </summary>
public bool Stopping
{
get
{
lock (stopLock)
{
return stopping;
}
}
}
/// <summary>
/// Returns whether the worker thread has stopped.
/// </summary>
public bool Stopped
{
get
{
lock (stopLock)
{
return stopped;
}
}
}
/// <summary>
/// Tells the worker thread to stop, typically after completing its
/// current work item. (The thread is *not* guaranteed to have stopped
/// by the time this method returns.)
/// </summary>
public void Stop()
{
lock (stopLock)
{
stopping = true;
}
}
/// <summary>
/// Called by the worker thread to indicate when it has stopped.
/// </summary>
void SetStopped()
{
lock (stopLock)
{
stopped = true;
}
}
/// <summary>
/// Main work loop of the class.
/// </summary>
public void Run()
{
try
{
while (!Stopping)
{
FileInfo[] rgFiles;
///Defines the contents of the TempDirectory as "di"
DirectoryInfo di = new DirectoryInfo(TempDirectory);
rgFiles = di.GetFiles("*.xml");
foreach (FileInfo s in rgFiles)
{
string SourceFile = s.Name;
string TargetFile = Path.Combine(SendDirectory, SourceFile);
File.Move(s.FullName, TargetFile);
}
Thread.Sleep(250);
}
}
finally
{
SetStopped();
}
}
}
This runs fine and does everything I want it to, the problem occurs when I want to exit the program, the threads keeps running (presumerbly because I haven't joined it to the main program thread). I've tired Application.Exit() on a button's click event but to no avail (I saw it kept moving files even after the UI had exited). In short, I need to either join or terminate the thread "SecondaryThread" from another class. Any help would be much appreciated, I am rather new to threading.