c# Visual Studio not showing debug string [duplicate] - c#

This question already has answers here:
How can I execute code after my form starts?
(5 answers)
Closed 5 years ago.
I am fairly new to C# but I feel like this should output "hi":
using System;
using System.Windows.Forms;
using System.Diagnostics;
namespace PathMet_Controller
{
class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Debug.WriteLine("hi");
}
}
}
My output window gives me this:
In playing around, it did output "hi" once, but it would only output after I stop running the file.

Because Application.Run will start special loop which will end when you exit your win forms application.
If you want to print something at start, use events provided by Form class.

Related

How to make windows form app resize correctly [duplicate]

This question already has answers here:
Creating a DPI-Aware Application
(9 answers)
Closed 2 years ago.
I'm developing a windows form GUI, and came up with this tricky problem: When windows resolution is set to 100%, everything is normal, Good Sample:
whereas when windows resolution is set to 150%, the graphical displays as well as the texts failed to autosize correctly, blocks messed up with each other, Bad sample:
Any advice would be helpful! Thanks.
Thanks guys and shout out to Ken White I've found my solution here: Creating a DPI-Aware Application
In short I added following and it worked:
namespace myApplication
{
static class Program
{
[STAThread]
static void Main()
{
// ***this line is added***
if (Environment.OSVersion.Version.Major >= 6)
SetProcessDPIAware();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
// ***also dllimport of that function***
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
}
}

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.

C# - How to change the Startup form Programmatically? [duplicate]

This question already has an answer here:
Changing startup form in C#
(1 answer)
Closed 6 years ago.
Is it possible to change the startup form programmatically, because when my application starts it asks for the serial key and I want it to only show up ONCE and when the user enters the serial number and presses the continue button, it will check if the serial number is correct and take him to the main form.
But how is it possible to change the startup Form after the user enters serial number?
For example i want it to lead them to Form1 instead of TrialCheck.
I'm using Visual Studio 2015 C#.
Your project has a Program.cs file which is the entry point to your program.
In its Main() method you should perform your validation and then decide which form to load.
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool registered = FunctionThatChecksSerialNumber();
if (registered)
{
Application.Run(new Form1());
}
else
{
Application.Run(new TrialCheck());
}
}
}

Struggling with Threading? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm newbie at C#, and I'm struggling with Threading(maybe).
When I start debugging, and execute one by one, ...and loading form is completed, procedure is placed at strange position like this :
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0)
Application.Run(new frmMain(args[1]));
else
Application.Run(new frmMain());
} // Stops here
I suspected it is normal, so I created new project and executed. and result was different with before like this:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); // Stops here
}
Now I'm very confused, So I need a help. someone tell me it is threading, but I didn't used any threading in my code.
P.S. I used NAudio Library to play music so I declared on frmMain class like this:
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using NAudio;
using NAudio.Wave;
using TagLib;
(...)
AudioFileReader _audioFileReader;
IWavePlayer _waveOutDevice = new WaveOut();
(...)
P.S. 2 ..And this is constructor of frmMain.
InitializeComponent();
this.listMusic.DragOver += new DragEventHandler(this.FileDragOver);
this.listMusic.DragDrop += new DragEventHandler(this.FileDragDrop);
this.listMusic.DoubleClick += new EventHandler(this.listDoubleClick);
_waveOutDevice.PlaybackStopped += new EventHandler<StoppedEventArgs>(this.PlaybackStopped);
It could be that another part of the code is using multiple threads. I've had situations where there were multiple threads and as I single-stepped through code, the current location bounces around. It's not really a problem as long as you understand what it's doing, but if you halt execution in the debugger, it can stop somewhere you don't expect.
If you want to stop execution in your code, place breakpoints (F9) on lines of your code, then, when you've started an operation that enters your code, step through as desired.
If you're curious about what other threads are running, there is the Threads window; while debugging, go to the Debug window, select Windows, then Threads. If you're not creating threads of your own, it may or may not be useful, but sometimes it's handy to see what might be loaded when you pause execution in the debugger.

visual studio open windows form from console application c# [duplicate]

This question already has answers here:
how to run a winform from console application?
(10 answers)
Closed 8 years ago.
The title is a bit vague, but my problem is this. In Microsoft visual studio im making a console application in c#, and in the code i wanted to be able to launch a windows form inside the same project. In visual basic this can be done with Application.run(formName), however if done in c# using a refrence, the windows form oppens but then immediately stops responding. What is the correct way to open a form from a console app?
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number");
int x = int.Parse(Console.ReadLine());
if (x == 3)
{
menu menuInstance = new menu(); //menu would be the name of the windows form
menuInstance.Show();
}
Console.ReadKey();
}
}
}
Use the same code a WindowsForms project does, Application.Run is still the way to go:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Hard to say what you did wrong with your original attempt at that, as you didn't post that code.

Categories