ASP Synchronously write and read in a single file - file-io

Is my first experience with ASP(vbscript) so I need your advice.
Ok there is a requirement to create a counter for keeping track of the number of registrant of a registration form.
What I thought was to create a function in vbscript (in the ASP file) that reads the counter from a file (count.txt), which resides on the server, and increments it by one.
This works when I test it. BUT will it work (and most important will it work properly) when multiple simultaneous registrations are made? It seems to me like a race condition (if it doesn't completely break it :))
Does anybody have any idea how to work around this problem in these specific technologies? any ideas and thoughts are welcomed.
Note:There is no database..:(
Thanks much in advance!

I'm not so familiar with the classic ASP (and the way the corresponding ISAPI extension processes requests) but the following solution might work.
You may want to synchronize access to the file from multiple threads by using SyncLock statement (since all the clients will be served by the same application instance but by different threads within the running process).
First, declare the global variable:
Application.Lock()
Application("SyncFileLock") = New Object()
Application.Unlock()
Next, use the variable declared above in your SyncLock statement:
SyncLock Application("SyncFileLock")
' Open the file, increment the counter, close the file
End SyncLock
This will ensure that multiple threads won't be writing to same file at the same time.

Related

vb.net - How do I selectively communicate with two instances of a program?

I'm new to VB, and so forgive me if this is a simple question.
I will be running multiple time consuming (single thread) processes in a program (that allows automation thru COM). And so to save some time, I want to open two or more instances of this program and run them simultaneously. But anything that I try to do on the program, it happens on the first opened program. This is what I have which my intentions are to open two instances of the program (which does correctly), and open a new document in each of the instances (which what it does is open two new documents in myProcess0 and none in myProcess1. Note: I have System.Diagnostics namespace activated.
Using myProcess0 As Process = Process.Start(programPath)
myProcess0.WaitForInputIdle()
pws0 = New COMprogram.Document
End Using
Using myProcess1 As Process = Process.Start(programPath)
myProcess1.WaitForInputIdle()
pws1 = New COMprogram.Document
End Using
Note: The COM program does not allow to create an handle for the program (like Matlab allows with MLApp.MLApp)
Any help will be appreciated it! Thanks in advance!
This is not exactly a solution, but my current "brute" workaround. This works if your iterations are independent of each other and wish to use multiple instances of a program in the same computer to perform these iterations (but for some reason that is unknown to me, any "Application" objects created point back only to the first instance of the Application).
What I'm doing, is tricking the code by using "desktops": http://technet.microsoft.com/en-us/sysinternals/cc817881.aspx
I simply open the VB code and a Application instance in each desktop, and for some reason the VB code only interacts with the Application opened in the current desktop and not on the others. This happens as well with Matlab somehow. For some reason, all Matlab Application objects reference the first instance.
This will be up to the COMprogram API. With Word or Excel, for example, you can't specify which instance you're accessing without manipulating an Application object.

Visual Basic / ASMX - how to use application cache variable?

I'm trying to amend our content management system so it'll handle SQL database failures more gracefully. It's a bunch of ASMX pages, and a Helpers.vb file in which I've written a SQL connection tester function.
Each of the ASMX pages call the same function.
I need to create a variable I can check that's persistent and performant, otherwise I'm going to have fall back on something disasterously slow like reading a text file every time I set up a sql connection string.
I've tried using application caching, but either it doesn't work in the context of my helpers.vb file, or I've made a mess of the syntax. One problem that's already stymied some of the approaches I've found via google - I can't use 'Import System.Web.Caching' - IntelliSense doesn't show the 'Caching' part.
Has anyone got any example code that might get me up and running? Or an alternative approach?
#Mike,
Many thanks, now I'm using HttpRuntime.Cache correctly... it works!
Thanks everyone for taking the time to post :)

How to enforce single instance of an application under mono?

So, I am able to enforce single instance of my application on Windows as follows.
[STAThread]
class method Program.Main(args: array of string);
begin
var mutex := new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");
if mutex.WaitOne(Timespan.Zero, true) then
begin
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += OnThreadException;
lMainForm := new MainForm;
lMainForm.ShowInTaskbar := true;
lMainForm.Visible := false;
Application.Run(lMainForm);
end
else
MessageBox.Show("Another copy running!!!");
end;
However, running the same application on Linux under mono this code does NOT work at all. I am able to run multiple copies. I don't know if it has to do with the fact that I am starting the application on the Terminal like mono MyPro.exe. If this is the problem, do you need to pass some values before you execute the command line.
Thanks in advance,
You need to enable shared memory in mono as Adrian Faciu has mentioned to make your approach work, however this is not the best approach (there's a reason it's disabled by default in the first place, even if I can't remember now exactly why).
I've used two solutions in the past:
A file-based lock. Create a known file, write the pid into that file. At startup in your app check if the file exists, and if it exists read the pid and check if there are any running processes with that pid (so that it can recover from crashes). And delete the file upon exit (in the instance that created it in the first place). The drawback is that there is a race condition at startup if several instances are launched pretty much at the same time. You can improve this with file locking, but you may have to use P/Invokes to do the proper file locking on Linux (I'm not entirely sure the managed API would do what you'd expect).
A socked-based lock. Open a known port. The advantage over the above is that you don't need to do any cleanup and there are no race conditions. The drawback is that you need a fixed/known port, and some other program might happen to use that exact port at the same time.
You probably need to enable shared handles using MONO_ENABLE_SHM environment variable:
MONO_ENABLE_SHM=1 mono MyPro.exe
Adrian's solution works for me(tm).
Wild guess: did you need MONO_ENABLE_SHM in both invocations?

How to implement a system wide text replacement in windows programmatically?

I have a small VB .Net application that, among other things, attempts to substitute system wide typed text by the user(hotstrings concept). To achieve that, I have deployed 'ahk2exe' and 'AutoHotkeySC.bin' with my application and did the following:
When a user assignes a new 'hotstring':
Kill 'hotstring' exe script file if running
Append new hotstring to the script file (if non exist then create a new one)
Convert edited/new script file to exe (using ahk2exe)
Run the newly converted script exe
(somewhere there I also check if the hotstring has been already assigned)
However, I am not totally satisfied with this method for the following two main reasons:
The extra resources deployed with the application.
Lag: The time it takes for the system to kill the process and then restart it takes a minimum of 5 seconds on my fast computer and more on other computers. That amount of time is much more than the time it takes the user to assign the hotstring, minimize/close the window and then test his/her new hotstring. When the user does so initially with no success they will think the process failed. So this method is not very good for user experience.
So, I am looking for a different method or implementation. May be using keyboard hooks? Or maybe adding a .dll library that achieves the same. Are there any resources you know about that might help (free or commercial)? What is the best way to achieve my desired goal?
Many thanks for your help.
Implementing what Autohotkey does would be a pretty non trivial task.
But I'm pretty sure that AHK supports an "autoreload" option for scripts
googling "autohotkey auto reload" turned up several pages discussing that very concept. IF that worked, all you'd have to do is update the script file and that's it, AHK should automatically reload the script.

Start external process several times simultaneously

I need to start an external process (which is around 300MB large on its own) several times using System.Diagnostics.Process.
The only problem is: once the first instance starts, it generates temporary data in its base folder (where the application is located), so I can't just start another instance - it would corrupt the data of the first one and mess up everything.
I thought about temporarily copying the whole application folder programmatically, so that each instance has its own, but that doesn't feel right.
Could anybody help me out? Thanks in advance!
Try starting each copy in a different directory.
If the third-party app ignores the current directory, you could make a symlink to it in a different folder. I'm not necessarily recommending that, though.
Pass an argument to your external process that specifies the temp folder to use.