I am having a hard time creating a class where I can use ListBox
to log events.
I know i have tonnes of articles on what I am asking on SO and google but that i didn't make out of it. So i am asking a little help here:
I have three classes:
1: WinForm - Where my list box is placed. From here I pass my listbox in the FileEnumeratorClass()
constructor.
2: FileEnumeratorClass - Recieving listbox here and then passing it to logger class.
class FileEnumeratorClass {
UILogger logger;
/// <summary>
/// ctor
/// </summary>
public FileEnumeratorClass (ListBox lbLog)
{
logger = new UILogger(lbLog);
}
/// <summary>
/// Figures out what has changed between the src and destination
/// What is currently implemented does not work......the solution is to compare SRC and DESTINATION for the first time.
/// And verify with end user.
/// </summary>
public List<FileDetails> IdentifyChanges()
{
logger.AddToLog("Change detection initiated...");
//Deletes any existing cached package. Assuming it is in incosistent form
logger.AddToLog( "Cleaning up any cached local package.");
DeleteLocalPackage();
}
}
3: UILogger
public class UILogger
{
public UILogger(ListBox lb)
{
lbLog = lb;
}
// This delegate enables asynchronous calls for setting
// the text property on a control.
delegate void AddToLogCallback(string text);
public void AddToLog( string message)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (lbLog.InvokeRequired)
{
var d = new AddToLogCallback(AddToLog);
lbLog.Invoke(d, new object[] { message });
}
else
{
// add this line at the top of the log
lbLog.Items.Insert(0, message);
}
// keep only a few lines in the log
while (lbLog.Items.Count > 1000)
{
lbLog.Items.RemoveAt(lbLog.Items.Count - 1);
}
}
}
But the above code does not work as expected. All showing up when thread is done. What I need is to call the methods AddToLog()
in the same sequence as they are written/called in FileEnumeratorClass -> IdentifyChanges().