My Form1_Load:
private void Form1_Load(object sender, EventArgs e)
{
Thread Usern = new Thread(new ThreadStart(Username));
Usern.Start();
}
And my Username function:
public static void Username()
{
try
{
string user = Environment.UserName;
MessageBox.Show(user.ToString());
}
catch(Exception exc)
{
MessageBox.Show(exc.Tostring());
}
}
Anyone know how I would call "user" used in the function above ^ inside of Form_Load event? I've tried changing the function return type to string but it says it's an invalid change. I know that I could just copy and paste the code from the function into form_load event but it has to be in the function.
Thank you guys in advance and sorry for such a "stupid question" as I didn't find anything related of my problem on SO.
You should probably make user
an instance variable instead of a local variable - in other words, declare it in your class:
private string user;
then in Username()
(which should be renamed to DetectUserName
or something similar) you would just have:
user = Environment.UserName;
Your method should be an instance method though, so that it's associated with an instance of the class - remove the static
keyword from the method declaration.
It's not at all clear why you want to do this in a different thread though...
See more on this question at Stackoverflow