adBrite

Your Ad Here

music

BidVertiser

Monday, April 25, 2011

Pass parameters to the timer in C#


These days I am using C# to develop the timer component running as a windows service, which runs automatically in a specific interval.

In C#, there are 3 kinds of timer type definitions:

1. System.Windows.Forms.Timer
2. System.Threading.Timer
3. System.Timers.Timer

In my project, I choose to use the third one: System.Timers.Timer
Here is the sample usage of this kind of timer:

System.Timers.Timer timer = new System.Timers.Timer();
timer. Interval = 10000; // set the interval as 10000 ms
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed); // set the event to execute
timer.AutoReset = true; // false: only execute once
timer.Enabled = true; // to decide if execute the event of timer specified


public void timer_Elapsed(object sender, EventArgs e)
{

}

From the above sample, we know it is quite easy to use it.

Next, I have a problem if I need pass some parameters to be used in the timer event defined above: timer_Elapsed(…). The two input parameters of this event interface are fixed, you can’t add more in.

There is a tricky to achieve it. Since we can’t add more input parameters in this event, we can set the parameters into the unused property of that sender object. This way is simple, but not good.

After researching, i find a better way to deal with it: creating a new timer to extend the system timer , and then defining the additional parameters inside this new timer.

Here are the sample codes:

// define a class to extend the timer,
// then define those additional parameters to be used in the event.
// for example: add one more parameter: param1.
namespace XXX
{
public class TaskTimer : Timer{
private string param1;

public string Param1
{
get
{
return param1;
}
set
{
param1= value;
}
}

public TaskTimer() : base()
{
}
}
}

// usage of this timer
using XXX;

TaskTimer timer = new TaskTimer();
timer.Interval = 10000;



// the event definition
public void timer_Elapsed(object sender, EventArgs e)
{
String param1 = (TaskTimer) sender).Param1;
}

Saturday, April 23, 2011

What is the difference between TCP and UDP

What is the difference between TCP and UDP

Difference between TCP and UDPThere are two types of internet protocol (IP) traffic, and both have very different uses.
  1. TCP(Transmission Control Protocol). TCP is a connection-oriented protocol, a connection can be made from client to server, and from then on any data can be sent along that connection.
    • Reliable - when you send a message along a TCP socket, you know it will get there unless the connection fails completely. If it gets lost along the way, the server will re-request the lost part. This means complete integrity, things don't get corrupted.
    • Ordered - if you send two messages along a connection, one after the other, you know the first message will get there first. You don't have to worry about data arriving in the wrong order.
    • Heavyweight - when the low level parts of the TCP "stream" arrive in the wrong order, resend requests have to be sent, and all the out of sequence parts have to be put back together, so requires a bit of work to piece together.
  2. UDP(User Datagram Protocol). A simpler message-based connectionless protocol. With UDP you send messages(packets) across the network in chunks.
    • Unreliable - When you send a message, you don't know if it'll get there, it could get lost on the way.
    • Not ordered - If you send two messages out, you don't know what order they'll arrive in.
    • Lightweight - No ordering of messages, no tracking connections, etc. It's just fire and forget! This means it's a lot quicker, and the network card / OS have to do very little work to translate the data back from the packets.

Thursday, April 21, 2011

linked list

Jump to: navigation, search
In computer science, a linked list (or more clearly, "singly-linked list") is a data structure that consists of a sequence of nodes each of which contains a reference (i.e., a link) to the next node in the sequence.
Singly-linked-list.svg
A linked list whose nodes contain two fields: an integer value and a link to the next node
Linked lists are among the simplest and most common data structures. They can be used to implement several other common abstract data structures, including stacks, queues, associative arrays, and symbolic expressions, though it is not uncommon to implement the other data structures directly without using a list as the basis of implementation.
The principal benefit of a linked list over a conventional array is that the list elements can easily be added or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory or on disk. Linked lists allow insertion and removal of nodes at any point in the list, and can do so with a constant number of operations if the link previous to the link being added or removed is maintained during list traversal.
On the other hand, simple linked lists by themselves do not allow random access to the data other than the first node's data, or any form of efficient indexing. Thus, many basic operations — such as obtaining the last node of the list (assuming that the last node is not maintained as separate node reference in the list structure), or finding a node that contains a given datum, or locating the place where a new node should be inserted — may require scanning most or all of the list elements.

Thursday, February 3, 2011

To enable ctrl+c ctrl+v ctrl+a ctrl+x on in textbox

        private void DescriptionTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            // To enable ctrl+c ctrl+v ctrl+a ctrl+x on in textbox
            if (e.Control && e.KeyCode == Keys.C)
            {
                DescriptionTextBox.Copy();
            }
            if (e.Control && e.KeyCode == Keys.V)
            {
                DescriptionTextBox.Paste();
            }
            if (e.Control && e.KeyCode == Keys.A)
            {
                DescriptionTextBox.SelectAll();
            }
            if (e.Control && e.KeyCode == Keys.X)
            {
                DescriptionTextBox.Cut();
            }
        }

Wednesday, February 2, 2011

Project 'PROJECTNAME' could not be opened because the Microsoft Visual C# 2008 compiler could not be created. Please re-install Visual Studio.

could not be opened because the Microsoft Visual C# 2008 compiler could not be created
 
 
Posted by Microsoft on 2/23/2009 at 8:55 PM
Thanks for reporting this issue. In your message, you've wrote by "running "devenv /ResetSkipPages"

Please run the following command "devenv /resetskippkgs" in Start->Run(note: not ResetSkipPages)

If this issue persists, It may help if you provide us with:
a setup log file

You can get the log files with the following steps:
1) Download collect.exe from the link below. http://blogs.msdn.com/heaths/attachment/8483493.ashx
2) You may choose to save the tool for later use, or to run directly.
3) The utility creates a compressed cabinet of all the VS and .NET logs to %TEMP%\vslogs.cab.

You can get more details about how to get the log files here:
http://blogs.msdn.com/heaths/archive/2008/05/09/visual-studio-and-net-log-collection-utility.aspx

Thanks again for your efforts and we look forward to hearing from you.
Visual Studio Product Team

Friday, December 17, 2010

C# Split String

=== Example program for splitting on spaces (C#) ===

using System;

class Program
{
    static void Main()
    {
        string s = "there is a cat";
        //
        // Split string on spaces.
        // ... This will separate all the words.
        //
        string[] words = s.Split(' ');
        foreach (string word in words)
        {
            Console.WriteLine(word);
        }
    }
}

=== Output of the program ===

there
is
a
cat

Wednesday, December 15, 2010

The Click Event for Buttons

The Click Event for Buttons

The click event gets activated when a button is clicked on. Examine the default code for a button:
private void button1_Click(object sender, EventArgs e)
{
}
In between the round brackets, we have this:
object sender, EventArgs e
The object keyword refers to the object which activated the event, a button in this case. This is being placed in a variable called sender. You can test this for yourself.
Start a new project. Add a button to your new form and double click it. Place the following code between the curly brackets:
MessageBox.Show( sender.ToString() );
We're just using the ToString method on the sender variable. Run your programme and click the button. You should see this:

C# Message Box for the Sender Object
The Message is displaying which object was the sender of the event, as well as displaying the Text property of the sender: the button with the Text "button1".
The other argument in between the round brackets was this:
EventArgs e
EventArgs is a class. It's short for event arguments, and tells you which events was raised. The letter "e" sets up a variable to use this class. If you change your line of code to this:
MessageBox.Show( e.ToString() );
the message box will then display the following:

clicksor

Go Daddy Girl Ella Koon- $7.49 .COM Domains
CompUSA