Sponsored Ad

Saturday, May 29, 2010

Thread Execution with Sleep() in C#

Thread Execution with Sleep() in C#

Thread execution with Thread.Sleep()works differently compare to normal execution. Lets take a example.

Suppose we have two thread thread1 and thread 2. in main program start thread1 first and then immidiatly start thread2 and inside both threads use sleep() to stop thread for few milliseconds. Sleep method use to block the current thread for a given time.

Lets run the same program without using sleep method.

Thread Execution with Sleep() in C#

Compare the output without sleep the execution of first thread complete first and then second thread starts and executes.

 

Thread Execution with Sleep() in C# Example:

using System;
using System.Text;
using System.Threading;

namespace Console_App
{

    public class Class1
    {
        public void fun1()
        {
            for (int i = 0; i < 7; i++)
            {
                Console.Write(" #" + i );
                Thread.Sleep(2);
            }
        }
        public void fun2()
        {
            for (int i = 0; i < 7; i++)
            {
                Console.Write( " @" + i );
                Thread.Sleep(2);
            }
        }
    }
    public class Class2
    {
        public static void Main()
        {
            Class1 a = new Class1();
            Thread thread1 = new Thread(new ThreadStart(a.fun1));
            Thread thread2 = new Thread(new ThreadStart(a.fun2));
            thread1.Start();
            thread2.Start();
            Console.ReadLine();
        }
    }
}

Friday, May 28, 2010

How to Get Form Values using ASP-C# | Post Mehtod Example

How to Get Form Values using ASP-C# | Post Mehtod Example

 

This simple asp-C# code will help you to get the form values with post mehtod. Assign the current page name into action="Default3.aspx" property of form and fill the text box with some values and click the submit button.

You can recieve the textbox values using

strValue=Request.Form["txtValue"];

where txtValue is the name of TextBox.

 

How to Get Form Values using ASP-C# | Post Mehtod Example:

 

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >

<html>

<body>

<form action="Default3.aspx" method="post">

Your name: <input type="text" name="txtValue" size="20">

<input type="submit" value="Submit">

</form>

<%

string strValue;

strValue=Request.Form["txtValue"];

if( strValue != "")

{

Response.Write("Value of TextBox is: <b> " + strValue + " </b><br />");

Response.Write(" Do you Like it ? ");

}

%>

</body>

</html>

Thursday, May 27, 2010

Example to Create and Start the Thread in C#

Example to Create and Start the Thread in C#

 

This is simple example to demonstrate the making a thread and starting it in C#. We have took a example to declare a start thread and pass this startthread to a thread constructor while declaring the thread.

Once you get the thread object, directly call

t1.Start();

to start the thread.

Now look at the thread execution.

  1. Before Thread Execution Starts
  2. After Thread Execution Starts
  3. Inside Thread Method

The “Inside Thread Method” is get called in last, the start() method just start the thread and execute next statement of the block.

 

Example to Create and Start the Thread in C# Example:

using System;
using System.Text;
using System.Threading;

namespace Console_App
{
    public class Class1
    {
        public static void function()
        {
            Console.WriteLine("Inside Thread Method");
        }
    }
    public class Class2
    {
        public static void Main()
        {
            ThreadStart thread1 = new ThreadStart(Class1.function);
            Thread t1 = new Thread(thread1);
            Console.WriteLine("Before Thread Execution Starts");
            t1.Start();
            Console.WriteLine("After Thread Execution Starts");
            Console.ReadLine();
        }
    }
}

Tuesday, May 25, 2010

HTML Formatting Through ASP-C#

HTML Formatting Through ASP-C#

 

While designing and coding ASP.NET Page, Sometimes there is a requirement of C# code written in ASPX page and contains HTML formatting.

This code helps you to achive the same. The First statement for H2-Heading 2 implementation.

Second statement format the paragraph HTML tag and assign a color to it.

 

HTML Formatting Through ASP-C# Example :

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >

<html>
<body>
<%
Response.Write("<h2>H2 - Heading 2 formatting with ASP-C# code </h2>");
%>

<%
Response.Write("<p style='color:#0000ff'> Paragraph formatting with ASP-C# code</p>");
%>
</body>
</html>

