.Net Code Monkey RSS 2.0
 Thursday, September 02, 2010

Mixed Scope Properties in .Net

There may be a time when you want to have mixed scope for your "Getters" and "Setters" in a property. For those of you who are not familiar with the syntax here it is

VB:

Public Property MyProperty as String
    Get
        Return _myField
    End Get
    Private Set
        _myField = Value
    End Set
End Property

This example allows a public "Getter" and a private "Setter".

The same can be done in C#:

public string MyProperty
{
    get { return _myField; }
    private set { _myField = value; }
}

Hope you find that useful.

Regards,

Dib.

Thursday, September 02, 2010 6:06:44 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.Net | Asp.Net | C# | Scope | VB Script
 Friday, July 30, 2010

VB.Net Single Instance application on Terminal Server

Yesterday at work I spent nearly the whole day bashing my head against a wall trying to replicate functionality that our original VB6 application had into the new .Net conversion. The orginal VB6 application used "App.PrevInstance" to determine if another instance was running or not. This appeared to work for multiple users on Terminal Servers, so each user could run an instance of the app, but it would warn if they tried to open another.

VB.Net does not appear to have such direct functionality. Looking on the web provided various 'solutions' to the problem. However each one seemed to have an 'issue' with our scenario. By the afternoon I was looking at the Mutex objects which a lot of people seemed to find worked for them. However for me I was getting an "Object synchronization method was called from an unsynchronized block of code" error when closing the second instance onwards.

Looking into the Mutex object in a bit more depth it seemed that maybe it was not the right object for our purpose but maybe the Semaphore object would be better.

In the end the solution I used was to use a named Semaphore. When the application first instance runs it would attempt to open the named Semaphore and if it did not exist, assume it was the first instance and create one. The second instance when run would again look for and open the named Semaphore and if it exists and it manages to open it then it would assmue it is a second instance of the application.

By prefixing the Semaphorename with "Local\" it will allow each Terminal Server session to have it's own Semaphore so each remote user would not affected by another.

The elements of the code I used are below.

Imports:

Imports System.Threading

Declarations:

Private SingleInstanceSemaphore As Semaphore = Nothing ' This needs to live for the life of the application
Private Const MAX_SESSION_INSTANCES As Integer = 1
Private Const SEMAPHORE_NAME As String = "{9A49BB2C-45D2-4bb9-B38E-74F9DB612B96}" ' Unique for each application

Main Method:

''' <summary>
''' This is the main application entrance point.
''' </summary>
Public Sub Main()
' We need a name for the semaphore that is specific to to the Application / User combination
' We prefix the semaphoreName with "Local\" to allow this to run under terminal services. 
' The "Local\" prefix forces this into local user space. 
' If we want to forbid this in TS, use the "Global\" prefix.
Dim semaphorePrefix As String = "Local" ' Or "Global" if required
Dim semaphoreName As String = String.Format("{0}\{1}{2}", semaphorePrefix, SEMAPHORE_NAME, Environment.UserName)
Dim anotherInstanceRunningForUser As Boolean = True
Dim runAnotherDialogResult As DialogResult = DialogResult.No
Dim mainForm As Form1 = Nothing

Try
    Try
        SingleInstanceSemaphore = Semaphore.OpenExisting(semaphoreName)
    Catch ex As Exception
        anotherInstanceRunningForUser = False
    End Try

    If (anotherInstanceRunningForUser = False) Then
        SingleInstanceSemaphore = New Semaphore(0, MAX_SESSION_INSTANCES, semaphoreName)
    End If

    If (anotherInstanceRunningForUser) Then
        runAnotherDialogResult = MessageBox.Show("Another instance is already running. Do you want to run another?", _
"Already Running...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) End If ' If this is the first instance of the application, or a further instance but the user wants more than one... If (anotherInstanceRunningForUser = False) Or _ ((anotherInstanceRunningForUser = True) And (runAnotherDialogResult = DialogResult.Yes)) Then ' Run the application mainForm = New Form1(mutexName) Application.Run(mainForm) End If Catch ex As Exception MsgBox(ex.Message) End Try

I hope that mayhelp someone in the future and save them a day trawling through the web sticking examples of code together and getting failed results like I did.

Regards,
Dib.

 

Friday, July 30, 2010 7:21:34 AM (GMT Standard Time, UTC+00:00)  #    Comments [2] -
.Net | Mutex | Semaphore | Single Instance | Terminal Server
 Tuesday, June 08, 2010

Today I hit hit this error message while filling a typed DataSet Datatable. The odd thing was I could preview the data in the DataSet designer and the data looked fine.

I had to resort to Google and that returned this page, which if you hit this error, I would definately recommend. Look specifically for Sanjeys post about six posts down.

Roy Osherove's Blog - Dataset Hell

Tuesday, June 08, 2010 6:35:03 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.Net | Asp.Net | C# | Database | Errors
 Thursday, May 27, 2010

Glass Effect Text without images (HTML and CSS only)

As part of the development for a new web site I am working on I ideally need the site header text to be a glass effect laid over the glass effect headr image. I also want the header text to be able to be 'read' by search engines.

Although many search engines can read the 'alt' text of an image I belive I am right in saying the major search engines put more emphasis or weight on plain text. Especially text in header tags H!, H2 etc.

With this in mind I wanted to create a text only header that also has a glass effect.

One option was to create a text header and then over lay half of the text with a semi trans parent PNG. This will work visually but will not allow the text to be selected properly.

So to allow the text to be selected I decided I needed to place one layer of text on top of another and then cut off the bottom of the top layer of text.

Below is the code I used.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head >
</title>
<style type="text/css">
div.Glass
{
background-color:#FFF;
background-image:url(http://michaelsync.net/wp-content/uploads/2008/06/silverlight-glass-button.jpg);
background-position:-22px -10px;
background-repeat:no-repeat;
border:solid 1px #FFF;
height:80px;
position:relative;
}
div.Text
{
color:#66F;
height:80px;
}
div.Shine
{
color:#99F;
height:39px;
overflow:hidden;
margin-top:-97px;
}
h1
{
margin-top:17px;
}

/* IE7 Specific CSS hack */
* + html div.Shine
{
margin-top:-80px;
}
</style>
</head>
<body>
<form id="form1" >
<div class="Glass">
<div class="Text">
<h1>Welcome</h1></div>
<div class="Shine">
<h1>Welcome</h1></div> </div> </form> </body> </html>

Thursday, May 27, 2010 12:17:21 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Asp.Net | CSS | Html
Archive
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
Blogroll
 Clemens Vasters
 Harry Pierson
Passion * Technology * Ruthless Competence
 Joshua Flanagan
A .NET Software Developer
 Michael Schwarz's Blog
Developing applications on the Microsoft platform since Windows 3.1!
 Omar Shahine
Yet another Microsoft blogger
 Scot GU
Scott Guthrie lives in Seattle and builds a few products for Microsoft
 Scott Hanselman
Programming Life and the Zen of Computers
 Tom Mertens
Tom's corner
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Duane Wingett
Sign In
Statistics
Total Posts: 39
This Year: 4
This Month: 0
This Week: 0
Comments: 39
Themes
Pick a theme:
All Content © 2012, Duane Wingett
DasBlog theme 'Business' created by Christoph De Baene (delarou)