Hi, I am writing a c# code that opens a file and searches for some condition and according to the condition that occuring tray icon changes.This should be done every 10 minutes.

I have coded this part well but I can not exit my application from a context menu item(exit) which never appears.

If I erase the line where my while loop appear, context menu appears and every thing goes well.

Do you have any suggestion where I might have made a mistake? I am a c# newbie and I cannot find the solution...

thanks for your help very much..


Here is the code:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Diagnostics;
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.ComponentModel.IContainer components;
private System.Boolean check;

public Form1()
{
this.check=true;
//this.HideApp();
//this.ShowInTaskbar = false;
this.components = new System.ComponentModel.Container();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
// Initialize contextMenu1
this.contextMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] {this.menuItem1});

// Initialize menuItem1
this.menuItem1.Index = 0;
this.menuItem1.Text = "E&xit";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

// Set up how the form should be displayed.
//this.ClientSize = new System.Drawing.Size(292, 266);

this.Text = "Notify Icon Example";

// Create the NotifyIcon.

this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
notifyIcon1.ContextMenu = this.contextMenu1;

// The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon1.Text = "Form1 (Situation controller)";
notifyIcon1.Visible = true;
notifyIcon1.Icon = new Icon("Icon2.ico");
while(this.check==true)
{
FileStream file = new FileStream("path.txt", FileMode.Open, FileAccess.Read,FileShare.Read);
StreamReader sr = new StreamReader(file);
string s;
bool control=false;
while((s = sr.ReadLine())!= null)
{
if(s.IndexOf("down")!=-1)
{
control=true;
}
}
if(control==true)
{
notifyIcon1.Icon = new Icon("Icon2.ico");
}
else
{
notifyIcon1.Icon = new Icon("Icon1.ico");
}
sr.Close();
file.Close();
//bu kısmı degistirdim
Thread.Sleep(1000*10*1);//1000*60*10
//this.Close();
//Application.Exit();
}
this.notifyIcon1.Dispose();
}

protected override void Dispose( bool disposing )
{
// Clean up any components being used.
if( disposing )
if (components != null)
components.Dispose();

base.Dispose( disposing );
}

private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
{
// Show the form when the user double clicks on the notify icon.

// Set the WindowState to normal if the form is minimized.
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;

// Activate the form.
this.Activate();
}

private void menuItem1_Click(object Sender, EventArgs e)
{
this.check=false;

notifyIcon1.Visible = false;

Application.Exit();

this.Close();
}

private void InitializeComponent()
{
//
// Form1
//
//this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
//this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.ShowInTaskbar = false;
this.Load += new System.EventHandler(this.Form1_Load);

}

private void Form1_Load(object sender, System.EventArgs e)
{

}
public void HideApp()
{
this.WindowState = FormWindowState.Minimized;
Hide();
}
public void ShowApp()
{
Show();
this.WindowState = FormWindowState.Normal;
}
[STAThread]

static void Main()
{
Application.Run(new Form1());

}



}
please if you know the solution my email is this:
[email protected]