How do I call a function without the Main function in C sharp?
trying to run a function without putting it in the Main() when the program is run. how do I start the new created function? trying to call RunMix() in the Main() but get an error because of the lable1
namespace mixer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 0;
public void RunMix()
{
while (i == 0)
{
label1.Text = knob1.Angle.ToString();
Application.DoEvents();
}
}
private void Form1_Load(object sender, EventArgs e)
{ 开发者_JS百科
RunMix();
}
}
}
In a console application, the Main() method is the entry point into the application. You have to put you code to start the application in there.
If you only want to test the function you can use the NUNIT or the Microsofts Unit Testing Framework. Otherwise you have to call the function from the Main().
Alright my first answer was completely off the topic because of your mysterious question. Now that you have updated it I have better - not complete - understanding of what do you mean.
Looking at code I guess what you are trying to do is to change the value of label when knob1 control's angle changes. If knob1 is a control it should have a change event and you should change value of label1 inside knob1_change event handler. If knob1 doesn't have any event - highly unlikely - then you should use a timer instead. Loop is simply a bad idea in your situation. Timer should work like this
Timer timer = new Timer();
public void RunMix(object sender, EventArgs e)
{
label1.Text = knob1.Angle.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
timer.Interval = 100;
timer.Tick += new EventHandler(RunMix);
timer.Start();
}
Stop timer when form is closed or use activate/deactivate cycle depending upon your requirement.
You can't have another method besides Main
as an entry point for the app.
For ex you can't start a program from a function like this:
public static MyMain(string[] args)
{
}
This is a java code for the same but i don't know the same in C#. But i think it can be possible in C# too.
class staticEx { static { System.out.println("Inside Static Block"); System.exit(0); } }
The above code is tested and got it from here while GOOGLEing. There can be a possibility of similar thing in C# as well.
精彩评论