C# Hotkey to exit console application - c#

So i was searching a way to set a hotkey to be able to exit a console application.
On my way i found this thread: Global hotkey in console application
which helped me a lot.
So basically i am setting a hotkey to exit my application. My code looks like this:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace ConsoleHotKey
{
class Program
{
static void Main(string[] args)
{
HotKeyManager.RegisterHotKey(Keys.A, KeyModifiers.Alt);
HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);
Console.ReadLine();
}
static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
{
Environment.Exit(1);
}
}
}
and my question is: Is it a good way to exit a console application like this?
I found some poeple saying that it is not such a good way but i couldn't understand why.
Can someone give me some clarifications please?

Related

Is it possible to open my winForm through Revit API button I have created?

I would like to run my winForm through the button I have created in Revit API, but I am new in this field and I am a bit stuck at the moment.
Here in my Command.cs I am stating what button does after clicking on it. Where instead of displaying "Hello World" I would like it to open my winForm.
Is there any way how can I do that? Do I need to somehow link my winForm application to this one?
namespace SetElevation
{
[Transaction(TransactionMode.Manual)]
class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
TaskDialog.Show("SetElevation", "Hello World!");
return Result.Succeeded;
}
}
}
Here is my winForm Program.cs application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
welcome to Revit addin development. You’re close.
I presume if you made your addin work above, that it is compiling as a DLL, not a standalone EXE. Your Winform app appears to be a separate EXE application.
To make this work, you’ll want to add your Form1 to the DLL project. Once you’ve got it in there, you can change TaskDialog.Show to instead these two lines:
var myForm - new Form1();
myForm.ShowDialog();
With that, you’re on your way.

How do I run my Console Application code within a Windows Forms? [duplicate]

This question already has answers here:
Run a console application from a windows Form
(3 answers)
Closed 1 year ago.
I made a Console Application with C# in VS.
I want to make a button, which runs some code. In this case, what I wrote in the Console Application. How would one go about that?
Basically I want to create a set of buttons, where each executes a specific task.
How would I do this? How can I take that code I created earlier and have a button execute it?
I can mention two options of many:
Deal with your Console app as a DLL and reference it in your WinForm Runner App.
Deal with your Console app as an EXE file and run it using Process.Start.
The first option will give you the ability to reach the methods while the second one will deal with it as a totally separate app with all its functionalities as a single application.
Example of the Second Option:
Create a Solution with three projects as in the screenshot below.
Suppose your Console App 1 code is
using System;
namespace SOC.ConsoleApp01
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Written from Console App 1");
Console.ReadLine();
}
}
}
While the second is
using System;
namespace SOC.ConsoleApp02
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Written from Console App 2");
Console.ReadLine();
}
}
}
Now build the Console App1 and Console App 2 by right click on each of them in the solution explorer and click Build or ReBuild.
This will result in execution files generated like in the following screenshots:
In your WinForm Project write the code of Subject buttons like this
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace SO.Console.Apps.Runner
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Process.Start(#"C:\REPOS\SO.Console.Apps.Runner\SOC.ConsoleApp01\bin\Debug\SOC.ConsoleApp01.exe");
}
private void button2_Click(object sender, EventArgs e)
{
Process.Start(#"C:\REPOS\SO.Console.Apps.Runner\SOC.ConsoleApp02\bin\Debug\SOC.ConsoleApp02.exe");
}
}
}
the result is

C# Unable to exit thread. [duplicate]

This question already has answers here:
How do I properly exit a C# application?
(4 answers)
Closed 5 years ago.
New to the threading and its concepts. I have following code where I am using Windows.Forms in console app to print a webpage.
My Code looks like below:
My Application does print the page but it never exists . its stuck at Application.Run();
How do I make my application exit?
Thanks for helping out.
( if I use Application.DoEvents(); wb.print(); does not print. )
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WebBrowserWithoutAForm
{
class Program
{
private static bool completed = false;
private static WebBrowser wb;
[STAThread]
static void Main(string[] args)
{
wb = new WebBrowser();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
wb.Navigate("http://www.google.com");
while (!completed)
{
//Application.DoEvents();
Application.Run();
}
Console.Write("\n\nDone with it!\n\n");
Console.ReadLine();
}
static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Console.WriteLine(wb.Document.Body.InnerHtml);
wb.Print();
completed = true;
}
}
}
MSDN for Application.Run says:
In a Win32-based or Windows Forms application, a message loop is a
routine in code that processes user events, such as mouse clicks and
keyboard strokes. Every running Windows-based application requires an
active message loop, called the main message loop. When the main
message loop is closed, the application exits. In Windows Forms, this
loop is closed when the Exit method is called, or when the
ExitThread method is called on the thread that is running the main
message loop.
The Exit method this talks about is Application.Exit.

