Run a program as a process in the background - c#

I want to create a program that won't close when you click the close button but, only when a exit function is run from within the form.I was thinking i could have it run in the background without the window needing to be open (Like the steam client and dropbox, mediafire, google drive, google chrome extension notification), and was wondering how to do that.
I was thinking of some ways to do it but i wasn't sure how to do them:
A starter program that keeps it open in the background
Running a background process that keeps information stored
I am running c# 2010 so i will need answers relating to that. I would also prefer to not use 3rd party extensions/tools except a basic text editor(Notepad++).Thank you in advance!

There are a number of possible options:
Hide the main form
Create a Tray application
Write a Windows Service

Related

Program Won't Show in the Foreground When I Click the Task Manager

I have a C# Windows Forms Application. It basically reads files from one directory and writes them to another. A lot of files. In the application, I have a label that says "Processing (filename)" and it updates as expected. The problem is that when I put another program in the foreground - any other program such as Firefox, Windows Explorer, whatever - I see my application in the task manager however I get no response when I click on it. Further, the only way to see my program is to minimize every other window on the screen. How can I fix this? I want the program to pop to the top of all the windows when I click on it in the Task Manager. I am using Windows 10.
Have you tried using a Background Worker? This will allow the UI thread free to do UI work. See - https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx#Examples

get status info Windows Media Player (stand-alone application)

I want to write an application which gets the status information about a running WMP thread, like playing/paused, shuffle mode on/off, repeat on/off, song title or media file path.
I was using the P/Invoke SendMessage-function to "simulate" a click on the play/pause/stop etc. buttons, but I'm unable read the values of labels/text boxes inside the WMP application with WM_GETTEXT.
I used Spy++ to get the handles of certain text fields - but they (obviously) change their handle after restarting the WMP.
Does somebody know a answer to this Problem?
It could be anything - P/Invoke or SendKeys - but not this AxWindowsMediaPlayer, because my application is a console app and I want the status information of the external process - not of my own.
Thanks
I will mark this thread as 'answered'.
I have solved this problem a few months ago, but lost the source code.
The aim of this application is to control the WMP via sshd or telnet.
You can download the binaries here if you want to:
WMP.rar (27 KB)
https://mega.co.nz/#!ug5DySRR!rQlI9OZZ8wYq7yAqaH-KYsmCl9LSaBjNdvqX0g_WDtI
The application is executed via console. You can see the commands by typing >wmp help

Enabling a console application through a Windows Forms application

I'm not going to go into details why am trying to do this, instead of making the main application do the work. I think it's currently easier for me. But I'm not going to use this technique in the future.
In my case, the main form has a button that opens another form. In the second one for you can adjust the amount, pause, resume and stop the work of the console application (sound totally useless (and maybe stupid), but, like I said, I'm not going to go into details why). This means that the application must have access to all the variables and resources of the whole program and vise versa.
I know how to launch a new form trough a main form, but I don't know how to launch a console application.
EDIT:
I forgot to mention, that the console application is a part of the solution.
Your requirement is a bit vague; "the application must have access to all the variables and resources of the whole program and vise versa". 'Variables and resources' cannot be shared across processes, you will instead need interprocess communication of some form.
If your console app merely needs to communicate back to the calling forms app that a RPC has succeeded then use exit codes in the console app, see: How do I return a value from a console application to a service in .NET?
Otherwise this has been answered before: Getting the ouput from Console window into Winform application
You'll need to either create a console emulator (time consuming and difficult to get right), or fire up cmd.exe in another process and use remote procedure calls (or another inter process communication method) to communicate between the two processes
If you want to communicate between the two processes, take a look at this library here:
https://github.com/TheCodeKing/XDMessaging.Net
It allows you to send messages from one app to the other. For example, App1 sends a message "stop" on the channel "randomkey" to ConsoleApp1, ConsoleApp1 can listen on the channel "randomkey" and intercept the "stop" message and stop its current processing.
If you wanted to just open the console window, just use System.Diagnostics.Process.Start();
You can just call Main directly. Beware of doing this on the UI thread directly though!
SomeConsoleApp.Main(new string[]{"-O", "File 1.txt", "-some-parameter"});
Or if you only have an exe, you can do:
System.Diagnostics.Process.Start("someconsoleapp.exe");

Asynchronous Drag and Drop to Windows Explorer

Question:
I need a DragAndDrop solution to download a file on drop in a folder of Windows Explorer for C# & .NET 4.0. It should not be necessary to have the file on the computer. The file will be big enough that the drag-time won't be enough to get the download done. I have found various questions, even accepted answers, but nothing that works. The very closest thing to something working is this demo project:
http://blogs.msdn.com/b/delay/archive/2009/11/16/creating-something-from-nothing-and-knowing-it-developer-friendly-virtual-file-implementation-for-net-refined.aspx
How to implement this code to download a file as part of the action of putting it to the drop place in Windows Explorer?
Web browsers solve this problem every day. Simplifying their model a little, do this:
Make a little program that performs your download given appropriate command line parameters. This little program should pop up a window with a progress bar and a cancel button.
Spawn this second program whenever the user "drops" something. This program will create the target file immediately and start filling it with data. It will maintain appropriate locks on the file until it is done downloading, at which point the "downloader" will exit.
If you're going to keep the "downloader" threads in the originating program, you will need some kind of download manager so that the user can get appropriate feedback on their downloads.
Okay, as Yahia said in the comments it's not possible without a proper shell extension for the different versions of Windows and .NET. You might have luck with the link I posted, but for me it crashes the Explorer and the developer thinks it works fine.
My honest opinion is with only .NET you can only do it with a FileSystemWatcher via copying special .temp-files, watching where they land, doing your task and replacing the .temp files when your task is done. Sad Windows.

C# Process automation

I'm starting a external application with System.Diagnostics.Process, this external process at one moment opens up a dialog where user has type something and then click OK. What i need is to wait with my application(the one where i started the external process) until the user has inserted something and clicked OK. After that OK i have to do some more task on that external process and then close it.
Yes, it's possible. There are a number of ways to get window information starting with a process handle and/or ID. See this question and responses for getting started. You will most likely end up using P/Invoke to the Win32 API to get this accomplished but there are dozens of good examples for getting this done.
Once you have the window handle you can use a timer polling scheme to test for the presence, or in your case, presence and then the disappearance of a window.
This is possible but there are some work behind it. First you need to run your code as unmanaged code as you will need to hook on Windows OS events with the Win32 API.
So an option would be to have a loop looking for the dialog to open, pause what ever your code are doing and continue when the dialog are gone.
If the application you are starting exists after the user interacts with the dialog, then you can just call Process.WaitFroExit() and your code will not continue until the process you started has quit.
There are quite a few helpful functions for interacting with processes in the System.Diagnostics.Process class (that I assume you are using to start this external application)

Categories