|
-
May 8th, 2010, 07:03 AM
#1
Thread Starter
New Member
Single instance of Pocket PC Console application!
Hello,
Would anyone happen to know how to force a single instance of a Console application on a Pocket PC (IPAQ) using vb.net (Visual Studio 2008)? I have been bouncing my head off the wall on this one. I just can't seem to grasp it! I have tried the mutex way but it does not seem to be working. Below is the code I have been trying to get to work.
Code:
'This is the Class
Imports System
Imports System.Runtime.InteropServices
Imports System.Reflection
Public Class OneInstance
Private Const ERROR_ALREADY_EXISTS As Integer = 183
<DllImport("CoreDll.Dll")> Private Shared Function GetLastError() As Integer
End Function
<DllImport("CoreDll.Dll", EntryPoint:="CreateMutexW")> Private Shared Function CreateMutex(ByVal lpMutexAttributes As IntPtr, ByVal InitialOwner As Boolean, ByVal MutexName As String) As Integer
End Function
Public Function IsInstanceRunning() As Boolean
Dim applicationname As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name()
If CreateMutex(IntPtr.Zero, True, applicationname) <> 0 Then
Return Marshal.GetLastWin32Error() = ERROR_ALREADY_EXISTS
Else
End If
Return False
End Function
End Class
Code:
'This is how I am calling it inside of the module
Imports System
Imports System.Xml
Imports System.Diagnostics
Module Module1
Sub Main()
On Error Resume Next
Dim a As New OneInstance
If a.IsInstanceRunning() Then
'End the second, third, etc process
Process.GetCurrentProcess().Dispose()
End If
' Continue on with the rest of the code
End Sub
End Module
Any comments and or help is appreciated!
Thanks,
Jlynn001
-
May 9th, 2010, 02:09 AM
#2
Frenzied Member
Re: Single instance of Pocket PC Console application!
If this is pocketpc then the operating system ensures on your behalf that there is only one instance of the application running.
There is a discussion on this in the last few weeks, if you search the forums
-
May 9th, 2010, 07:23 AM
#3
Thread Starter
New Member
Re: Single instance of Pocket PC Console application!
Well that is the confusing part as it is considered pocketpc but the OS is WinCE. So it in itself does not limit an application to only one instance of itself.
-
May 9th, 2010, 01:02 PM
#4
Frenzied Member
Re: Single instance of Pocket PC Console application!
Windows CE doesn't disallow multiple instances - Pocket PC DOES disallow multiple instances.
PocketPC runs on top of Windows CE
I don't know if console apps are treated differently though 
If so, the createmutex should sort it. I posted working code for this in an earlier thread.
-
May 10th, 2010, 09:29 AM
#5
Thread Starter
New Member
Re: Single instance of Pocket PC Console application!
Hi Pete,
Sorry I must be doing something wrong. I just can't get it to work. I copied your code and it still is not working can you possibly tell me what I am doing wrong? No matter what I try it just keeps allowing more then one instance. The application name is PDA411a.exe. The code is below that I have been trying to use.
Code:
Imports System
Imports System.Xml
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Reflection
Module Module1
Sub Main()
Dim p As New System.Diagnostics.Process()
On Error Resume Next
Call CreateMutex(CInt(0), 1, "<PDA411a")
If GetLastError() = ERROR_ALREADY_EXISTS Then
MsgBox("One is running already")
Else
End If
MsgBox("Waiting to run part B")
p.StartInfo.FileName = "PDA411b.exe"
p.Start()
End Sub
Public Declare Function CreateMutex Lib "Coredll.dll" (ByRef lpMutexAttributes As Integer, ByVal bInitiaOwner As Integer, ByVal lpName As String) As Integer
Public Declare Function GetLastError Lib "CoreDll.dll" () As Integer
Const ERROR_ALREADY_EXISTS = 183
End Module
-
May 10th, 2010, 11:47 AM
#6
Frenzied Member
Re: Single instance of Pocket PC Console application!
You tried stepping through the code?
-
May 10th, 2010, 11:54 AM
#7
Frenzied Member
Re: Single instance of Pocket PC Console application!
Hi,
if you are using Sub Main, then the program will drop through and end, so PDA411a won't exist, as it will have terminated.
You checked using TaskManager?
-
May 10th, 2010, 12:07 PM
#8
Thread Starter
New Member
Re: Single instance of Pocket PC Console application!
Hi,
I have not stepped through as I can't start the second instance in order to test it. I have checked with Task Manager and it is running. It shows as PDA411a.exe. I added the MsgBox ("Waiting to run part B") so it would wait during my testing.
-
May 10th, 2010, 12:18 PM
#9
Frenzied Member
Re: Single instance of Pocket PC Console application!
If I take out the 'On Error Resume Next' (Shudder) - the application doesn't run - it fails when it gets to the messagebox.
-
May 10th, 2010, 12:37 PM
#10
Thread Starter
New Member
Re: Single instance of Pocket PC Console application!
Interesting. I just deployed it to the emulator. Ran it which stops and waits at the part B message box. Then debugged it on the emulator and it worked fine. Very interesting! I am assuming you are saying it bombs at the already running message box? If I change it to your application.Exit(). I get the Name application is not declared error. How would I declare it? Sorry I am very new at this and I just can't grasp it yet!
-
May 10th, 2010, 12:42 PM
#11
Thread Starter
New Member
Re: Single instance of Pocket PC Console application!
Even more interesting is that the same exe which works on the emulator does not work on the PDA itself. By working I mean stop or detect the second instance of the application.
-
May 10th, 2010, 12:51 PM
#12
Frenzied Member
Re: Single instance of Pocket PC Console application!
If you take out the on resume next then it fails at the line...
MsgBox("Waiting to run part B")
Using SubMain you do not exit - when you application reaches the 'End Sub' of 'Sub Main' then it will automatically exit.
So as it stands, without the messageboxes, your application would start up, start up PDA411b and then end.
-
May 10th, 2010, 01:28 PM
#13
Thread Starter
New Member
Re: Single instance of Pocket PC Console application!
Here is all the code. Maybe it will help!
Code:
Imports System
Imports System.Xml
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Reflection
Module Module1
Sub Main()
On Error Resume Next
Call CreateMutex(CInt(0), 1, "PDA411a")
If GetLastError() = ERROR_ALREADY_EXISTS Then
'Application.Exit()
MsgBox("One is running already")
Else
End If
Dim stra As String
Dim s As String = ""
Dim userid As String = ""
Dim username As String = ""
Dim usercred As String = ""
Dim resid As String = ""
Dim resname As String = ""
Dim devtype As String = ""
Dim p As New System.Diagnostics.Process()
Dim MyValue As String = SearchValue("\?\?.ini", "DeviceName")
Dim datafile As String
If MyValue = "" Then
MyValue = System.Net.Dns.GetHostName()
Else
End If
Dim oWrite As System.IO.StreamWriter
datafile = ("My Documents\Personal\" & MyValue & ".txt")
oWrite = IO.File.AppendText(datafile)
' The xml node reader.
Dim reader As XmlNodeReader = Nothing
' The xml document to read from.
Dim doc As New XmlDocument()
' Load the xml document.
doc.Load("My Documents\?\?.xml")
' Set the reader to open the xml document.
reader = New XmlNodeReader(doc)
' Read all the data in the XML document and display it.
'oWrite.WriteLine("Launched: " & Now)
While reader.Read()
Select Case reader.NodeType
Case XmlNodeType.Element
' Keep track of the element that the user is on.
s = reader.Name
Case XmlNodeType.Text
If Not s.Equals("user_id") Then
Else
userid = reader.Value
End If
If Not s.Equals("user_name") Then
Else
username = reader.Value
End If
If Not s.Equals("user_credentials") Then
Else
usercred = reader.Value
End If
If Not s.Equals("resident_id") Then
Else
resid = reader.Value
End If
If Not s.Equals("resident_name") Then
Else
resname = reader.Value
End If
If Not s.Equals("device_type") Then
Else
devtype = reader.Value
End If
End Select
End While
If Not (reader Is Nothing) Then
reader.Close()
End If
stra = IO.File.GetLastWriteTime("\My Documents\?\?.xml")
oWrite.WriteLine("Launched: " & Now & "," & userid & "," & username & "," & usercred & "," & resid & "," & resname & "," & devtype & "," & MyValue & "," & stra)
oWrite.WriteLine()
oWrite.Close()
'Launch
p.StartInfo.FileName = "\Program Files\?\?.exe"
p.Start()
'Wait for exit then Launch PDA411b.exe
'MsgBox("Waiting to run part B")
p.WaitForExit()
p.StartInfo.FileName = "PDA411b.exe"
p.Start()
End Sub
Private Function SearchValue(ByVal File As String, ByVal Value As String) As String
Try
Dim SzR As New System.IO.StreamReader(File)
Dim MyString() As String
While SzR.Peek <> -1
MyString = Strings.Split(SzR.ReadLine, "=")
If MyString(0) = Value Then
SzR.Close()
Return MyString(1)
End If
End While
SzR.Close()
Return ""
Finally
End Try
End Function
Public Declare Function CreateMutex Lib "Coredll.dll" (ByRef lpMutexAttributes As Integer, ByVal bInitiaOwner As Integer, ByVal lpName As String) As Integer
Public Declare Function GetLastError Lib "CoreDll.dll" () As Integer
Const ERROR_ALREADY_EXISTS = 183
End Module
Ultimately I would like to replace the One is running already message box with the exit application command, but I can't get that to work either.
Just wanted to say thanks for the help also Pete. I do really appreciate it!
Last edited by jlynn001; May 11th, 2010 at 04:10 PM.
Reason: Some wrong code
-
May 10th, 2010, 04:31 PM
#14
Frenzied Member
Re: Single instance of Pocket PC Console application!
HI,
you really need to step through your code to see what is happening. Check the value of GetLastError - I think you will find it is non-zero.
Certainly remove the 'On Error Resume Next' which will hide a multitude of sins.
As I explained, using Sub Main will drop through and exit the application, so you need structure your code accordingly.
Last edited by petevick; May 10th, 2010 at 04:35 PM.
-
May 11th, 2010, 07:12 AM
#15
Thread Starter
New Member
Re: Single instance of Pocket PC Console application!
Hi Pete,
I did step through the code. It works on the emulator but not on the Ipaq itself. I don't understand why! When I run it. It goes right to the waiting for part B message box. Which is what it should do. If I run it again it goes to the One is already running message box. Which it is suppose to do. If I try the same thing on the device itself. It always goes to the waiting to run part B. No matter how many times I run it.
-
May 11th, 2010, 07:57 AM
#16
Frenzied Member
Re: Single instance of Pocket PC Console application!
Have you checked the task manager on the device?
Did you step through the code on the device, as well as the emulator?
-
May 11th, 2010, 09:57 AM
#17
Thread Starter
New Member
Re: Single instance of Pocket PC Console application!
Yes that is the strange part. Everything is the same on both the emulator and IPAQ. Except the results of the code.
-
May 11th, 2010, 01:19 PM
#18
Thread Starter
New Member
Re: Single instance of Pocket PC Console application!
Hi Pete,
Quick update. It seems the emulator I was using was a PocketPC 2003 SE Emulator. The device is a iPAQ, and or a WinCE device. Now that I am using the Windows Mobile 6 Classic Emulator. It does not work properly either. It goes right to the Waiting to Run part B message box. So it has to be something with WinCE and the code correct?
-
May 11th, 2010, 03:57 PM
#19
Frenzied Member
Re: Single instance of Pocket PC Console application!
If the device is an iPaq, it will be running Windows Mobile so shouldn't need a mutex - the operating system will take care of it.
Settings>system>About
Should show exactly what it is running.
-
May 11th, 2010, 04:06 PM
#20
Thread Starter
New Member
Re: Single instance of Pocket PC Console application!
That was what I thought as well but it does allow more than one instance to run. Maybe it is my coding not sure. I thought WM would block and force only one instance, but this does not seem to be the case. I see that GetLastError() always comes back with the error code of 6. Which is a invalid_handle_error I believe. So what would be causing that? Thanks for all your help Pete. I am really just learning this stuff and it is quite confusing!
-
May 13th, 2010, 01:00 PM
#21
Thread Starter
New Member
Re: Single instance of Pocket PC Console application!
I was able to get this working by adding a form to it. Once switched from console to a form application it is working as it should now! Thanks for all your help Pete!
-
May 14th, 2010, 09:12 AM
#22
Frenzied Member
Re: Single instance of Pocket PC Console application!
Glad you got it sorted - Console apps obviously work differently
-
Jun 7th, 2010, 05:32 AM
#23
New Member
Re: Single instance of Pocket PC Console application!
The device is a iPAQ, and or a WinCE device. Now that I am using the Windows Mobile 6 Classic Emulator.
Website Design Company India
Web Promotion Company India
Website Designing India
-
Jun 7th, 2010, 07:16 AM
#24
Frenzied Member
Re: Single instance of Pocket PC Console application!
the device is a ipaq, and or a wince device. Now that i am using the windows mobile 6 classic emulator.
?????
-
Mar 24th, 2011, 01:36 AM
#25
New Member
Re: Single instance of Pocket PC Console application!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|