Answered I need to write a program that automatically

sandralopez9297

New member
Joined
May 11, 2020
Messages
2
Programming Experience
Beginner
For example, I need to write a program that automatically turns on and runs the first process of an application within 30 seconds, then automatically disables it, and launches the third process of that application in 30 seconds.
Within 30 seconds of running the first process, the program automatically starts a second process and also waits 30 seconds to automatically turn it off and then turn on and run the fourth process for 30 seconds, .. .
the program automatically runs to the nth process, n receives a value from the keyboard.
So what code do I need to write and how to use the function?
Thanks for your help!
 
Your question is both broad and vague. Broad:
I need to write a program that [does X] So what code do I need to write
It must be nice to have others tell you exactly what to do and you just have to copy and paste. Vague:
runs the first process of an application [...] then automatically disables it
What does that even mean?

You need to spend a bit more time thinking, analysing, understanding and researching, then ask specific questions about just the parts that you're having trouble with. Asking us to provide code for the entire application based on those barely coherent requirements is just too much. You probably need to start by working your way through a beginner tutorial to make sure that you understand the basics of the language and the platform. You then need to break the requirements down into parts and tackle each part individually. For instance, the first thing you said was:
I need to write a program that automatically turns on
That is a problem in and of itself so you should tackle that without regard for anything else. You would start by typing appropriate keywords into a search engine to see what you can find. It should be obvious that the code of an application can't be used to start that application so this isn't even really a C# problem. The next thing was:
runs the first process of an application within 30 seconds
Step 1 would be to clarify in your own mind exactly what "the first process of the application" even means and research how to do that without regard for the 30 seconds. If you can't find what you need or understand what you find, then you can post a question here about that issue specifically. You can provide a FULL and CLEAR explanation of the problem, including what you tried and what happened when you tried it, and we can try to help you fix it, not just write code for you to copy and paste without knowledge or understanding. Once you can do what you need to do, then you can think about how to do it in 30 seconds. Etc.

In my opinion, the biggest mistake that beginners make is attacking problems as a monolith instead of breaking it down into parts and attacking each part individually. Many beginners don't even bother to use search engines because they don't know what keywords to use, because there are no keywords to adequately encapsulate such big problems. The smaller and more specific the problem, the easier it is to search effectively for a full or partial solution. This is really just problem-solving 101 - divide and conquer - and nothing specific to programming. Make the effort to understand your problem, do what you can to solve it and then ask about specific issues along the way and you'll find plenty of people willing to help. Ask us to write your application for you and you'll find people less forthcoming.
 
Last edited:
jmcilhinney
Thank you for your advice. I will present in detail and attach code next time. This is my first time posting on forums so I don't know the rules for asking questions. I'm sorry for the inconvenience and hope admins sympathize.

Here is my code:
C#:
        {
            this.isStartAll = true;
            this.indexStart = int.Parse(this.tbStartFrom.Text);
            while (this.isStartAll && this.indexStart <= int.Parse(this.tbStartTo.Text))
            {
                Process proInfo = Process.Start(Application.StartupPath + "//Info2screen.exe");
                Thread.Sleep(30000);
                InforApp.SetWindowText(InforApp.FindWindow(null, "Info2screen"), this.dataTable.Rows[this.indexStart][1].ToString());
                Thread.Sleep(30000);
                proInfo.Kill();
                this.indexStart++;
            }
            this.indexStart = 0;
            this.isStartAll = false;
        }
I can only automatically turn on and off each process of the same application in turn every 30 seconds.

I need to run two threads:
Thread 1: run the first process for 30 seconds and then automatically shut down and initialize the third process.
Thread 2: after 15 seconds of running the first process, automatically start running the second process. The second process also runs for 30 seconds and then automatically shut down and start the fourth process ...

So what types of statements and functions can I use to make multiple processes?
And can I create multi-process instead of multi-thread?

Thanks for your help!
 
Do you know how many processes you have in total? Do you have a fixed schedule of when to start and stop the processes? What happens if a process is running longer than it should? Should it be killed (with extreme prejudice)? What happens if a process runs for shorter that scheduled? Should the next process start sooner or will it have to wait until its scheduled time?

