How to pass objects back from Backgroundworker thread: Tuple Class

http://thegrayzone.co.uk/blog/2010/07/background-worker-and-multiple-parameters/

 using System;
using System.ComponentModel;

private void StartBackgroundWorker()
{
  BackgroundWorker worker = new BackgroundWorker();

  worker.DoWork += new DoWorkEventHandler(worker_DoWork);
  worker.RunWorkerCompleted += new  RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
 
  // Declare Tuple object to pass multiple params to DoWork method.
  var params = Tuple.Create<int, DateTime, bool>(44, DateTime.Now, false);
  worker.RunWorkerAsync(params);
}

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
  // Get Tuple object passed from RunWorkerAsync() method
  Tuple<int, DateTime, bool> params = e.Argument as Tuple<int, DateTime, bool>

  // Do some long running process
 
  // Set the result using new Tuple object to pass multiple objects to completed event handler
  e.Result = Tuple.Create<CustomObject, bool, string>(customObj, true, “hello”);
}

private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
  // Get result objects
  Tuple<CustomObject, bool, string> res = e.Result as Tuple<CustomObject, bool, string>
}

http://msdn.microsoft.com/en-us/library/system.tuple.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

 

Closing Forms in VB.NET

This code finds all open forms and closes them one by one. Then it exits the application. This ensure nothing is left behind leaching memory.

http://bytes.com/topic/visual-basic-net/answers/507363-close-all-forms-but-main

dim Success as Boolean
do
success = true
try
for each f as form in my.application.openforms
if f.name <> “Main” then f.close()
next f
catch ex as exception
success = false
end try
loop until success

Me.close()
Application.Exit()

 

Another note about the above code:http://www.vbforums.com/archive/index.php/t-255285.html

Application.Exit() does NOT raise the Form.Close() event and can leave processes of a Form running in the memory ( thus is classed as unsafe ) , before running Application.Exit() you must use Form.Close() on each Form. here’s a quote direct from MSDN …

CAUTION The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.