2

I have a console application doing some things in the background, and I want to make it so that the user can change some things that the console application is doing.

I want to add a Windows Form Application to just get the user input and send it to the console application to use. I have looked and couldn't find what I'm looking for

I found this question - Add GUI to Existing Code in Visual Studio - but this didn't help me much.

I have this:

bool OnOrOff = true; 

but I want to check if a check box from the windows form is checked instead of seting it to true like this:

on Windows Form the checkbox is named CheckOnOrOff and it is checked.

bool OnOrOff = CheckOnOrOff.Checked();
Community
  • 1
  • 1
  • @Rob Yes but in the console application, So if one the form the checkbox is checked then i want to send that to the console application. – Because Of Cancer Dec 19 '16 at 03:47
  • @Rob but how? like in the example I cant just call CheckOnOrOff.Checked() because it is not recognized. I have the windows form as a reference to the console application. – Because Of Cancer Dec 19 '16 at 04:00

3 Answers3

1

I assume that the user can change the settings while the console application is running and the effect should be taken immediately. Adding your winforms application as reference in console will not help since it's will be a different application. So this is what I suggest:

  1. Make a new winforms application and change the output type from 'Windows Application' to 'Console Application' so we can see the console. Port your console logic proccess to the winforms project

  2. Add a new static class which will hold flag between winforms and console. Example:

namespace FormWithConsole {
    public static class SharedData {
        public static bool Feature01 { get; set; }
    }
}
  1. Add a checkbox your Windows Form and add code bellow to the checkbox changed event:

private void checkBox1_CheckedChanged(object sender, EventArgs e) {
    SharedData.Feature01 = checkBox1.Checked;
}
  1. Add a button to start your console process, and use thread to start your console process as follow:

Thread thread;
 private void button1_Click(object sender, EventArgs e) {
    if (thread != null) {
        try {
            Console.WriteLine("Aborting current process");
            thread.Abort();
            thread = null
        }
        catch (ThreadAbortException) { }
    }
    ConsoleProcess process = new ConsoleProcess();
    thread = new Thread(process.StartProcess);
    thread.IsBackground = true;
    thread.Start();
}
  1. This is ConsoleProcess class, which hold your console logic

class ConsoleProcess {
     public void StartProcess() {
         while (true) {
             if (SharedData.Feature01) {
                 // Do something here
             }
             Console.WriteLine("Regular process here...");
         }
     }
 }

If you want the form minimized to system tray refer to minimize app to system tray

Community
  • 1
  • 1
Hadi Susanto
  • 170
  • 5
0

I think you should design a database to store the user input. Your console project and window project will run and manage by this database.

Rai Vu
  • 1,595
  • 1
  • 20
  • 30
0

You can take input from windows form (by User) and then pass it to Console application by using parameter argument in Console Application.

The parameter of the Main method is a String array that represents the command-line arguments

So, if I had a console program (MyCApp.exe) like this:

class Program
{
  static void Main(string[] args)
  {
    foreach (var arg in args)
    {
      Console.WriteLine(arg);
    }
  }
}

That I started at the command line like this:

MyCApp.exe Arg1 Arg2 Arg3 The Main method would be passed an array that contained three strings: "Arg1", "Arg2", "Arg3".

If you need to pass an argument that contains a space then wrap it in quotes. For example:

MyCApp.exe "Arg 1" "Arg 2" "Arg 3" Command line arguments commonly get used when you need to pass information to your application at runtime. For example if you were writing a program that pass basic information to copies a file from one location to another you would probably pass the two locations as command line arguments. For example:

MyCApp.exe C:\file1.txt C:\file2.txt Copyit

Here 'C:\file1.txt' is first Argument, 'C:\file2.txt' is first Argument, 'Copyit' is third Argument

Durgesh Pandey
  • 2,314
  • 4
  • 29
  • 43