I have a WPF application, and i need to launch some actions (and receive return values) starting from another console application.
I encountered many problems so i don t know exactly how to proceed:
1- I tried to use command lines with arguments(it worked to launch the wpf application), but i couldn't receive return Values, because they are only returned on application shutdown. Also it doesn't fit my need since some actions must be called while the wpf app is still running.
2- I thought abt developping a small dll to communicate between Console and WPF application, but i don't know what technology would be light, efficient and fit my needs.
Has any one achieved similar task in the past? If so, how did you go about it?
Thanks,
What you are looking for is usually termed 'Inter-process communication'. Named pipes are a type of IPC and can be used in .net. This guide should get you started.
You could use WCF technology to achieve this. Define contract in you wpf application, where you should set your action logic - methods, by which you want to manage your application. If it is single sided, use netTcpBinding and create endpoint in your WPF application - it is a listener from the commands from outeside. Then in your your console application add proxy class of your wpf application, using svcutil. This will allow you to send your commands. If you do everything right - this should work. I suppose you have 1 wpf application and 1 console application.
You may launch your wpf app and then use:
Named Pipes - the simplest way but you must create a protocol of interaction between apps. You may transfer serialized objects for example. (example);
Shared files;
WCF - most powerfull and complex technology.
Related
I'm new in programming with .Net and C# and, as said in the title, I have a WPF app which is accessible in a system tray icon and I want to run it a windows service.
Typically, I want an output like it was described in an answer provided in a discussion here.
If you want it in the system tray I think what you'll have to do is make it a Windows service. I've only written 1 Windows Service and that was years ago, but I believe that's what you'll have to do. If I'm correct about writing a Windows service, then what I would suggest you do is create a new Visual Studio solution and add two projects to it. One would be a DLL which would run as a Windows service. The second project would be a WPF project that will be your UI the user interacts with. Then you'll have to use some messaging system to communicate between the two. For the action messages that would mimic what Outlook does, I've used some WPF toast messages to accomplish that. If you Bing/Google "WPF toast popup" you'll get lots of results.
I have many searched in Internet and find some helpful answers like:
URL1
You can't, not directly, because the windows service will necessarily start when the machine does, not when a user logs in. The service will also be running in a different context, likely as a different user. What you can do is to write a separate system tray based "controller" that interacts with the service.
URL2
It needs some effort to achieve. Well, just two hints: 1) use static property System.Environment.UserInteractive to detect in which mode your application is running, see http://msdn.microsoft.com/en-us/library/system.environment.userinteractive.aspx; 2) get rid of app.xaml, because it will force starting WPF Application in all cases; instead, create and run and instance of System.Windows.Application (or better, a specially derived class) explicitly and only for interactive mode, see http://msdn.microsoft.com/en-us/library/system.windows.application.aspx.
And, I could not apply their instructions.
Thanks advance!
If this is possible, I would like to make in C# a wpf application that has a console window in it. Also, I would like to have the console interact with the WPF form so that if you are complete with a console activity it can tell the wpf application and close. I guess this would be a basic inheritance program, but with a console/simulated console included.
You want something called interprocess communication.
There is a lot of ways of do that, but the easiest is the Named Pipes IMHO.
I have a quite small (so far) project which consists of
Core
GUI
TUI
The TUI (console application) is communicating with another console application in order to send and retrieve certain information.
The purpose of the GUI is to make it easier.
However, I am stuck on how would I make my TUI communicate with my GUI. So far, I know I can start my TUI from the GUI like this:
System.Diagnostics.Process.Start(#"cmd.exe", #"/k C:\project\TUI\bin\Debug\TUI.exe");
But now, I do not have any reference to the console application and I do not know to send information forth and back. I tried to search for the answer, but my search would only lead to how to start them in a normal way (not both, together, communicating).
So all in all, my question is: How to start a console application from windows forms project so those two to be able to communicate?
You can use the Input and Outut Streams of the Process.
Read this :
https://msdn.microsoft.com/de-de/library/system.diagnostics.process.standardinput(v=vs.110).aspx
I'm trying to make an app in Visual Studio with C# that will stay in the background and then other programs could start the app with just one argument and it will popup a notification (another form) with that argument.
I need the app to be consisted of just one process, i don't need multiple processes/instances of my app. I just want it to stay in the background and when someone starts the app with an argument, for example MyApp.exe "this is a notification", it will just pop up another form that will have that cmd argument in it, and it won't start the app as another process.
I hope you get my point, if you don't I can explain it more.
What you are wanting is more of a job for a Windows Service. A Windows Service is a program, with no GUI interface, that sits in the background and does something. The service is usually started when the computer boots up and stays running till the computer shuts down.
Communication between other programs and you service can be done using Windows Communication Foundation (or WCF). WCF allows communication between two applications using one or more transports.
Without using WCF, another way (although arguably less efficient) to "communicate" is through a database. Your service can monitor a table for rows added and your main program can add these rows whenever something needs to happen. The choice comes down to really what your end goal is.
Good luck!
Consider using named pipes (System.IO.Pipes)
or WCF's System.ServiceModel.NetNamedPipeBinding.
Other options are: old .NET Remoting or TCP/IP Sockets.
The 'background app' can be an Windows Service, but it won't allow showing any GUI form to the user, as background services don't have any forms.
For an easy way to create Windows "Background" Services see OpenSource TopShelf project.
I have a systemtray application (C#, Windows Forms). Next to this executable I will have another x amount of executables (written in C#) that must somehow send a message (preferably in string format) to the system tray application.
I do NOT want to install an entire Windows service for this.
It is NOT client-server. It all happens on the same PC. Using a listener combined with sockets would be to troublesome and it might even be blocked by it's own firewall I think.
I'm looking for something similar to a console application that can handle parameters on it's main function. Only this time for an already running Windows Form application.
Is it possible to somehow make a global function/procedure in the system tray application that can be called by other executables? Like "global void PerformAction(params here){..}"? This would seem to be the best solution but I'm not sure if .NET 4 supports this.
Example: executable X1.exe sends message "perform action [A] param [B]" to the system tray application and then terminates itself. The system tray application will then read that string and then knows that it needs to call function A with parameter "B".
But how do I send/receive the message?
Thank you.
Without resorting to WCF - you can use a simple wrapper over Named Pipes - like this one I posted as an answer to another question.
Hope this helps!
For local communication you could try anonymous pipes.
Link: http://msdn.microsoft.com/en-us/library/bb546102.aspx
You can also check out the remote method invocation.
Here is an example:
http://www.codeproject.com/Articles/14791/NET-Remoting-with-an-easy-example
There is a very handy method available to native application, i.e. window messages. With some hack, you can also use it with your .net application.
I would suggest you to refer to
SendMessage and SendMessageA api functions. You might have to write some unsafe code though.
If you used self hosted Asp.Net Web API, then you could use simple http calls to that application to execute methods. This is nice because you can test it using fiddler or anything that can send an http request.
Here is a link for an example.
http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api
The easiest solution to me seemed to use a WCF service (inside the tray application) as Steve B mentioned.
I used this tutorial: http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication.
I haven't heard of piping before but I think that the above link uses a WCF Service with piping. It did solve my problem.
Thanks for the many solutions provided (and so fast).