Events raised from other threads

  • Thread starter Thread starter John Paulsson
  • Start date Start date
J

John Paulsson

I'm interested knowing best design principles for this "issue". I've got
a Windows form application which processes and updates some FTP files in
a predefined way. On an unexpected disconnect, an event is raised from
the client thread. When this happens, I want the Transaction() to abort
its process and instead do some other stuff inside the main thread.

Please have a look at the following pseudo-code:

private FTPClient m_ftp = null;
private ftphandle m_h = null;

Form1::OnLoad()
{
m_ftp = new FTPClient();
ftp.OnDisconnect += new FTPEventHandler(OnDisconnect);
m_h = ftp.Connect("127.0.0.1", "/files");

Transaction();
}

Form1::Transaction()
{
// Main thread

PrepareFiles(h);
AdjustFiles(h);
ProcessFiles(h);
CompleteFiles(h);
}

App:OnDisconnect()
{
// Event raised from a new thread created by FTPClient object
frmStatus.ShowModal();
}

If OnDisconnect is called, and I show this frmStatus form, the code
inside the Transaction function will still continue to run in the
background and try to interact using Form1.

Should I set a boolean member variable inside the OnDisconnect handler
and check it after every row in the Transaction function AND inside the
function it calls, and let them determine wheter Transaction should
abort or not, run Transaction() in a third thread and just kill it on
Disconnect, what's your suggestion?
 
John,

What I would do is have a flag that is set when the event is fired.
Then, in each of your methods, check the flag to see if the event fired. If
it did, then do nothing.

However, there are times it seems in your processing that you might not
care if you are disconnected anymore (for example, after you have downloaded
all the files, what do you care if you are disconnected?). You might want
to consider those cases as well.

Hope this helps.
 
Thank's for your reply Nicholas! I'm processing the files directly on
the ftp server and need to stay connected through out the process.

I'll use a flag as you suggested together with some exception handling
then to abort processing on disconnects. Thank you.
 
Back
Top