How to have just ONE instance running of an exe with 3.5 .NET framework and CE windows 5.0 - c#-4.0

I've searched long and hard for an answer to my question and have found answers only to a class that does not exist in the 3.5 .NET frameworks version (and with CE Windows 5.0.) Also, it's Visual C# with Visual Studio 2008.
When I try the following (using System.Threading)
bool ok;
Mutex myMutex = new Mutex();
ok = Mutex(true,"scanner", out ok);
The compiler complains that the Mutex is a type, but used as a variable. If I use a [STAThread], it also complains that it can't find it and I"m forced to use [MTAThread].
Are the classes for the mobile device type of c# programs the same for console based ones? I'm somewhat new to this and the idea of how to declare, use, lock and everything you wanted to know about a mutex is making my head sore. O.o I suspect it's because the classes are different from the console verseions and the mobile versions.
It seems so simple, create a mutex, check for it's existance, if so then don't start a new exe instance. That is all I want...just run one instance of any exe. This is a mobile device thus it's a standalone and I never have to worry about anyone else using the .exe. Just need to stop the user from tapping the start button 15 times and having 15 instances of the application running (then complain about low memory usage!)
I've read that a good way is to create a thread, lock it then check if the lock was successful to test if an instance of an exe is already running, close it if so else run the exe. Is there an easier way to do this?

One way to approach this would be to open a file for writing. Only one instance of the application could obtain this lock at a time. On startup, try and obtain a file handle for writing, if it fails, there must be another instance already running: exit immediately.
I think you can forget mutexes since we are talking about separate proceses here.

Related

Unable to debug in Visual Studio because process can not access file

When I try to debug in Visual Studio I get the error message:
Unable to copy file "C:\Users\Name\Dropbox\Company Name\Development\Product Name 4 - Release Candidate\packages\MahApps.Metro.1.1.2.0\lib\net45\MahApps.Metro.dll" to "bin\Debug\MahApps.Metro.dll". The process cannot access the file 'bin\Debug\MahApps.Metro.dll' because it is being used by another process. Product Name 4 - Release Candidate
How can I fix this error?
This happens all the time in Dropbox. Dropbox does some occasional (very brief) locking of files as it is indexing them, and if you happen to attempt to open a file handle with the write attribute set at the same moment, the program will receive a file I/O exception (this can happen to your own code as well, so if you regularly work in Dropbox, be sure to handle that gracefully).
Try compiling/running it again and see if the problem goes away. If not, then you likely still have an instance of your application running in the background. This can occur if your program ever forks. VS will terminate the original process, but often not forked processes from it. Check task manager to be sure. It will be listed as a background process in Windows 8/8.1

How to find my JavaFX application in process manager?

I have a JavaFX application that should check if it has already been launched when you launch it (need only single instance running at the time). The problem is how to define it!
I have tried packing my JavaFX application into .exe file (or make an .exe launcher) that will be shown as the "MyProg.exe" process in the Windows task manager, not the "javaw.exe". But it is not the solution (I've tried netbeans' tools) because it adds jre to my application thus enlarging it from 1 mb. to 130+ mb. (can you help me with it?)
I've tried launch4j, but both launcher and .exe package starts javaw.exe to run my app. And when I check process manager and see 2 javaw.exe I don't know, is it 2 instances of my app or just another app running?
I hope I described it clearand will be very thankful if somebody can help me with it!
Judging by your approach it seems that you are already found a way to get a list of Windows' processes from Java. In this case you can use one of the next solutions:
Take other process info into account. E.g. command line or window title. Windows command line can be retrieved using wmic.exe PROCESS command and for Unix-bases systems by ps -Af.
When your program starts store it's process id in the registry or file in homedir. When another instance starts it should check that value and if process with that id alive then just exit.

Getting Peformace Counter related error on Window Azure

I am facing some critical issue which might be interesting for whom , those who are playing with window azure sdk. I have created on EXE which read performance counter data like CPU, memory, asp.net session from system like
queryCollection = ExecuteWMIQuery("SELECT * FROM win32_perfformatteddata_perfdisk_physicaldisk");
and I have aded this EXE in startup task of simple asp.net application which i have uploaded on window Azure. Now when i connecting to RDP of that I can see following errors in my event log as per below.
Disabled performance counter data collection from the
"ASP.NET_64_2.0.50727" service because the performance counter library
for that service has generated one or more errors. The errors that
forced this action have been written to the application event log.
Correct the errors before enabling the performance counters for this
service.
======================================================================
Windows cannot open the 64-bit extensible counter DLL
ASP.NET_64_2.0.50727 in a 32-bit environment. Contact the file vendor
to obtain a 32-bit version. Alternatively if you are running a 64-bit
native environment, you can open the 64-bit extensible counter DLL by
using the 64-bit version of Performance Monitor. To use this tool,
open the Windows folder, open the System32 folder, and then start
Perfmon.exe.
So i am thinking that my EXE trying to fetch performance counter for 32 bit (win32 indicate that) and that will log above error.
So anyone here came across this type of issue , also if my guess is correct then is there any way to implement my EXE logic such way that it can be run smoothly in any environment(32 or 64 bit)?
Hope that this would remain interesting question here!!!
Thanks In Advance
Arun.
That is correct. IIS running in Azure is running 64-bit unless you change it to run 32-bit in a startup task. You could try building it with the Any CPU setting. But most likely the best way is to do something like what the sysinternal tools does. They will spawn a new process that runs in 64-bit mode when needed. Then you can handle both.
I encountered this error while migrating to a Azure VM.
Solved it by using the InstallUtil which is located in the Framework64 folder instead of the one in the Framework folder

Are there recent Microsoft changes affecting the behavior of AppInitDLLs registry entry?

I support a product that detects unique key combinations when pressed to launch a notification alert.
This monitoring is done by a dll that is injected. Originally this was done specifically to winlogon.exe, but due to some changes in Vista we added the reference to our dll in AppInitDLLs to have it injected into every running process.
This is not working on my newest development machine, and some behavior on client machines mimicks the behavior. Another dll listed, C:\Windows\system32\nvinitx.dll, is still correctly being loaded, but mine is not.
Are there any known recent security patches that may affect this?
there are no new security changes as far as I know, you can inject any dll (but it must be compatible with the process you are injecting into) like if the process is 32bit your dll must be 32 and if the process is 64bit u need to inject 64bit or odd behavior will appear. another things that there is a new bool value must be set in windows 7 (not sure in vista) that is
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\LoadAppInit_DLLs" must be set to one

DLL self-registration: how to register just for the current user?

I'm using Visual C++. I'd like to really only have the DLL registered for the current user, even if the users is an administrator.
For that reason, I changed all of the project.rgs references to HKCR.
But, using Process Monitor while using regsvr32 to register the DLL, I notice that it still tries to create HKCR\TypeLib\{ ... }.
This happens on the call to CComModule::RegisterServer().
Is this inherent of CComModule? Is there anything I'm missing, in terms of configuration of Visual C++? I know that I can use RegOverridePredefKey, but I don't want to load more DLLs, and it's not a very elegant solution.
CComModule::RegisterServer() has a BOOL first parameter that indicates whether it should register the type library. Set it to FALSE and it will not try to.
HKCR is not mapped to the current user registry hive, if you really want to register the server for the local user use HKEY_CURRENT_USER\Software\Classes instead of HKCR.

Resources