views:

436

answers:

5

Hi, Im developing a mobile application in dot net Compact framework. I managed to edit the registry HKEY_CLASSES_ROOT so that a click on file with .xyz extension will open my application. basically, i need to do some operation on this file when it's clicked.

however, i realise that if i do that the first time, it reaches program.cs at static void Main. but when the program is running and i click on the file with .xyz extension again, it doesnt loads the program static void Main. i tried setting breakpoints at the form that is currently running but still nothing.

so where does it go to? how can i detect file .xyz is clicked and do something??

A: 

Do you have string args in Main?

static void Main(string[] args)
SwDevMan81
Hi,Tks for the quick reply.I have allow command line argument by converting:`static void Main() { Application.Run(new Main()); }`to this`static void Main(String[] args) { if (args.Length != 0) MessageBox.Show("Array len != 0"); else MessageBox.Show("Array len = 0"); Application.Run(new Main()); }`but still when the program is running and i open a .xyz file extension, it would not reach main at all.
Hmm, yeah this could be tough one. Can you drag and drop the files into the program if its running (http://stackoverflow.com/questions/68598/how-do-i-drag-and-drop-files-into-c). I know thats probably not preferred.
SwDevMan81
It only reaches Main(String[] args) when the program is not running and first started. when my program is running with its form and all, and when i click the file, it just open my current form that is running (for e.g: setting form).im not sure if this is the correct approach. to use my main program to handle this operation. basically my main program has all the GUI and other features.
erm how do i drag and drop files into the program for mobile application?
yeah, that wont work either
SwDevMan81
hey SwDevMan81, i was thinking of creating an additional .exe file that just handle this operation. basically, my current solution got a few proj.1. the main proj which has the form and basically everything.2. some other projects which i will convert to dll and input as reference into my main projso i was thinking of adding an additional proj that will just handle this operation. but how do i gerenerate two separate .exe files during installation? or how do i pass information passed from one.exe to two.exeokay im probably confused now.
I probably would not go the multiple process route unless completely necessary. I just found this (http://www.megasolutions.net/cSharp/Send-a-message-to-a-Single-Instance-Application-18112.aspx) page. Not sure right now how much is CF compatible, but it might be a start. OpenNETCF might be able to support some of the process stuff if its not in the framework.
SwDevMan81
Tks. am still trying to see how can i convert this to CF. tough!!
A: 

Your problem is that you're looking for static void Main to be called again, which is a Static Constructor. Static constructors are only called once. They are called before the class (in this case your program) is initialized, then never again. They're not called for each new instance of your program..

smoore
This is not going to get called, as the app is already running.
ctacke
Good point. I assumed it would be treated the same as a class.
smoore
+1  A: 

The big problem you're having is that once the application is running Main will never get called again, and in fact it shouldn't.

Under Windows Mobile, unfortunately, the CF itself attempts to keep applications singletons, so when you try to runt he app a second time, the CLR itself intercepts that and instead brings the existing instance to the fore. One of the unfortunate side effects of this is that you get no opportunity to handle command-line parameters.

To get this to work, you have to do a few things:

  1. Subvert the CF's attempt to make your app a sinlgeton so you can run multiple instances.
  2. Hold a mutex in the app to know if you are already running.
  3. Spawn a thread early on to listen for incoming parameters from subsequent instances. When parameters come in, bring yourself to the fore and handle the parameters appropriately.
  4. On startup, if the mutex already is taken (i.e. the app is already running), use IPC (a point-to-point queue is a good way) to send the existing app's listener thread (see #3) your parameters and then just quit.
ctacke
#1 is a great trick and also a great explanation of what's going on under the hood. I'm very glad my co-workers never read that article, given their fondness for anything that alters the expected behavior of an application. :)
MusiGenesis
A: 

hi everybody

I have a similiar problem, but the tutorial written by ctacke doesn´t work. If I understand that tutorial correct it works for CF1.0 and CF2.0 because of the undocumented feature #NETCF_AGL_PARK_.
I use CF3.5 / Windows Mobile 6.0 and there it doesn´t work.

I need to do the following: my application has their own file format (.xyz) If I open a file with double click and my application wasn´t started yet, everything is fine, because I visit the static void constructor, means I can read out the arguments. But if my application is just in the background, I only got the focus, but no information about the clicked file...


Can anybody help me???

tueber
The exact same thing that I described is what has to be done. Now you might have to break out an actual debugging tool like Remote Spy++ to get the class or name of that Form (formerly #NETCF_AGL_PARK_), but the methodology *must* be the same.
ctacke
maybe I don´t understand the conzept correctlymy FormMain_Activated_Event contains that:string path = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase; IntPtr hWnd = FindWindow("#NETCF_AGL_PARK_" + path, string.Empty); SetWindowText(hWnd, "??"); //"#NETCF_AGL_PARK_" + path);First Question here: In which name I have to rename my window...the thing with the mutex i unterstand, but I NEVER came back into the static void Main(string args[])sorry but i didn´t understand. Can you provide a sample??Thanks for help
tueber
+1  A: 

You say that you need to perform some operation on a file every time it's clicked. I'm assuming this is something GUI-related, like you want to show properties of the file when it's double-clicked.

Assuming your program has one main form, you could set its MinimizeBox property to false and in its Deactivate event put this.Close();. This way, when you click a file of the right type, your application will start and read the command line args and display the file details. If the user then closes your app with the OK button in the upper right, it will close out for real so that the next time it opens it will open a new instance and read the command line args properly. Or, if the user just navigates to some other program in WinMo, your application's Deactivate event will fire, closing the app. Either way, the app is always either open and on top, or completely closed, so clicking a file in file explorer will always open a new instance.

MusiGenesis
Thinking outside the box. well done (though I'd prefer to not close it due to startup times in the CF).
ctacke
@ctacke: thanks, and thanks also for the opportunity to use my latest favorite joke: "I don't think outside the box, I just have a bigger box." :)
MusiGenesis