Archive for the ‘vb’ Category

Mutex – single instance of program – VB – Winforms

February 19, 2015

Three ways to use mutexes to guarantee that your VB.Net winforms application only runs one instance at a time on a given host.
I don’t know why I redid this three times yesterday – I must have made some mistake that led me to believe that the first two tries weren’t working!


Public Class globals

Public Shared unique_mutex As Mutex

'ref: http://odetocode.com/blogs/scott/archive/2004/08/20/the-misunderstood-mutex.aspx
' http://renren.io/questions/35665/how-to-force-c-sharp-net-app-to-run-only-one-instance-in-windows
' flavor: TryOpenExisting
Public Shared Function guarantee_unique_instance_of_program() As String
Dim sMutexName As String = "Global\VcProcessPaymentsMutex"
Dim bAlreadyExisting As Boolean = False
Dim bFirstTimeCreated As Boolean = False

' http://renren.io/questions/5447537/c-sharp-mutex-issue-to-prevent-second-instance

bAlreadyExisting = Mutex.TryOpenExisting(sMutexName, globals.unique_mutex)

Select Case bAlreadyExisting
Case True
Return "FAILURE Instance already running"
Case False
globals.unique_mutex = New Mutex(False, sMutexName, bFirstTimeCreated)
Return "SUCCESS"
End Select

End Function

'ref: http://odetocode.com/blogs/scott/archive/2004/08/20/the-misunderstood-mutex.aspx
' http://renren.io/questions/35665/how-to-force-c-sharp-net-app-to-run-only-one-instance-in-windows
' I think this is the ScottGu method (more or less)
Public Shared Function guarantee_unique_instance_of_program_GOOD3() As String
Dim bFirstTimeCreated As Boolean = False
globals.unique_mutex = New Mutex(False, "Global\VcProcessPaymentsMutex")
If (Not unique_mutex.WaitOne(0, False)) Then
Return "FAILURE Instance already running"
Else
Return "SUCCESS"
End If
GC.Collect()
End Function

Public Shared Sub release_mutex()
unique_mutex.ReleaseMutex()
End Sub

' this is the "FirstTimeCreated" flavor
' ALSO GOOD (why did I redo this yesterday?)
' flavor: TryOpenExisting
_
Public Shared Function guarantee_unique_instance_of_program_GOOD2() As String

'1) try to open one that is existing
Dim bTryToOpenExisting As Boolean = False
bTryToOpenExisting = Mutex.TryOpenExisting("Global\VcProcessPaymentsMutex", unique_mutex)
If (bTryToOpenExisting) Then
Return "FAILURE Instance already running"
End If

' 2) if it didn't exist yet, then create it
Dim bFirstTimeCreated As Boolean = False
Using unique_mutex As New Mutex(False, "Global\VcProcessPaymentsMutex", bFirstTimeCreated)

If (Not bFirstTimeCreated) Then ' this should never be hit
Return "FAILURE Instance already running"
End If

GC.Collect()
End Using

Return "SUCCESS"

End Function


(more…)

dynamically select StateServer based on machinename (for PROD,QA,DEV selection)

December 4, 2014

First go have a look at Maartens post.

My purpose – to use one line in web config, but use the correct stateserver ip address when deployed.
And I tested this to make sure it worked.

web config entry

VB version (I’m running)

Imports System

' purpose - dynamically return a state server ip address based on DEV/PROD/QA deployment
' ref http://blog.maartenballiauw.be/post/2008/01/23/ASPNET-Session-State-Partitioning.aspx
Public Class WebConfigSessionStateServer
Implements System.Web.IPartitionResolver

Private Shared m_DynamicStateServerAddress As String = ""
Private ReadOnly Property DynamicStateServerAddress As String
Get
If (String.IsNullOrEmpty(m_DynamicStateServerAddress)) Then Initialize()

Return m_DynamicStateServerAddress
End Get
End Property

Public Function ResolvePartition(key As Object) As String Implements IPartitionResolver.ResolvePartition

Return DynamicStateServerAddress

End Function

Private Sub Initialize() Implements IPartitionResolver.Initialize
Dim sDEV As String = "tcpip=666.666.238.252:42424"
Dim sPROD As String = "tcpip=666.666.79.176:42424"
Dim sQA As String = "tcpip=666.666.238.249:42424"

Select Case DeploymentType
Case "QA"
m_DynamicStateServerAddress = sQA
Case "DEV"
m_DynamicStateServerAddress = sDEV
Case "PROD"
m_DynamicStateServerAddress = sPROD
Case Else
m_DynamicStateServerAddress = sPROD
End Select

End Sub