Thursday, May 20, 2010

How to Stop Scrolling Background Image with Text

How to Stop Scrolling Background Image with Text

 

Generally image scroll along with the page if you want that image should be fix while scrolling the page, The given code will help you to do the same.

other code is also there to scroll image along with page. So as per your requirement select the code and enjoy the coding.

Happy Cpoding :)

 

Source code of How to Stop Scrolling Background Image with Text

 

<html>

<head>

<style type="text/css">

body {background-image: url('Sunset.jpg'); background-repeat: no-repeat; background-attachment: fixed }

/* if you want that text should scroll with image use below code.

body {background-image: url('Sunset.jpg'); background-repeat: no-repeat; background-attachment: scroll }

*/

</style>

</head>

<body>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

<p>Stop Scrolling BackGround Image with page</p>

</body>

</html>

Wednesday, May 19, 2010

Set Image Properties in a webpage

Set Image Properties in a webpage

 

Sometimes while designing webpage, placing a image and setting their properties are touph task. This example helps you to arrange a image on webpage using background-image property. with some other image arrangement properties. Let me know if you face any problem while arranging a image.

Source code of Set Image Properties in a webpage

<html>

<head>

<style type="text/css">

body{ background-image: url('Sunset.jpg'); background-repeat: no-repeat; background-position: center; }

</style>

</head>

<body>

</body>

</html>

Tuesday, May 18, 2010

What is use of Volatile Variable in C#

Volatile variable is used to declare a variable whose value can be change any time by any program.

 

Volatile variable is basically use with thread where number of thread access the same variable and the current thread can access the latest value written by another thread without use of lock.

Here is code to show how to declare volatile variable.

 

Source Code of Volatile variable declaration:

 

using System;
using System.Text;

namespace Console_App
{
    public class Class1
    {
        public volatile int i;

        public void fun_volatile(int j)
        {
            i = j;  
        }    
    }
    public class Class2
    {
        public static void Main()
        {
            Class1 obj = new Class1();
            obj.fun_volatile(1);
            Console.ReadLine();
        }
    }

}

How to Check if Given Object is Compatible with given Type in C# | IS Operator

This sample program will help you to check given object compatibility with given type using is operator given in C#.

Like in this example we are comparing the int and string type to passing object type.

 

 

 

Program Output:

How to Check if Given Object is Compatible with given Type

 

 

C# Source Code:

 

using System;
using System.Text;

namespace Console_App
{
    public class Class1
    {
        public static void fun1(object obj)
        {
            if (obj is int)
            {
                Console.WriteLine("int");              
            }
            else if (obj is string)
            {
                Console.WriteLine("string");
            }

        }    
    }
    public class Class2
    {
        public static void Main()
        {
            Class1.fun1(1);
            Class1.fun1("1");
            Console.ReadLine();
        }
    }

}

Thursday, May 13, 2010

How to do Boxing and Unboxing in C#

Boxing is a process to convert value type to reference type and Unboxing is a process to convert Reference type to value type.

 

Output:

How to do Boxing and Unboxing in C#

 

Source code of Boxing and Unboxing:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
 

        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.Boxing_UnBoxing_Example();
            Console.ReadLine();
        }

        private void Boxing_UnBoxing_Example()
        {
            int i = 123;
            //Boxing Example
            object b = i;
            Console.WriteLine("Boxing Value: " + b);

            //Unboxing Example
            int j = (int)b;
            Console.WriteLine("UnBoxing Value: "  + j);
        }
    }
}

Tuesday, May 11, 2010

How Break Statement works in loop – C# | Break Statement Example

Break statement breaks the loop and sends the control pointer to out of the loop. Neither it executes the after break code nor it complete the rest of the loop.

As you can see in example after break code never executes and before break only executes once.

 

Output:

Break Example Output C#

 

Source code:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.BreakExample();
            Console.ReadLine();
        }

        private void BreakExample()
        {
            for (int i = 1; i <= 5; i++)
            {
                WriteLine("Before Break : " + i);
                break;
                WriteLine("After Break : " + i);
            }
        }
    }
}

How to Use Arrays in ASP.NET using C#

How to Use Arrays in ASP.NET using C#

 

