Program opens cmd then closes - c#

Title says it all, it opens a command prompt then closes in 1/2 a second. i dont know why and i havent found anything on this. i just made a new c# app. Might have something to do with Main as it has no reference.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace dumb_thing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static void Main(string[] args)
{
}
private void Animatetext_Click(object sender, EventArgs e)
{
Animatetext.Text = "";
Thread.Sleep(700);
Animatetext.Text = "P";
Thread.Sleep(350);
Animatetext.Text = "Pa";
Thread.Sleep(350);
Animatetext.Text = "Par";
Thread.Sleep(350);
Animatetext.Text = "Para";
Thread.Sleep(350);
Animatetext.Text = "Parad";
Thread.Sleep(350);
Animatetext.Text = "Parado";
Thread.Sleep(350);
Animatetext.Text = "Paradox";
Thread.Sleep(350);
}
}
}

It seem your codes is about to run a form application. No console application.
But if you are trying to develop a console application, the console will close automatically because there is no command to make it stay or wait the next key press. So you can add a line to make the application stay before closing the Main() method.
static void Main(string[] args)
{
//
// your coding here
//
Console.ReadLine();
}
It is not a good practice to develop a console application via form application template. Unless to get debug output.
Editted
If you want to develop a Form app, you have to remove the Main method in your form class. The Main method is only called once to start the form but you no need to add in your Form classes.
Full Example for Form
Program.cs (Here is where the Main method is placed and called only once to start the other form.)
namespace WindowsFormsApp1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
And now, here is the other form classes.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
Make sure to includes all library needed. This example taken from VS2019 .NET Framework Form Application template.
Other Occurs
In VS, you can set to start your project with console or not. You can set to in your Project Properties > Application Tab > Output type set to Windows Application. By default this setting will set to template setting (if form: windows application and if console: console application).
For best practice in form application, use the debugger console and set output using Debug.WriteLine().

Related

UNIT test. How to open the form in the test?

There is a Windows Forms application.
When the application starts, the form "Form0" will start.
Form0.ShiwInTaskBar = False;
I want to test the application.
I completed:
- created the project "UnitTestProject1".
- prescribed a link to a solution that I will test.
When testing, the Form0 should open.
The "Form0" class does not have a "Show ()" method.
How to open the form in the test?
Form0
namespace rsh
{
public partial class Form0 : Form
{
public Form0()
{
InitializeComponent();
}
}
}
Тест
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
//
using rsh;
namespace UnitTestProject1
{
[TestClass]
public class TestsStart
{
[TestMethod]
public void TestStart()
{
// Тест "Form0"
Form0 form0 = new Form0();
form0.
}
}
}
If you want to test the code:
You should be directly hitting the code as you are doing white box testing. You'll not be able to touch the UI side.
If you want to test the UI:
Please use a tool to interact with the UI of an Windows application:
Click Here..
Of course it is possible if you for your own testing
1.- In your testing project add the System.Windows.Forms reference (References section)
2.- do the below:
Form0 form0 = new Form0();
form0.ShowDialog();

Problem to open REPL using Xamarin Test Cloud

I'm beginner in Xamarin Test Cloud and I want to write tests for Xamarin Test Cloud.
I have Xamarin UITests in my solution and I tried to launch REPL, but UITest REPL window didn't open.
using System;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Xamarin.UITest;
using Xamarin.UITest.Android;
using Xamarin.UITest.Queries;
namespace MurakamiKiev.UITests
{
[TestFixture]
public class Tests
{
AndroidApp app;
[SetUp]
public void BeforeEachTest ()
{
app = ConfigureApp.Android.StartApp ();
}
[Test]
public void TestLaunch ()
{
app.Repl();
}
}
}
Where is the error?
Also, what I need to write to launch specified activity?
If you don't have the application source code in the same solution then you'll need to specify the prebuilt app by pointing to it via a full path.
[SetUp]
public void BeforeEachTest ()
{
app = ConfigureApp.Android.ApkFile("<path-as-string>").StartApp ();
}

attempting to connect to R from C#