Private Shared m_DeploymentType As String = ""
Public Shared ReadOnly Property DeploymentType As String
Get
If (m_DeploymentType = "") Then
Dim sname As String = Environment.MachineName 'utils_SysNetInfo.machinename
m_DeploymentType = "PROD"
If (sname.ToUpper().IndexOf("DEV") > -1) Then m_DeploymentType = "DEV"
If (sname.ToUpper().IndexOf("QA") > -1) Then m_DeploymentType = "QA"
End If

Return m_DeploymentType

End Get
End Property

End Class

C# version (translated by Telerik. Bless them. Buy things from them What a great service.)

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

namespace utils_lib
{

// purpose - dynamically return a state server ip address based on DEV/PROD/QA deployment
// ref http://blog.maartenballiauw.be/post/2008/01/23/ASPNET-Session-State-Partitioning.aspx
public class utils_SessionStateServer_Dynamic : System.Web.IPartitionResolver
{

private static string m_DynamicStateServerAddress = "";
private string DynamicStateServerAddress
{
get
{
if ((string.IsNullOrEmpty(m_DynamicStateServerAddress)))
Initialize();

return m_DynamicStateServerAddress;
}
}

// ************************************************************
public string ResolvePartition(object key)
{

return DynamicStateServerAddress;

}

// ************************************************************
public void Initialize()
{
string sDEV = "tcpip=666.666.238.252:42424";
string sPROD = "tcpip=666.666.79.176:42424";
string sQA = "tcpip=666.666.238.249:42424";

switch (DeploymentType)
{
case "QA":
m_DynamicStateServerAddress = sQA;
break;
case "DEV":
m_DynamicStateServerAddress = sDEV;
break;
case "PROD":
m_DynamicStateServerAddress = sPROD;
break;
default:
m_DynamicStateServerAddress = sPROD;
break;
}

}

// ************************************************************
// use machine name to decide PROD DEV QA
private static string m_DeploymentType = "";
public static string DeploymentType
{
get
{
if ((string.IsNullOrEmpty(m_DeploymentType)))
{
string sname = Environment.MachineName;
//utils_SysNetInfo.machinename
m_DeploymentType = "PROD";
if ((sname.ToUpper().IndexOf("DEV") > -1))
m_DeploymentType = "DEV";
if ((sname.ToUpper().IndexOf("QA") > -1))
m_DeploymentType = "QA";
}

return m_DeploymentType;

}
}

} // class

}

//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Twitter: @telerik
//Facebook: facebook.com/telerik
//=======================================================

asp.net mvc – mixing razor code with css

June 26, 2014

1) vb syntax
2) purpose: hide/show a div from razor (I realize it’s possible to do this easily in jquery)

the page/view:
Html.BeginForm(“results”, “Results” )

@

Html.EndForm()

the css:
.dvPaging {
@Model.NotPaged()
vertical-align:text-top;
width:50%;
height: 20px;
}

the razor code:
I think it’s best to do what we need in a function.
I put presentation layer code in my view model

‘ purpose – called by the css to show or hide the paging section based on whether there are more than
‘ one page or not
Public Function NotPaged() As String
Dim sRet As String = “”
If (Not isPagination) Then sRet = “display:none; height:0px;”
Return sRet
End Function

generic list in vb

December 18, 2013

here’s a good link for reference:
http://dillieodigital.wordpress.com/2009/10/13/how-to-sortfind-custom-objects-in-a-list-with-vb-net/
and I also used:
http://stackoverflow.com/questions/200151/search-for-object-in-generic-list

1) we have a generic list: Private Shared m_ListLDAP As List(Of PersonPermission)

2) the class
Public Class PersonPermission
Public netid As String
Public campus As String
Public usertype As String

Public Sub New(snetid As String, slocation As String, stype As String)
netid = snetid
campus = slocation
usertype = stype
End Sub

Public Sub New()
End Sub

End Class

3) we load the list from a datatable
4) then later in the code, we find an element in the list based on the user netid, and return some values. Frankly, I don’t understand the syntax. To me, it looks like LINQ, but I guess the Find function of the generic list is calling my little inline delegate function … or something like that.

' ******************************************************************************************************************
' purpose - search the list on netid, return campus and usertype
Public Shared Sub get_ldap_override_values(ByVal netid As String, ByRef campus As String, ByRef usertype As String)

If (IsNothing(m_ListLDAP)) Then load_user_list()
campus = ""
usertype = ""
Dim myLocatedObject As PersonPermission = m_ListLDAP.Find(Function(p) p.netid = netid)
If (Not IsNothing(myLocatedObject)) Then
campus = myLocatedObject.campus
usertype = myLocatedObject.usertype
End If
End Sub