Click to See Complete Forum and Search --> : Converting to Single-Instance App


bryker
November 13th, 2002, 05:21 PM
[Windows 2000, VB.NET]

I have the following situation:

ProjectA [A.exe], a Windows app, contains a Main() procedure which displays a form in ProjectB [B.dll], a Class Library. A.exe's sole purpose in life is to display this window in B.dll. A.exe is often (but not always) executed by a user double-clicking a file whose file extension is registered to A.exe. This A.exe, having received that file as a command-line parameter, then calls the form in B.dll.

In order to keep A.exe's Main() from immediately executing after displaying B.dll's form, A.exe calls that form modally. So, A.exe sits there until the window is closed. Then A.exe finally exits.

Here's what I need:

First and foremost, I need A.exe to be a single-instance application (like, say, MS Word). That is, if a user double-clicks a file, A.exe should display B.dll's form, and the file (which A.exe accepts as a command-line parameter) should display (this is how the app currently works). But if a user double-clicks *another* such file, then the *same* A.exe instance needs to be summoned, and it should add this file to the already-displayed form from the same B.dll. Right now, as I have coded it, a new instance of A.exe would instantiate a new B.dll and a new window would display.

So that's the main thing--A.exe should be a single-instance app. Secondly, as you can see, I have a problem in the way A.exe calls B.dll's form. At the moment, having it call that form modally is fine--that's the only (clean) way I can keep A.exe from exiting execution almost immediately. But this modal stuff obviously has to go--if I suddenly made A.exe a single-instance app *now*, it wouldn't be ready to accept that second file as a command-line parameter--it would still be sitting there from the previous file's call, waiting for the (modal) form of B.dll to be closed. So this is a related issue--I've got to pull off the single-instancing, but I also have to quit calling the form in B.dll the way I am: modally.

Thanks a lot for your help!

Athley
November 14th, 2002, 03:14 AM
A thought is to create a child form to a MDI form each time a new one is double-clicked (Like the Office programs work).

Dim frm As New Form1
Application.Run(frm)

This runs the form in a non modal state, but I am not sure it helps you in your situation.

/Leyan

DSJ
November 14th, 2002, 09:08 AM
I think your best bet might be too look into multi-threading... You can run frmB on a thread created in A.exe, and have A.exe wait for the thread to finish before ending while at the same time watching for new files to process. The only part I'm not sure about is how you'd pass a new filename to the A.exe if it's already running. I think you'll have to use a messageQ or something similar for this.

Also as Athley said you could make it work like the newest Office Apps. work. I saw an example (either on MSDN or the VB Homepage) about how to do this, although it looked somewhat complicated.