Winfprm Application Hangs on exit.


L

Learning.Net

hi ,

I have a application which reads files, directory,and its version and
version information is written to text file.Its working fine if files
in directory are less but problem arises when no of file increases.
Its hangs and on pressing exit button it showing application not
responding..
Plz help me out
Here is da sample code::::::

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Collections.Specialized;
using System.Threading;



namespace FileVersionUitility
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblSourceName;
private System.Windows.Forms.TextBox txtBFileInfo;
private System.Windows.Forms.Button btnVersion;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
public string DirSourcePath ,DirDestinationPath;
// FileInfo[] files;
public static StreamWriter writer;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label lblDestinationPath;
private System.Windows.Forms.TextBox txtBDestPath;
private System.Windows.Forms.Button btn2Browse;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblSourceName = new System.Windows.Forms.Label();
this.txtBFileInfo = new System.Windows.Forms.TextBox();
this.btnVersion = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.btn2Browse = new System.Windows.Forms.Button();
this.txtBDestPath = new System.Windows.Forms.TextBox();
this.lblDestinationPath = new System.Windows.Forms.Label();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// lblSourceName
//
this.lblSourceName.Location = new System.Drawing.Point(16, 48);
this.lblSourceName.Name = "lblSourceName";
this.lblSourceName.Size = new System.Drawing.Size(104, 23);
this.lblSourceName.TabIndex = 0;
this.lblSourceName.Text = "Source Path:";
this.lblSourceName.TextAlign =
System.Drawing.ContentAlignment.MiddleRight;
//
// txtBFileInfo
//
this.txtBFileInfo.AutoSize = false;
this.txtBFileInfo.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.txtBFileInfo.ForeColor = System.Drawing.Color.Wheat;
this.txtBFileInfo.Location = new System.Drawing.Point(120, 48);
this.txtBFileInfo.Name = "txtBFileInfo";
this.txtBFileInfo.ReadOnly = true;
this.txtBFileInfo.Size = new System.Drawing.Size(224, 24);
this.txtBFileInfo.TabIndex = 1;
this.txtBFileInfo.Text = "";
//
// btnVersion
//
this.btnVersion.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnVersion.Location = new System.Drawing.Point(16, 144);
this.btnVersion.Name = "btnVersion";
this.btnVersion.Size = new System.Drawing.Size(104, 23);
this.btnVersion.TabIndex = 2;
this.btnVersion.Text = "Get File Version";
this.btnVersion.Click += new
System.EventHandler(this.btnVersion_Click);
//
// button1
//
this.button1.BackColor = System.Drawing.Color.OldLace;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button1.Location = new System.Drawing.Point(360, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "Browse";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// groupBox1
//
this.groupBox1.BackColor = System.Drawing.Color.OldLace;
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.btn2Browse);
this.groupBox1.Controls.Add(this.txtBDestPath);
this.groupBox1.Controls.Add(this.lblDestinationPath);
this.groupBox1.Controls.Add(this.lblSourceName);
this.groupBox1.Controls.Add(this.txtBFileInfo);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.btnVersion);
this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 8.25F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.groupBox1.Location = new System.Drawing.Point(24, 48);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(472, 192);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "File Version Information";
//
// button2
//
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button2.Location = new System.Drawing.Point(360, 144);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 23);
this.button2.TabIndex = 7;
this.button2.Text = "Exit";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// btn2Browse
//
this.btn2Browse.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btn2Browse.Location = new System.Drawing.Point(360, 96);
this.btn2Browse.Name = "btn2Browse";
this.btn2Browse.TabIndex = 6;
this.btn2Browse.Text = "Browse";
this.btn2Browse.Click += new
System.EventHandler(this.btn2Browse_Click);
//
// txtBDestPath
//
this.txtBDestPath.AutoSize = false;
this.txtBDestPath.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.txtBDestPath.Location = new System.Drawing.Point(120, 96);
this.txtBDestPath.Name = "txtBDestPath";
this.txtBDestPath.ReadOnly = true;
this.txtBDestPath.Size = new System.Drawing.Size(224, 24);
this.txtBDestPath.TabIndex = 5;
this.txtBDestPath.Text = "";
//
// lblDestinationPath
//
this.lblDestinationPath.Location = new System.Drawing.Point(16,
96);
this.lblDestinationPath.Name = "lblDestinationPath";
this.lblDestinationPath.Size = new System.Drawing.Size(104, 23);
this.lblDestinationPath.TabIndex = 4;
this.lblDestinationPath.Text = "Destination Path:";
this.lblDestinationPath.TextAlign =
System.Drawing.ContentAlignment.MiddleRight;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.OldLace;
this.ClientSize = new System.Drawing.Size(528, 278);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnVersion_Click(object sender, System.EventArgs e)
{

if(txtBFileInfo.Text=="")
{
MessageBox.Show("Source Path Missing!! Please Select The Directory
Path ","Error report",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else if(txtBDestPath.Text=="")
{
MessageBox.Show("Destination Path Missing!! Please Select The
Destination Path ","Error
report",MessageBoxButtons.OK,MessageBoxIcon.Error);
}



// FileVersionInfo myFileVersionInfo;
// DirectoryInfo root = new DirectoryInfo(DirSourcePath);
// DirectoryInfo[] dirs = root.GetDirectories();
// files = root.GetFiles();
// string fileName;
// foreach (FileInfo file in files)
// {
// fileName = file.FullName;
//// MessageBox.Show(file.FullName);
// myFileVersionInfo =
FileVersionInfo.GetVersionInfo(fileName);
//
// MessageBox.Show("File Name: " + file.Name + " Version
Info : "+ myFileVersionInfo.FileVersion);
// writer.WriteLine("File Name: " + file.Name + " Version
Info : "+ myFileVersionInfo.FileVersion);
//
// }
else
{
writer = new StreamWriter(DirDestinationPath);
//Create a Directory object using DirectoryInfo
// writer.WriteLine("File Name :" + " Version " + "Location");
DirectoryInfo dir =
new DirectoryInfo(DirSourcePath);
//Pass the Directory for displaying the contents
getDirsFiles(dir);


writer.Close();
MessageBox.Show("File is Created.......");
}



}

private void button1_Click(object sender, System.EventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new
FolderBrowserDialog();
folderBrowserDialog.ShowDialog();
txtBFileInfo.Text = folderBrowserDialog.SelectedPath;
DirSourcePath =txtBFileInfo.Text;
}

public static void getDirsFiles(DirectoryInfo d)
{
//create an arrakmly of files using FileInfo object
FileInfo [] files;
//get all files for the current directory
files = d.GetFiles("*.*");
ArrayList arr = new ArrayList();
//iterate through the directory and print the files
foreach (FileInfo file in files)
{
//get details of each file using file object
String fileFullName = file.FullName;
String fileName = file.Name;
String fileSize = file.Length.ToString();
String fileExtension =file.Extension;
String fileCreated = file.LastWriteTime.ToString();
FileVersionInfo fileVersion =
FileVersionInfo.GetVersionInfo(fileFullName);
if(fileVersion.FileVersion!="")
{
arr.Add(fileName + " Version Details:: " +
fileVersion.FileVersion);
}
else
{
arr.Add(fileName + "Last Modified date :" +
file.LastWriteTime);
}
// arr.Add(fileName);
// arr.Add(fileVersion.FileVersion);
arr.Sort();

//
// Console.WriteLine(fileName + " " + fileSize +
// " " + fileExtension + " " + fileCreated);

// writer.WriteLine(fileName + " " + fileSize + " " +
fileExtension + " "
// + fileCreated + "Version :" + fileVersion.FileVersion);
// writer.WriteLine(fileName + " " +
fileVersion.FileVersion + " " + fileFullName);

}

foreach( string s in arr)
{
writer.WriteLine(s);
}
//get sub-folders for the current directory
DirectoryInfo [] dirs = d.GetDirectories("*.*");

//This is the code that calls
//the getDirsFiles (calls itself recursively)
//This is also the stopping point
//(End Condition) for this recursion function
//as it loops through until
//reaches the child folder and then stops.
foreach (DirectoryInfo dir in dirs)
{
// Console.WriteLine("--------->> {0} ", dir.Name);
writer.WriteLine(dir.FullName);
writer.WriteLine();
getDirsFiles(dir);
}

}

private void btn2Browse_Click(object sender, System.EventArgs e)
{
saveFileDialog1.Filter = "Text files(*.txt)|*.txt";
saveFileDialog1.ShowDialog();
DirDestinationPath = saveFileDialog1.FileName;
txtBDestPath.Text = DirDestinationPath;
}

private void button2_Click(object sender, System.EventArgs e)
{
Application.Exit();
this.Close();
}

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

}
}
}
 
Ad

Advertisements

M

Moty Michaely

Hi,

I would firstly suggest doing all the time consuming work in a
background thread.
You can easily use the BackgroundWorker component:
http://msdn2.microsoft.com/en-us/library/8xs8549b.aspx

If you don't want to do that, you should be able to pump the message
queue in the middle of the time consuming job. Insert an
Application.DoEvents() call in your foreach loop to pump the messages
(such as the button Click).

Feel free to ask any further questions.
Cheers,
Moty.
 
Ad

Advertisements

L

Learning.Net

Hi,

I would firstly suggest doing all the time consuming work in a
background thread.
You can easily use the BackgroundWorker component:http://msdn2.microsoft.com/en-us/library/8xs8549b.aspx

If you don't want to do that, you should be able to pump the message
queue in the middle of the time consuming job. Insert an
Application.DoEvents() call in your foreach loop to pump the messages
(such as the button Click).

Feel free to ask any further questions.
Cheers,
Moty.

Thanx for da help,If possible can u Suggest with some code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top