This source code shows how to lock a certain routine so it cannot be accessed/used by another process. To demonstrate this, we will use Threads.
using System;
using System.Threading;class ThreadLock
{private int Tests = 0;
public void TestRun()
{
Thread t1 = new Thread(new ThreadStart(Exec));
t1.Name = “thread 1″;
Thread t2 = new Thread(new ThreadStart(Exec));
t2.Name = “thread 2″;
t1.Start();
t2.Start();
}public void Exec()
{
while (Tests < 5)
{
lock (this)
{
int count = Tests;
count++;
Console.WriteLine(count + “: ” + Thread.CurrentThread.Name);
Thread.Sleep(1000);
Tests = count;
}
}
}static void Main(string[] args)
{
ThreadLock test = new ThreadLock();
test.TestRun();
}
}
Result

Tags: c# sample code
This is a sample function on how to move a file from one folder/directory to another while considering some factors such as:
Move File Function
‘fromFile and toFile parameters consist of directory-path+filename+extension
Public Function MoveFile(ByVal fromFile As String, ByVal toFile As String) As Boolean‘Check if File do exists
Dim fileExists As Boolean = System.IO.File.Exists(fromFile)‘Check if File is being use by other process
Dim fileIsLock As Boolean = IsFileLocked(fromFile)‘If file do exists and is not lock, begin transfer
If fileExists AndAlso Not fileIsLock Then‘Check if file already exists in the destination folder
If System.IO.File.Exists(toFile) Then‘delete file from destination folder and move new file [overwrite]
System.IO.File.Delete(toFile)
System.IO.File.Move(fromFile, toFile)
Return True‘Of course you can always check the destination file information,
‘and maybe decide whether to delete it or not and just rename the new file
‘Example by getting file information
Dim destFile As New FileInfo(toFile)‘Get size [in bytes]
If destFile.Length > 0 Then
‘decide what to do here if the destination file has some contents
‘you might not want to overwrite it if it contains important data
End IfElse
‘If the file does not exists in the destination folder, just move
System.IO.File.Move(fromFile, toFile)
Return True
End If
Else
Return False
End IfEnd Function
Simple Function to check if file is locked
Public Function IsFileLocked(ByVal strFullFileName As String) As Boolean
Dim returnVal As Boolean = False
Dim fs As System.IO.FileStream = NothingTry
fs = System.IO.File.Open(strFullFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.None)
Catch ex As Exception
returnVal = True
Finally
If fs IsNot Nothing Then
fs.Close()
End If
End TryReturn returnVal
End Function
Tags: vb .net sample code
Posted by matt as Code, MS SQL, Transact-SQL
When working with MSSQL, there are cases where your simple stored procedure (SP) just won’t work when deployed to your live server even though it works perfectly on your local PC. I have encountered this problem a couple of days ago where my SP won’t work on Windows Azure SQL. I did some research and found out the as of now it does not support calling of OpenXML. And unfortunate, this function is needed in my SP. So instead of working on xml, I changed the parameter to string [delimited by a comma], split it and return as table. Here’s the code for the “split” function and how to use it in queries.
Run the code below to create the Split function:
Create FUNCTION [dbo].[Split]
(
@InputArray NVARCHAR(max),
@delimiter CHAR(1)
)
RETURNS @ReturnTable TABLE (ID INT,RValue NVARCHAR(150))
ASBEGIN
DECLARE @TempArrayStr NVARCHAR(max)
SET @TempArrayStr = @InputArrayDECLARE @i INT
DECLARE @RValue NVARCHAR(max)
DECLARE @counter INT = 1SET @TempArrayStr = REPLACE (@TempArrayStr, ‘ ‘, ”)
SET @i = CHARINDEX(@delimiter, @TempArrayStr)WHILE (LEN(@TempArrayStr) > 0)
BEGIN
IF @i = 0
SET @RValue = @TempArrayStr
ELSE
SET @RValue = LEFT(@TempArrayStr, @i - 1)
INSERT INTO @ReturnTable(ID,RValue) VALUES(@counter,@RValue)
IF @i = 0
SET @TempArrayStr = ”
ELSE
SET @TempArrayStr = RIGHT(@TempArrayStr, LEN(@TempArrayStr) - @i)
SET @i = CHARINDEX(@delimiter, @TempArrayStr)
SET @counter = @counter + 1
END
RETURN
END
Sample usage:
declare @StringOfValues varchar(max)
set @StringOfValues = ‘value1,value2,value3,value4′
SELECT * FROM split(@StringOfValues, ‘,’)
Returns:
ID RValue
1 value1
2 value2
3 value3
4 value4
SELECT * FROM split(@StringOfValues, ‘,’) where ID = 2
Returns:
ID RValue
2 value2
Tags: database guide • mssql sample script
I’m currently using Telerik Treeview and I’m fascinated on how great its features are. I have an example on NodeClick event.
Highlight parent/s when a node is clicked
The Treeview
<telerik:RadTreeView ID=”MyTreeview” MultipleSelect=”true” OnNodeClick=”MyTreeviewClick” />
Note: MultipleSelect must be set to True
The Click routine
Protected Sub MyTreeviewClick(sender As Object, e As RadTreeNodeEventArgs)
SelectParent(e.Node)
End Sub
Highlight parent function
Protected Sub SelectParent(node As RadTreeNode)
If node.ParentNode IsNot Nothing Then
node.ParentNode.Selected = True
End If
End Sub
If you want to highlight all parents of selected node, you can always call it recursively
Protected Sub SelectParent(node As RadTreeNode)
If node.ParentNode IsNot Nothing Then
node.ParentNode.Selected = True
SelectParents(node.ParentNode)
End If
End Sub
Calling a routine on selected node
You can use a variety of Nodes properties and functions. In this example I use node’s level property which returns the level [integer] of the the current node. This is helpful if you have static node levels. I’m calling my function ONLY on specific node level.
Protected Sub MyTreeviewClick(sender As Object, e As RadTreeNodeEventArgs)
If e.Node.Level = 2 Then
//Do something here when any of the 2nd level node is clicked
End If
End Sub
You can also use this to highlight parents only on selected node levels.
Protected Sub MyTreeviewClick(sender As Object, e As RadTreeNodeEventArgs)
If e.Node.Level = 2 Then
SelectParent(e.Node)
End If
End Sub
Hope this helps
Tags: asp .net tutorial • telerik sample code
When working on customized Gridview (especially when you’re planning to use it multiple times within your solution), you can always create your own class by extending the default Gridview. So instead of putting all properties inside <asp:GridView> tag like this:
<asp:GridView ID=”MyOwnGridViewID” runat=”server” CssClass=”MyOwnGridViewClass” AllowPaging=”true” PageSize=”25″>
<!– As well as other properties here –>
</asp:GridView>
You can simply call:
<asp:MyOwnGridView ID=”MyOwnGridViewID” runat=”server” />
Here’s the code for your class (in VB):
Public Class MyOwnGridView Inherits GridView
Public Sub New()
Me.AllowPaging = true
Me.CssClass = “MyOwnGridViewClass”
Me.PageSize = “25″
…etc
End SubEnd Class
The code is neat and easy to debug.
Tags: vb .net sample code
.Net asp .net tutorial asp .net tutorial c# sample code CSS sample script database guide java sample code javascript sample code mssql sample script string reverse in C# t-sql telerik sample code user-defined function vb .net sample code visual studio tutorial
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.