Burak Ozdemir 's Blog
Computer Science & Softwares - Latest Technology


Where people meet new technology !


Threading in C#

Programming 1 Comment »

What is Threading?
-Parallel execution of code pieces on CPU, commonly compiled codes started in a single thread but it is possible to create multi-threads within a program.

Why Threading?
-Because threading is really useful, and it is used in nearly all kind of serious softwares, threads enables us to do independent jobs simultaneously, and it is sometimes hard to handle, if it is done with experienced hands, it enhances the software greatly. I want to give a simple example, consider you want your program to check for available updates and install them without interrupting the user’s work. This is only done with threads, while user working with your software, another thread connects to Internet and checks available updates without blocking the current execution of the software.

A simple threading example with C# :

Thread test_thread;

public void Thread_Task()
{
for(int i=0;i<100:++i)
TextBox1.Text=Convert.ToString(i);
}
private void button1_Click(object sender, EventArgs e)
{
test_thread=new Thread(new ThreadStart(this.Thread_Task));
test_thread.IsBackground = true;
test_thread.Start();
}

In the above simple code piece, we created a thread object and a thread task function, then we assigned task function to thread object in a button_click function, then we started the thread, as you notice, if user clicks the button a new thread starts and show the value of i in the TextBox1 without blocking the program, user may continue working and at the same time values of i are showed in the TextBox1. This was a sample example to demonstrate threads created and used, besides there is a vast usage and restrictions for threads, for example there will be a problem what if two threads wants to access the same resource and wants to edit, there are lots of synchronization problems and techniques.

A* Search Algorithm / C# Implementation

Programming 1 Comment »

What is A* Search?
-A kind of informed search algorithm used in agent based systems.

I am here to put a C# implementation of it, I do not care if someone is interested in or not, I feel I could give this away, do what you want with it… :D Comments are welcome as always.

Download

Getting MX record from DNS

Programming No Comments »

First of all, if you downloaded and tried the Tiny Mailer (Download link is below), you should be realized that program making a direct connection to Mail Exchange servers and sending your mail. But how to obtain the ip address of the mail exchange server from the given receiver’s mail address.The trick is there, if you know the ip address of the mx server then you can make a direct connection as a normal SMTP Client and send your mail. Below there is a small code piece that shows how you can do this process in C# .Net. This little piece of code takes advantage of using “nslookup” tool. It opens the “nslookup” in the background make a query for a given host then returns the output to a string, then the first mx record is parsed and returned from the function.

public string getmxserver(string hostname)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.UseShellExecute = false;
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = “nslookup”;
proc.StartInfo.Arguments = “-type=mx ” + hostname;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
StreamReader pout = proc.StandardOutput;
string s = pout.ReadToEnd();
string[] snew = s.Split(’,');
string[] snew2 = snew[1].Split(’=');
string[] snew3 = snew2[1].Split(’ ‘);
string[] snew4 = snew[1].Split();
string mxserver = snew4[4];
return mxserver;
}

As you can see it is the simplest way of Getting MX record. I hope this would be helpful to those who are intended in coding SMTP client.

C# Mail Client & Sending Mail With C# .NET

Programming No Comments »

As I mentioned below, the first small tutorial will be about writing a small mail client (smtp client). Before starting to tutorial, you have to design a simple GUI (graphical user interface) like below screen shot, a simple interface is enough. In this mail client we will use System.Net.Mail library to create mail and smtp client objects. Our code will be similar to below code piece,

MailMessage mymes = new MailMessage();
mymes.To.Add(new MailAddress(toTextBox.text);
mymes.From=new MailAddress(fromTextBox.text);
mymes.Subject=subjectTextBox.text;
mymes.Body=bodyTextBox.text;
SmtpClient myclient = new SmtpClient(”smtp server host address as string”);
myclient.Send(mymes);

It is easy as it is. It is extremely short and easy to understand (8 lines as you see), the complexity may emerge, if the smtp server needs authorization and maybe it need a ssl connection to authorize username & password (credentials). I leave those to ones who are interested in more than a simple stmp client, if you have any question or help please leave a comment.

An example GUI:
gui.png

New Category

Programming No Comments »

I have decided to open a new category in my blog to improve the content and try to share something useful, the new category will be about programming, I will giving small tutorials about various kinds of programming languages, but mainly based on c# and c++. The first small tutorial will be about writing a small smtp mail client with c# using .NET.

Copyright Protected ©2007 http://www.buraak.com
Contact info : <burak [at] buraak [dot] com>
Entries RSS Comments RSS Login
eXTReMe Tracker

Privacy Policy