Problems with the oputput in the console, C# [duplicate]

This question already has answers here:
Why is the console window closing immediately once displayed my output?
(15 answers)
Closed 7 years ago.
I'm learning to program in C# and I'm having problems with the console, when I run the code below the console show the output but immediately close the window and I can't see anything. I don't know what to do to keep the window open. Any suggestion? I'll be very grateful.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testApp_1
{
class Program
{
static void Main(string[] args)
{
String word = "Hello world!";
Console.WriteLine(word);
}
}
}
This happens because when you reach the last line, you also reach the end of the program, thus it terminates. To keep the console window open, just add
Console.ReadKey() as the last line.
The execution continues after writing to the console and then the program exits. you need to do something to pause the execution, usually you would read something from the console:
Console.ReadKey();
to pause until the user presses a key
so your whole program might look like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testApp_1
{
class Program
{
static void Main(string[] args)
{
String word = "Hello world!";
Console.WriteLine(word);
//if you want the user to exit with any key press do this
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
//if you want the user to hit 'enter' to exit do this
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
}
}
you can read about Console.ReadKey and Console.ReadLine on MSDN
Your code closes immediately, because it doesn't have anything else to do. What you'll want to do is to add this to the bottom of your code:
Console.Readline();
That will cause it to wait until you press Enter.
You can use following method to read some user input:
https://msdn.microsoft.com/de-de/library/system.console.readline%28v=vs.110%29.aspx
But you have to do some input.
You can also use logger frameworks: log4net:
http://logging.apache.org/log4net/
Console.ReadLine() is missing.
If you hit ctrl + F5 ( run without debugging) it will not close. But if you run with debugging, it will close after execution.
You can use Console.ReadLine(); in the end to wait for the user to hit return.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testApp_1
{
class Program
{
static void Main(string[] args)
{
String word = "Hello world!";
Console.WriteLine(word);
Console.ReadLine();
}
}
}

Console.Write in .NET GUI Application

In a C# .NET GUI Application. I also need the console in the background for some tasks. Basically, I'm using a Thrid Party library for some processing (takes lot of time) which writes its intermediate results to console. This processing is a computationally time taking task. So, I'm assigning this task to backgroundworker. I mean background worker calls these library functions. But problem is there is no way for me to show the user status of computation, because I don't have source of the library. I was hoping Console will be shown. But surprisingly Console.WriteLine doesn't seem to work. I mean, there isn't any console window shown. How come?
EDIT:
I tried setting application type = console. But there seems to be a problem. Only, main thread is able to access the console. Only Console.WriteLines executed by main (Application) thread are displayed on console. Console.WriteLines executed by other (BackgroundWorker)threads of the GUI, the output is not shown. I need console only for Background workers. I mean, When background worker starts, console starts & when it ends console will be off.
Create your own console window and use the Console.SetOut(myTextWriter); method to read anything written to the console.
Set your application type to "Console Application". Console applications can also create GUI windows with no problem, and write to the console at the same time.
If you don't have control of the main application, and you want to make sure that a console is shown, you can p/invoke AllocConsole (signature here).
This isn't the same as being a console application though, your application will always get a separate console window, which might be surprising to someone who launched it from a command prompt window. You can work around that with AttachConsole (signature and example here) but shell redirection of the output still won't work. That's why I suggest setting the application subsystem to console if you can.
Followed by #jgauffin, here is the implementation of Console.SetOut method.
Create a TextWriter inherited class.
using System;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ConsoleRedirection
{
public class TextBoxStreamWriter : TextWriter
{
TextBox _output = null;
public TextBoxStreamWriter(TextBox output)
{
_output = output;
}
public override void Write(char value)
{
base.Write(value);
_output.AppendText(value.ToString()); // When character data is written, append it to the text box.
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
}
And in the Form, code as below.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ConsoleRedirection
{
public partial class FormConsole : Form
{
// That's our custom TextWriter class
TextWriter _writer = null;
public FormConsole()
{
InitializeComponent();
}
private void FormConsole_Load(object sender, EventArgs e)
{
// Instantiate the writer
_writer = new TextBoxStreamWriter(txtConsole);
// Redirect the out Console stream
Console.SetOut(_writer);
Console.WriteLine("Now redirecting output to the text box");
}
// This is called when the "Say Hello" button is clicked
private void txtSayHello_Click(object sender, EventArgs e)
{
// Writing to the Console now causes the text to be displayed in the text box.
Console.WriteLine("Hello world");
}
}
}
Original code is from https://saezndaree.wordpress.com/2009/03/29/how-to-redirect-the-consoles-output-to-a-textbox-in-c/
You can check the link for cross-thread calls and advanced implementations at comments.

Categories