The problem is that after I added the new class, the error came up when I did build the solution. What can be wrong?
In Form1, I don’t have any code yet.
I just added a new class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenHardwareMonitor.Hardware;
namespace OpenHardwareMonitorReport
{
class Program
{
static void Main(string[] args)
{
Computer computer = new Computer();
computer.Open();
var temps = new List<decimal>();
foreach (var hardware in computer.Hardware)
{
if (hardware.HardwareType != HardwareType.CPU)
continue;
hardware.Update();
foreach (var sensor in hardware.Sensors)
{
if (sensor.SensorType != SensorType.Temperature)
{
if (sensor.Value != null)
temps.Add((decimal)sensor.Value);
}
}
}
foreach (decimal temp in temps)
{
Console.WriteLine(temp);
}
Console.ReadLine();
}
}
}
Then I see file Program.cs and the error on Main():
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace NvidiaTemp
{
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());
}
}
}
Error 2 Program 'D:\C-Sharp\NvidiaTemp\NvidiaTemp\NvidiaTemp\obj\x86\Debug\NvidiaTemp.exe' has more than one entry point defined: 'NvidiaTemp.Program.Main()'. Compile with /main to specify the type that contains the entry point. D:\C-Sharp\NvidiaTemp\NvidiaTemp\NvidiaTemp\Program.cs 14 21 NvidiaTemp
I experienced this issue after adding an xUnit test class to my .NET Core 2.1 project.
The following article gives a detailed explanation of why, and provided the answer that worked for me - here.
Basically, the compiler automatically generates a Main
for the new class. You can provide a directive in your .csproj file to keep this from happening:
<GenerateProgramFile>false</GenerateProgramFile>
Add this to your <PropertyGroup>
section and recompile.
Others have pointed out that you have two static void Main methods. There are two easy fixes for this, one obvious and one that hasn't been specifically mentioned yet:
Main1
, NotMain
, etc.With solution 2, you can have identical Main(string[] args)
signatures in different classes without the compiler whining.
A C# program can only have one Program.Main(). Main is the first method run when the program starts, so the compiler needs to know which one is the real one, and it can't if you have two.
It looks like you're making a Windows application. You should either add code to the existing main, or add it to an event handler triggered by your main form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With