Windows Application
Form1.cs, Form1.Designer.cs, Program.cs
Form1.Designer.cs
namespace ex2
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name=”disposing”>true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.loadButton = new System.Windows.Forms.Button();
searchWord = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.filePath = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.searchButton = new System.Windows.Forms.Button();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
statusBox = new System.Windows.Forms.ListBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// loadButton
//
this.loadButton.Location = new System.Drawing.Point(12, 56);
this.loadButton.Name = “loadButton”;
this.loadButton.Size = new System.Drawing.Size(75, 23);
this.loadButton.TabIndex = 1;
this.loadButton.Text = “Load”;
this.loadButton.UseVisualStyleBackColor = true;
this.loadButton.Click += new System.EventHandler(this.loadButton_Click);
//
// searchWord
//
searchWord.Location = new System.Drawing.Point(10, 26);
searchWord.Name = “searchWord”;
searchWord.Size = new System.Drawing.Size(130, 20);
searchWord.TabIndex = 2;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.filePath);
this.groupBox1.Controls.Add(this.loadButton);
this.groupBox1.Location = new System.Drawing.Point(15, 16);
this.groupBox1.Name = “groupBox1″;
this.groupBox1.Size = new System.Drawing.Size(261, 90);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Text = “File”;
//
// filePath
//
this.filePath.Location = new System.Drawing.Point(15, 25);
this.filePath.Name = “filePath”;
this.filePath.Size = new System.Drawing.Size(226, 20);
this.filePath.TabIndex = 2;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.searchButton);
this.groupBox2.Controls.Add(searchWord);
this.groupBox2.Location = new System.Drawing.Point(17, 124);
this.groupBox2.Name = “groupBox2″;
this.groupBox2.Size = new System.Drawing.Size(258, 61);
this.groupBox2.TabIndex = 5;
this.groupBox2.TabStop = false;
this.groupBox2.Text = “Word”;
//
// searchButton
//
this.searchButton.Location = new System.Drawing.Point(164, 25);
this.searchButton.Name = “searchButton”;
this.searchButton.Size = new System.Drawing.Size(75, 23);
this.searchButton.TabIndex = 3;
this.searchButton.Text = “Search”;
this.searchButton.UseVisualStyleBackColor = true;
this.searchButton.Click += new System.EventHandler(this.searchButton_Click);
//
// groupBox3
//
this.groupBox3.Controls.Add(statusBox);
this.groupBox3.Location = new System.Drawing.Point(18, 205);
this.groupBox3.Name = “groupBox3″;
this.groupBox3.Size = new System.Drawing.Size(256, 164);
this.groupBox3.TabIndex = 6;
this.groupBox3.TabStop = false;
this.groupBox3.Text = “Status”;
//
// openFileDialog1
//
this.openFileDialog1.FileName = “openFileDialog1″;
//
// statusBox
//
statusBox.FormattingEnabled = true;
statusBox.Location = new System.Drawing.Point(12, 24);
statusBox.Name = “statusBox”;
statusBox.Size = new System.Drawing.Size(226, 121);
statusBox.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 381);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = “Form1″;
this.Text = “Form1″;
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button loadButton;
static System.Windows.Forms.TextBox searchWord;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox filePath;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button searchButton;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
static System.Windows.Forms.ListBox statusBox;
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace ex2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static List<String> searchResults = new List<String>();
public class SearchTask
{
public Thread s_thread;
public string f_name;
public SearchTask(string file)
{
f_name = file;
s_thread = new Thread(new ThreadStart(SearchWord));
}
private void SearchWord()
{
FileStream fs = new FileStream(f_name,FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string s = sr.ReadToEnd();
string[] fileWords = s.Split(‘ ‘);
searchResults.Add(” ” + fileWords);
foreach(string word in fileWords)
{
if (word == searchWord.Text)
{
//searchResults.Add(“”);
searchResults.Add(“Cuvant gasit”);
}
else
searchResults.Add(“Cuvant nu gasit”);
}
}
}
private void loadButton_Click(object sender, EventArgs e)
{
System.Windows.Forms.DialogResult OPEN = openFileDialog1.ShowDialog();
if (OPEN == DialogResult.OK)
{
String path = openFileDialog1.FileName; //The Path to the file//
filePath.Text = path;
}
}
private void searchButton_Click(object sender, EventArgs e)
{
string word = searchWord.Text;
string path = filePath.Text;
//MessageBox.Show(path);
//string dest = path.Split(“.”, 1);
string dest = @”C:\Documents and Settings\admin\Desktop\SD\”;
int index = 0;
byte[] segment = new byte[30];
using (Stream source = File.OpenRead(path))
{
while (source.Position < source.Length)
{
index++;
// Create a new sub File, and read into it
string newFileName = Path.Combine(dest, Path.GetFileNameWithoutExtension(path));
newFileName += index.ToString() + Path.GetExtension(path);
using (Stream destination = File.OpenWrite(newFileName))
{
while (destination.Position < segment.Length)
{
int bytes = source.Read(segment, 0, segment.Length);
destination.Write(segment, 0, bytes);
// Are we at the end of the file?
if (bytes < segment.Length)
{
break;
}
}
}
}
}
//MessageBox.Show(index.ToString());
List<SearchTask> tasks = new List<SearchTask>();
for (int i = 1; i <= index; i++)
{
string pathToFile = Path.GetFileNameWithoutExtension(filePath.Text) + i + Path.GetExtension(filePath.Text);
SearchTask task = new SearchTask(pathToFile);
tasks.Add(task);
}
foreach(SearchTask t in tasks)
{
t.s_thread.Start();
for (int i = 0; i < searchResults.Count(); i++)
statusBox.Items.Add(searchResults[i]);
}
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace ex2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Description
Sa se creeze o applicatie desktop care sa permite incarcarea unui fisier text , specificarea unui cuvant , si care sa caute acel cuvant in fisierul text, prin segmentarea fisierului in mai multe bucati si cautand in paralel (cu thrad-uri pt fiecare segment) acel cuvant. De asemenea sa se afiseze info de debug in form de genul : de cate ori s-a gasit cuvantul , la ce pozitii, timp de inceput cautare, timp de sfarsit cautare pt fiecare thread in parte.
Filed under: Samples | Leave a Comment »