Monday, December 6, 2010

Throttle Socket In C#

public class ThrottleSocket
{
Socket _basesocket;
private long _maximumBytesPerSecond;
private long _byteCount;
private long _start;
public const long Infinite = 0;
public ThrottleSocket(Socket basesocket)
: this(basesocket, Infinite)
{
}
public ThrottleSocket(Socket basesocket, long maximumBytesPerSecond)
{
_basesocket = basesocket;
_start = CurrentMilliseconds;
_byteCount = 0;
_maximumBytesPerSecond = maximumBytesPerSecond;
}
protected long CurrentMilliseconds
{
get
{
return Environment.TickCount;
}
}
public long MaximumBytesPerSecond
{
get
{
return _maximumBytesPerSecond;
}
set
{
if (MaximumBytesPerSecond != value)
{
_maximumBytesPerSecond = value;
Reset();
}
}
}
protected void Throttle(int bufferSizeInBytes)
{
if (_maximumBytesPerSecond <= 0 || bufferSizeInBytes <= 0) { return; } _byteCount += bufferSizeInBytes; long elapsedMilliseconds = CurrentMilliseconds - _start; if (elapsedMilliseconds > 0)
{
long bps = _byteCount * 1000L / elapsedMilliseconds;
if (bps > _maximumBytesPerSecond)
{
long wakeElapsed = _byteCount * 1000L / _maximumBytesPerSecond;
int toSleep = (int)(wakeElapsed - elapsedMilliseconds);
if (toSleep > 1)
{
try
{
Thread.Sleep(toSleep);
}
catch (ThreadAbortException)
{
}
Reset();
}
}
}
}
public int Receive(byte[] buffer, int offset, int count,SocketFlags socketflag)
{
Throttle(count);
return _basesocket.Receive(buffer, offset, count,socketflag);
}
public int Receive(byte[] buffer)
{
Throttle(buffer.Length);
return _basesocket.Receive(buffer);
}
public void Send(byte[] buffer, int offset, int count,SocketFlags socketflag)
{
Throttle(count);
_basesocket.Send(buffer, offset, count,socketflag);
}
public void Send(byte[] buffer)
{
Throttle(buffer.Length);
_basesocket.Send(buffer);
}
protected void Reset()
{
long difference = CurrentMilliseconds - _start;
if (difference > 1000)
{
_byteCount = 0;
_start = CurrentMilliseconds;
}
}
}

Sunday, December 5, 2010

Capture Screen in C#


Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
bmpScreenshot.Save("filename.png", ImageFormat.Png);

Click Through Forms in C#

To make a Click Through Form
use the code below

{
......
this.TransparencyKey = this.BackColor;
....
}

To make a Panel transparent use the code below
{
......
this.TransparencyKey = this.panel1.BackColor;
......
}

Saturday, March 6, 2010

C Sharp Programming

C# (pronounced "See Sharp") is a multi-purpose computer programming language suitable for all development needs. This WikiBook introduces C# language fundamentals and covers a variety of the base class libraries (BCL) provided by the Microsoft .NET Framework.

Although C# is derived from the C programming language, it has features such as garbage collection that allow beginners to become proficient in C# more quickly than in C or C++. Similar to Java, it is object-oriented, comes with an extensive class library, and supports exception handling, multiple types of polymorphism, and separation of interfaces from implementations. Those features, combined with its powerful development tools, multi-platform support, and generics, make C# a good choice for many types of software development projects: rapid application development projects, projects implemented by individuals or large or small teams, Internet applications, and projects with strict reliability requirements. Testing frameworks such as NUnit make C# amenable to test-driven development and thus a good language for use with Extreme Programming (XP). Its strong typing helps to prevent many programming errors that are common in weakly typed languages.

The .NET Framework
.NET Framework Overview

An overview of the .NET class library used in C#.

Console Programming

Input and Output using the console.

Windows Forms

GUI Programming with Windows Forms.


Console Programming

The example program below shows a couple of ways to output text:

using System;
public class HelloWorld
{

public static void Main()
{

Console.WriteLine("Hello World!");// relies on "using System;"
Console.Write("This is");
Console.Write("... my first program!\n");
System.Console.WriteLine("Goodbye World!");// no "using" statement required

}

}


The above code displays the following text:

Hello World!
This is... my first program!
Goodbye World!


That text is output using the System.Console class. The using statement at the top allows the compiler to find the Console class without specifying the System namespace each time it is used.

The middle lines use the Write() method, which does not automatically create a new line.To specify a new line, we can use the sequence backslash-n ( \n ). If for whatever reason we wanted to really show the \n character instead, we add a second backslash ( \\n ). The backslash is known as the escape character in C# because it is not treated as a normal character, but allows us to encode certain special characters (like a new line character).



Input


Input can be gathered in a similar method to outputing data using the Read() and ReadLine methods of that same System.Console class:


using System;
public class ExampleClass
{

public static void Main()
{

Console.WriteLine("Greetings! What is your name?");
Console.Write("My name is: ");
string name = Console.ReadLine();
Console.WriteLine("Nice to meet you, " + name);
Console.Read();

}

}


The above program requests the user's name and displays it back. The final Console.Read()waits for the user to enter a key before exiting the program.

Error


The Error output is used to divert error specific messages to the console. To a novice user this may seem fairly pointless, as this achieves the same as Output (as above). If you decide to write an application that runs another application (for example a scheduler), you may wish to monitor the output of that program - more specifically, you may only wish to be notified only of the errors that occur. If you coded your program to write to the Console.Error stream whenever an error occurred, you can tell your scheduler program to monitor this stream, and feedback any information that is sent to it. Instead of the Console appearing with the Error messages, your program may wish to log these to a file.