You can declare C# arrays in ASPX file using Server tag <% %>.

The string[] strArray = new string[5]; line declares a string array of 5 capacity. next five statements assign the values to this array.

Last statement is trying to print the array values using for loop. Please note that you can not use writeln here for new line. use <br> to get new line after current line.

 

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<body>

<%
string[] strArray = new string[5];
    strArray[0] = "CsharpTalk.com";
    strArray[1] = "Indiahub.com";
    strArray[2] = "SoftwareTestingNet.com";
    strArray[3] = "CsharpHub.com";
    strArray[4] = "SharePointBank.com";

for (int i = 0; i <= 4; i++)
{
    Response.Write(strArray[i] + "<br>" );
}

%>

</body>
</html>

Monday, May 10, 2010

How to concatenate two string using C# | Concatenation example

This sample program will help you to concatenate two given string. We have demonstrated two methods to concatenate.

string.Concat(strInput1, "-", strInput2);

is a efficient method to concatenate two string , you can use any one of these methods as per your convenience.

 

Concatenation example - C#

 

Source Code:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyFirstConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string strResults = "";
            Console.WriteLine("Please enter first string:");
            string strInput1 = Console.ReadLine();
            Console.WriteLine("Please enter second string:");
            string strInput2 = Console.ReadLine();
            Console.WriteLine("you have entered string 1: " + strInput1 + " and string 2: " + strInput2);
            Console.WriteLine();

            //method 1
            strResults = strInput1 + "-"+strInput2;
            Console.WriteLine("Output of concatination: " + strResults);

            //method 1
            strResults = string.Concat(strInput1, "-", strInput2);
            Console.WriteLine("Output of concatination: " + strResults);
            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }

    }
}

Tuesday, May 4, 2010

How to Find Position of a Element in a Array

This post will help you to find the exact position of a element in given array. This program usage IndexOf method to find the element. Its returns –1 in case of a given element is not present in Array otherwise can return a index from 0 to array length-1.

 

How to Find Position of a Element in a Array

 

Source Code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("If IndexOf methosd return -1, it means the content is not present in array <BR/><BR/>");
        string[] Sites = { "IndiHub.com", "TestingWiz.com", "SharePointBank.com" };
        int intLocation = Array.IndexOf(Sites, "IndiHub.com");
        Response.Write("Indihub.com Location is at: " + intLocation + "<BR/><BR/>");
         intLocation = Array.IndexOf(Sites, "BharatClick.com");
        Response.Write("BharatClick.com Location is at: " + intLocation + "<BR/><BR/>");
    }

}

Monday, May 3, 2010

How to work With ListBox in C# | Add Items | Get Selected Items

This sample program will help you to work with listbox using C#. This small demonstration will help you add items in listbox and get selected values from listbox.

There is two separate button for different functionalities.

 

OutPut:

 

How to work With ListBox in C# | Add Items | Get Selected Items

 

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void btnClick_Click(object sender, EventArgs e)
    {
        lblOutput.Text = "Your selection is: <br>";
        foreach (ListItem lst in lstSites.Items)
        {
            if (lst.Selected == true)
            {
                lblOutput.Text += lst.Text + "<br>";
            }
       }
    }

    protected void btnAddItem_Click(object sender, EventArgs e)
    {
        lstSites.Items.Add(txtInput.Text.ToString());
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ListBox ID="lstSites" runat="server" SelectionMode="multiple">
                <asp:ListItem>IndiHub.com</asp:ListItem>
                <asp:ListItem>BharatClick.com</asp:ListItem>
                <asp:ListItem>SharepointBank.com</asp:ListItem>
                <asp:ListItem>SoftwareTestingNet.com</asp:ListItem>
            </asp:ListBox>
            <br />
            <br />
            <asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:Label ID="lblOutput" runat="server" Text="Label"></asp:Label>
            <br />
            <br />
            <asp:Button ID="btnClick" runat="server" Text="Get Selected Items" OnClick="btnClick_Click" />
            <asp:Button ID="btnAddItem" runat="server" Text="Add Items" OnClick="btnAddItem_Click" />
        </div>
    </form>
</body>
</html>

Sponsored Ad

More Related Articles

Website Update

Followers