If this is your first time posting to forums, this may help:

 
Topics merged. No need to create new topics for the same problem. Just update your current topic with the new info unless its about a different problem.

Welcome to the forums @sandralopez9297

OP new code on post #3
 
As Skydiver suggests, there are a number of details that would need to be addressed but it sounds like what you basically want is:
  1. Start your app and wait 30 seconds.
  2. Start running a list of executables, starting one every 15 seconds.
  3. Stop each executable after it has been running for 30 seconds.
Is that about the size of it?
 
You're close. You can try something like this :
C#:
    public class Launcher
    {
        public void Run_Timer()
        {
            Timer timing_Scheduler = new Timer(1000);
            timing_Scheduler.Elapsed += Timer_Elapsed;
            timing_Scheduler.Start();
        }
        private bool app1_Started = false; private bool app2_Started = false;
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {

            if (app1_Started == false)
            {
                Process.Start("notepad"); app1_Started = true;
            }
            else if (e.SignalTime.Second == 30 && app2_Started == false)
            {
                KillProcess("notepad");
                Process.Start("cmd"); app2_Started = true;
            }
        }
        private void KillProcess(string process_name)
        {
            Process[] process = Process.GetProcessesByName(process_name);
            process[0].Kill();
        }
And call it like this. I used a button as an example :
C#:
        private void button1_Click(object sender, EventArgs e)
        {
            Launcher launcher = new Launcher();
            launcher.Run_Timer();
        }
 
Sending a WM_CLOSE message (Winuser.h) - Win32 apps should be preferred to killing the process if possible. It may be possible with Process.MainWindowHandle Property (System.Diagnostics)
Process.CloseMainWindow should be the first option for GUI apps and Process.Close the first option for non-GUI apps and the second step for GUI apps. Process.Kill should only really be used if you MUST end the process and it doesn't respond to either of the other two requests. I've previously advised calling Process.WaitForExit with a timeout period and only killing the process if it doesn't exit in that time.
 
Process.CloseMainWindow should be the first option
Forgot all about that method of Process class, of course this method should be used, it also works for closing a console process that I tested.

Close method does not do anything with the external app, it is part of the Close/Dispose pair.
 
Close method does not do anything with the external app, it is part of the Close/Dispose pair.
I was misremembering what I had read some time ago. This is from the documentation for the CloseMainWindow method:
Kill is the only way to terminate processes that do not have graphical interfaces.
That said, so is this:
The behavior of CloseMainWindow is identical to that of a user closing an application's main window using the system menu.
Console windows have a system menu so I guess that's why it works for a Console app.
 
Sending a WM_CLOSE message (Winuser.h) - Win32 apps should be preferred to killing the process if possible. It may be possible with Process.MainWindowHandle Property (System.Diagnostics)
Judging by the OP's code on #3 - It looks like they want to close the window without any possibilities of the user being prompted though, and hence why they are calling kill. We all know there are procedures for closing windows, but for right or wrong, I assume there is a reason why the OP is using it, so i left it in there.
 
It'll be interesting to see what the OP is actually trying to do. When I first read the requirements, to me it felt like an Operating Systems assignment from a book that as first written in C, and then translated to Java, and now translated to C#. I had a feeling that translations over into C# did not take into account the nuances of shutting down an application in Windows.

The follow-up post #3, on the other hand has given me the impression that the OP is trying to run a machine in kiosk mode OR is trying to do some software stress testing. (Part of Microsoft's stress tests for Windows does some randomized application startups and shutdowns, but since it is Windows after all, it respects the rules of correctly shutting down an application.) The name of the process "InfoScreen2.exe" makes me lean towards the kiosk mode computer, though.

It feels that the company trying to do things that way where it has to have something that schedules the start ups and shutdowns -- including forcible shutdowns -- that way makes me think that it has many dysfunctional teams where each team is writing their own information screen application instead of there just being a single application. It's that or it a student project to demo all of the class student projects in the kiosk, but since the student projects think they are running standalone, then apps must be terminated.

We won't really know unless the OP comes back and tells us what the relationship is between the applications and why she is taking this approach of scheduling run times for them.
 

Latest posts

Back
Top Bottom