How to know if it is the Windows console? - windows

Is there any safe way to know whether the actual window is the windows console?
GetConsoleTitle does not works since the title can be changed.

When you run CMD (or any console based programs) from your program via CreateProcess or ShellExecuteEx functions, you'll get its process ID. Use it with EnumWindows and GetWindowThreadProcessId to find its console window.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Process[] processes = Process.GetProcessesByName("cmd");
foreach (Process p in processes)
{
var window = p.MainWindowHandle;
// Do something
}
}
}
}

Related

WPF Designer Extensibility: how to get the current designer zoom value?

I created a simple custom control with design-time support like the one described here
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.ComponentModel;
namespace CustomControlLibrary
{
public class ButtonWithDesignTime : Button
{
public ButtonWithDesignTime()
{
// The GetIsInDesignMode check and the following design-time
// code are optional and shown only for demonstration.
if (DesignerProperties.GetIsInDesignMode(this))
{
Content = "Design mode active";
}
}
}
}
Now I need to know what is the zoom factor set at design time by Visual Studio, but I cannot figure out how.
Is there an event that I can listen to for finding out this? Or is there a property that could reveal this?
I'm not sure if this is the most elegant way of doing it, but this is what I came up with. This problem has been thwarting me for ages!
private Point GetScaleFactor()
{
var rootVisual = PresentationSource.FromVisual(this)?.RootVisual;
if (rootVisual != null)
{
var transformToRoot = TransformToAncestor(rootVisual);
if (transformToRoot is Transform t)
{
return new Point(t.Value.M11, t.Value.M22);
}
}
return new Point(1, 1);
}

Unity WebGL - Is there a way to clear all errors on build?

I am wondering if there's a way to clear the error that pops up in the browser after the game is built?
I tried this code I found online, but it doesn't work as expected. I tried to put the script below in the Editor folder, but it didn't work as well.
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEngine;
using System.Reflection;
//using UnityEditor;
public class ClearConsole : MonoBehaviour
{
public void ClearLog()
{
var assembly = Assembly.GetAssembly(typeof(UnityEditor.Editor));
var type = assembly.GetType("UnityEditor.LogEntries");
var method = type.GetMethod("Clear");
method.Invoke(new object(), null);
}
void Update()
{
ClearLog();
}
}
#endif
Sorry for my English. Thanks in advance.

ToggleGroup in Unity3D, which is the active one?

How do we know which toggle is active from the toggle groups?
Unity Doc says use ToggleGroup.ActiveToggles but I can't understand how to use it?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Linq;
private void Example()
{
Toggle theActiveToggle = yourToggleGroup.ActiveToggles().FirstOrDefault();
Debug.Log("It worked! " + theActiveToggle.gameObject.name);
}

LINQ not working on IEnumerable

I'm using Autofac (I've registered the base nuget package in a console app) and want to take a look at a list of registrations.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
// First, create your application-level defaults using a standard
// ContainerBuilder, just as you are used to.
var builder = new ContainerBuilder();
var appContainer = builder.Build();
appContainer.ComponentRegistry.Registrations.Where(x => true);
}
}
}
The problem is the line
appContainer.ComponentRegistry.Registrations.Where(x => true);
Here intellisense is not giving me the Where linq method however it does compile as far as I can tell without any warnings, errors in messages.
I tried this further down
IEnumerable<string> list = new List<string>();
list.Where(x => true);
And intellisense is working correctly and giving me all the standard list methods.
I've tried this in a few different apps from scratch and I'm getting the same behaviour.
Any ideas as to whats going on?
EDIT:
The following works and shows correctly in intellisense
IEnumerable<IComponentRegistration> test = new List<IComponentRegistration>();
test.Where(x => true);
I'm using
<package id="Autofac" version="3.0.1" targetFramework="net45" /> from nuget.
and hovering over the ComponentRegistrations gives
and in this case scope is defined as
ILifetimeScope _scope;
However I get the same thing if i try directly off this
var builder = new ContainerBuilder();
var appContainer = builder.Build();
appContainer.ComponentRegistry.Registrations.Where(x => true);
Also IComponentRegistry is defined as (in Autofac)
public interface IComponentRegistry : IDisposable
{
...
IEnumerable<IComponentRegistration> Registrations { get; }
...
}
Comment copied to answer.
If I've understood you correctly and your problem is that intellisense isn't working on the line
appContainer.ComponentRegistry.Registrations.Where(x => true);
You should probably try and disable your addons and see if that takes care of it as it's working fine for me. Since you say you had someone else confirm it, maybe start with any addons that your installations have in common.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
// First, create your application-level defaults using a standard
// ContainerBuilder, just as you are used to.
var builder = new ContainerBuilder();
var appContainer = builder.Build();
var registrations = appContainer.ComponentRegistry.Registrations.Where(x => x.Target.Equals("test"));
}
}
}
Try assigning the linq expression to a variable and see if it works

How to get how many threads are running in multithreaded windows service application

I have a multithreaded windows service application, I want to know every moment how many threads(with thread id, thread name, corresponding process id) are running which are created by my application.
Thank in advance.
With C#
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Process proc = Process.GetCurrentProcess();
Console.WriteLine(proc.Threads.Count.ToString());
}
}
If you want working threads for some other process(not current app) change GetCurrentProcess() with GetProcessById(wanted proc) or GetProcessByName(wanted proc)
If you want to get something specific from the threads
proc.Threads[index].[take a look at the properties];
EDIT: How can I get all threads name of the process?
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Process[] proc = Process.GetProcessesByName("youprocess");
Console.WriteLine(proc[0].Threads.Count.ToString());
}
}
GetProcessesByName returns an array of processes(there may be several processes with the same names).
If you are sure there is only one - proc[0] is what you want. If There are several you can access them with index - proc[0], proc1, etc.... proc is null if there are not processes with "your name"
A great utility for doing what you describe is Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
By using the windows task manager
Ctrl-Alt-Del
View->SetColumns
Check Thread Count

Resources