I am trying to connect to R using C#. I installed R.Net and referenced it my project. This is my first attempt at C#. Any ideas what I am doing wrong?
This is the sample C# code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RDotNet;
namespace RNet_Calculator
{
public partial class Form1 : Form
{
// set up basics and create RDotNet instance
// if anticipated install of R is not found, ask the user to find it.
public Form1()
{
InitializeComponent();
string dlldir = #"C:\Users\R\R-2.15.2\bin\x64";
bool r_located = false;
while (r_located == false)
{
try
{
REngine.SetDllDirectory(dlldir);
REngine.CreateInstance("RDotNet");
r_located = true;
}
catch
{
MessageBox.Show(#"Unable to find R installation's \bin\i386 folder.
Press OK to attempt to locate it.");
}
}
}
}
}
this is the Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Form1
{
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());
}
}
}
This is actually nothing to do with R. You have probably overwritten the namespace in one place and not in another. You have the code
namespace RNet_Calculator
in your form code. If you open Form1.designer.cs you will probably see
namespace Form1
Just change the namespace from Form1 to RNet_Calculator and your errors should disappear.
EDIT
In response to your edit, you should either change the single RNET_Calculator namespace back to Form1 or you should (but don't have to) change the Form1 namespace in your Program.cs file (and any other files in your project) as well. Doing this means you also should change the namespace in your project properties. Right-click your project, select Properties, and in the Application tab (should be the first one to open), change the "Default namespace" textbox to RNET_Calculator.

How does one use ManagementEventWatcher to keep track of suspend/resume?

I am trying to use ManagementEventWatcher in a service to keep track of when a computer goes in and out of sleep mode. I am new to .NET and C# so I am struggling quite a bit to come up with syntax to make this work.
I have found a blog post that details how he used ManagementEventWatcher to keep track of this status, but he did not post up his entire code. I am trying to go through and make a simple service that creates a .txt log file stating that the computer has been suspended/resumed but am running into problems with the namespaces and types.
Here is the code to the service.cs file:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Management;
namespace SleepNotifierService
{
public class WqlEventQuery : EventQuery { }
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
WqlEventQuery query = new WqlEventQuery("Win32_PowerManagementEvent");
_watcher = new ManagementEventWatcher(query);
_watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
_watcher.Start();
}
protected override void OnStop()
{
_watcher.Stop();
}
void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
try
{
int eventType = Convert.ToInt32(e.NewEvent.Properties["EventType"].Value);
switch (eventType)
{
case 4:
Sleep();
break;
case 7:
Resume();
break;
}
}
catch (Exception ex)
{
//Log(ex.Message);
}
}
public void Sleep()
{
}
public void Resume()
{
}
}
}
Again, this is the first time that I am programming with .NET and C# so I apologize for my ignorance.
I am getting namespace errors such as:
The type or namespace name
'ManagementEventWatcher' could not be
found (are you missing a using
directive or an assembly reference?)
Thanks,
Tomek
You need the System.Management namespace, which is included in the code sample provided by you. I believe you need to reference the System.Management library in your project settings. Follow the following steps to do this( I am assuming you are suing Visual Studio):
Go to the Solution Explorer, and expand your project, right click on the References folder/option and select Add References from the context menu. Now select the .Net tab and select the System.Management from the list and click OK.

C# CefSharp Blank Page

I am trying to learn how to use CefSharp as a browser in c# (unless anyone can suggest any better alternatives).
Initially I set the configuration to Any CPU (and edited the .csproj and app.config) , but the browser didn't display any content, instead it just shows a white/grey screen.
I then tried changing the configuration to x64 (as my PC is x64), but after doing got the following errors:
The type or namespace name 'Winforms' does not exist in the
namespace 'CefSharp'
The type or namespace name 'ChromiumWebBrowser' could not be found
The "FindUnderPath" task was not given a value for the required parameter
"Path".
The OutputPath property is not set for project 'cef.csproj'. Please check
to make sure that you have specified a valid combination of Configuration
and Platform for this project. Configuration='Debug' Platform='x64'. This
error may also appear if some other project is trying to follow a project-
to-project reference to this project, this project has been unloaded or is
not included in the solution, and the referencing project does not build
using the same or an equivalent Configuration or Platform.
My code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public ChromiumWebBrowser chrome;
public Form1()
{
InitializeComponent();
InitializeChromium();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void InitializeChromium()
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
string url = "https://www.google.co.uk/";
chrome = new ChromiumWebBrowser(url);
this.pContent.Controls.Add(chrome);
chrome.Dock = DockStyle.Fill;
}
private void pContent_Paint(object sender, PaintEventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
}
}
Any help or suggestions are greatly appreciated.

Categories