Search code examples
c#cwindowsmultithreadingsystem-tray

How do I run a C program as a thread from a C# System Tray Application?


I have a C console application which used to be run in the foreground in a CMD terminal in Windows and take user keystroke inputs. However it now needs to be moved to the background and requires no user input.

I have created a system tray, which is implemented correctly with a right click exit and right click about. And have a terminal program which does the functionality.

Rather than rewritting the program again I would like to be able to create a thread which calls the functions from my existing program which are do not require the terminal.

Just to stress the point the console interactive aspects have been removed from the code as have the applications response to keystrokes etc. Hopefully this clarifies things slightly.

Question: Is this possible? And how would I be able to implement this?

(I am generally not a PC programmer using more embedded C so .NET is quite foreign to me. Any help is greatly appreciated)


Solution

  • As already posted, you could use the Process.Start from your C# application tray.

        // Uses the ProcessStartInfo class to start new processes, 
        // both in a minimized mode. 
        void OpenWithStartInfo()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
            startInfo.WindowStyle = ProcessWindowStyle.Minimized;
    
            Process.Start(startInfo);
    
            startInfo.Arguments = "www.northwindtraders.com";
    
            Process.Start(startInfo);
        }
    

    http://msdn.microsoft.com/en-us/library/53ezey2s(v=vs.110).aspx

    You could try launching it on a hidden state: http://msdn.microsoft.com/en-us/library/system.diagnostics.processwindowstyle(v=vs.110).aspx

    Some programmers are telling to be careful. If any input is expected, then probably your process might halt. make sure nothing really stops your program.

    You could try to start a cmd command with Process.Start also, but I would say it is just too much. This would start another process without your C# tray application control:

     Process.Start("start /min cmd /c mycommand");