4 Replies - 954 Views - Last Post: 11 November 2011 - 08:09 PM Rate Topic: -----

#1 cloudd901  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-November 11

Question Updating a Variable from Form on 2nd Thread

Posted 10 November 2011 - 10:13 PM

I'm still pretty new to C#. Learning on my own has it's disadvantages, but maybe someone can help.

I'm coding a simple game (pick-up sticks) to get use to coding. Once you click begin, I have a popup Form that allows you to select the computer type (Basically type 1, 2, or 3). I open the second Form in a new thread and keep the first thread paused until the second is closed.
namespace Sticks
{
    public partial class guiMain : Form
    {
        //Set Public Namespace Variables
        public int compType = 0; //1=timid 2=greedy 3=smart
        public guiMain()
        {
            InitializeComponent();
        }

        private void btnBegin_Click(object sender, EventArgs e)
        {
            btnHelp.Enabled = false;
            btnBegin.Enabled = false;
            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
            t.Start();
            this.Invoke((MethodInvoker)delegate { label2.Text = "Test"; });
            while (t.IsAlive)
            {
                Thread.Sleep(100);
                this.Refresh();
            }
        }
    }
}



The second Form contains buttons that are suppose to be able to set compType variable but I don't know how to update it from a Form on another Thread.

namespace Sticks
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //set compType to 1
            Close();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //set compType to 2
            Close();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            //set compType to 3
            Close();
        }
    }
}



Now remember, I'm still starting out. I am using Microsoft Visual C# 2010 Express for my coding. I have Google this issue but only came up solutions that I did not understand how to implement. Thanks in advanced!

Is This A Good Question/Topic? 0
  • +

Replies To: Question Updating a Variable from Form on 2nd Thread

#2 cloudd901  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-November 11

Re: Question Updating a Variable from Form on 2nd Thread

Posted 11 November 2011 - 04:30 AM

Yay, I think I got it.
Using a Global Variable (sort-of).
I made a third class with public variables that I can call anywhere in my script.
In this case, I added the compType variable that I can update in the 2nd Thread.
    public static class GlobalVars
    {
        public static int compType;
    }



And I call it with and update it with:
int Mypile1cnt = GlobalVars.pile1cnt;

//
//do stuff her and update Mypile1cnt...
//

GlobalVars.pile1cnt = Mypile1cnt;



If someone has another way of doing this, I would like to see.
Thanks!
Was This Post Helpful? 0
  • +
  • -

#3 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Question Updating a Variable from Form on 2nd Thread

Posted 11 November 2011 - 06:52 AM

Hiya,

In my opinion you are overcomplicating things by trying to using threading before you have the basics, such as passing things between methods and classes, firmed in your mind.
I'm not an expert myself yet, so I don't fully understand how threading works but you definitely do not need it to make this game work. I'd consider adding fancy stuff after you have the core of the program working.

Anyway... classes will work for what you need, you'll want to use GET & SET within your class though by doing that you will be keeping the value stored in your class separate from the one in your main program.
You'll see a lot of the class collections in Visual Studio are set up in this way, if you explore them.

    public class MyTest
    {
        public MyTest();

        public bool Value
        {
        get;
        set;
        }
        // You can write the above like this, included both to avoid confusion.
        public string myname { get; set; }
    }

You then create a new object of your custom type to access the class. You'll notice if you to "Test." and "myOb." different options will appear in Intellisense after you type the dot.
MyTest myOb;

myOb.myname = txtBoxName.Text;


You should check out the Object Explorer to it contains a tree list of all the classes, your own and the Visual Studio ones, with submenus etc to show the values you can call from them.

It's definitely worth following a few tutorials and reading a few articles about classes and inheritance so you understand their use. They'll explain better than me most likely to :P

This post has been edited by negligible: 11 November 2011 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5578
  • View blog
  • Posts: 11,918
  • Joined: 02-June 10

Re: Question Updating a Variable from Form on 2nd Thread

Posted 11 November 2011 - 10:34 AM

This comes up a lot. We have some nice tutorials on form-to-form, class-to-class communication.

See FAQ #2


FAQ (Frequently Asked Questions - Updated Oct 2011

Spoiler

Was This Post Helpful? 0
  • +
  • -

#5 cloudd901  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-November 11

Re: Question Updating a Variable from Form on 2nd Thread

Posted 11 November 2011 - 08:09 PM

View Postnegligible, on 11 November 2011 - 06:52 AM, said:

In my opinion you are overcomplicating things by trying to using threading before you have the basics, such as passing things between methods and classes, firmed in your mind.


I took your opinion and found out how to work with forms (without using multiple threads). I'm still getting use to object oriented programming. I spent the last 3 years scripting with AutoIt (more or less a BASIC language). I just had to create a new form object and use ShowDialog to get the result I needed.
Form2 f2 = new Form2();
f2.ShowDialog();



I'm going to update my code using GET and SET and ShowDialog. Thanks for all your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1