I have an application that watches a directory for changes. When a file (a log file) is created, it analyses its contents, writes the results in a form (that exists already and has been initialized, although may be currently hidden) and finally shows this form to the user.
When the application starts, just an icon is shown in the task bar. The main method just creates the icon in the task bar and initializes the class that watches/analyses and controls the form with the results.
public static void Main(string[] args) {
NotificationIcon notificationIcon = new NotificationIcon();
notificationIcon.notifyIcon.Visible = true;
if (notificationIcon.Init()) {
MainForm = ResultForm.GetInstance();
Application.Run();
}
}
"ResultForm" is the class I just mentioned before and has the following methods relevant to the problem:
public static ResultForm GetInstance() {
// _this is an attribute from the class. Is used to work always
// with just one instance of the classe
if (_this==null)
_this= new ResultForm();
return _this;
}
private ResultForm() {
// initialization of the GUI form
InitializeComponent();
[...]
// watcher for the log files
logsWatcher= new FileSystemWatcher(LOGFILES_FOLDER);
logsWatcher.Created += new FileSystemEventHandler(NewFile);
logsWatcher.EnableRaisingEvents=true;
logsWatcher.SynchronizingObject = this;
}
private void NewFile (object source, FileSystemEventArgs e) {
// make sure the file is of the correct type
[...]
// perform some analysis on the file
[...]
// update the contents in the form (some TreeViews and labels)
[...]
// show the form to the user
_this.Show();
}
And now comes the problem. If the application starts, a file is analysed and the main form has not been shown yet, it will be shown as "Not responding" when the analysis is complete, although everything has been already done. If a new file is created after, it will be successfully analysed, although the form will stay in this "Not responding" state.
HOWEVER, if the form has been opened at least once before since the application started (say, you double-clicked on the icon, the form was shown and you closed it (or left it open, it does not matter)), everything will work smoothly.
As a workaroud, I can modify the main with the following two lines (before the Run()
method), so the form will be shown at least once before any file comes:
MainForm.Show();
MainForm.Hide();
(I hide it back because it should not be visible until an analysis has been performed or the user has explictly clicked on the icon.) Other than that, there is no difference in the program: the work done is the same and the form is always shown once everything is done. I have made sure with the debugger that the end of the method is reached during execution.
How can I solve this problem without the mentioned workaround?
I have tried creating a thread for the analysis, using Application.DoEvents()
or blocks of code similar to this one. In the best of the cases, the form shows all its contents properly, but stays in a "Not responding" state. I have also tried to just leave the Show()
call in the method with exactly the same results, which tells me it is not a problem of heavy load, but of something I may be doing wrong.
EDIT Since @thecoon requested it, I have uploaded a small project that reproduces the problem. It has been done with SharpDevelop, in case you also use it. --> http://dl.dropbox.com/u/1153417/test.zip
There is a small explanation in the Main() method.