❔ Passing data from a web page to a ""singleton"" C# desktop application
So, let's say I have a web page that has a button that opens my C# application (through a url protocol in the registry), through which it might receive some parameters.
However, I also want it so that if I have an instance of my C# application open, clicking the button will instead send those parameters to the already-open application, rather than starting a new instance of it.
How would I go about doing this?
------
Currently, I have it so that my program maintains a named mutex, and if that named mutex is owned by another process, then there is a previous instance of my program running, and the current instance aborts.
20 Replies
you could use a named pipe to pass whatever you want to the running instance
ok so this is the first time I've encountered pipes so let me know if I'm getting anything wrong
in my first instance, when I establish that my program is the first instance, I create a NamedPipeServerStream, with PipeDirection.In
in my second instance, I create a NamedPipeServerClient, with PipeDirection.Out and connect from the client to the server
how do I get the info out of the pipe and usable by the rest of the application?
and how would I do other stuff while waiting for info to come out of the pipe? Do I spin up a thread to handle the pipeServer?
yes
start a task, use a cancellation token to loop until cancelled and just
await pipeServer.WaitForConnectionAsync();
until a client connects. then read the message, disconnect, and continue the loopsorry, I'm.. not quite sure how this works.
So I have
and
and in the client I have
Where do I go from here?
why is it static
id make a service that just handles the pipe
well, currently it's just placed under
which contains
oh is the desktop app not a gui app?
anyway this is a simple example of a server:
I'm trying to use windows forms for UI, this is Program.cs which calls Application.Run(new MainForm())
ah winforms
so i assume youre not using mvvm
anyway thats not relevant
lol
so here you just run the server
and to send a message you just create a client stream, connect, write to it, then dispose it
simple as that
mmkay lemme give it a shot
OK so, I have no issues with the server, but when I connect I get a "access to the path is denied" error.
thats not how you should create the client stream
also, use the async versions
oh I see. what's the "just a string" constructor for?
and uh do I need the client to be async?
well it doesnt have to be
cuz for my purposes it seems like I can just leave it
mmkay
but its better to use async
actually youre probably only doing this on startup
then closing the process
in this case its fine to leave it sync
yeah
you use it if you want the default values for all other params
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.