0

I am new to C#, so please try to be basic and explain things as much as possible.

This is my code right now:

using System;
using System.Threading;
class MathQuiz
{
  static void Main() 
  {
    Thread ask = new Thread (new ThreadStart (prompt));

    ask.Start();
    Console.ReadKey();
  }

  static void prompt()
  {
    Console.WriteLine ("Testing!");
  }
}

What I want to do, however, is have the new thread read a ConsoleKeyInfo to an object. Then, if the user doesn't press a key in 10 seconds, they move on to the next question, which repeats the process with a different answer.

Hopefully, you're still with me.

I need to have a variable in the MAIN thread be modifiable and callable in the "prompt" thread.

How can I do this?

3 Answers 3

5

Mark it as static volatile so threads can access it without creating class's instance and modify it and use the same value across all threads.

Sign up to request clarification or add additional context in comments.

2 Comments

Mark what as static volatile? I tried a few places to no avail.
A variable that belongs to MathQuiz class. So it will be able to be modified by any thread and every object accessing it would have the most up-to-date value. Also you can use this variable to control thread's behaviour (e.g. stop it when something hapeens in the main app's thread).
2

You can make a static field in your class.

Alternatively, you can pass an anonymous delegate to the thread constructor; it will be able to access local variables.

In either case, it should also be volatile.

2 Comments

I don't know what "it" is. The variable? The class? The methods?
@IanVal: The field / variable.
1

The thread will still have access to the variables you've defined in the same class. The prompt function won't have access because you've defined it as static. Create a static variable, and pass the function to ThreadStart as MathQuiz.prompt

Also, be aware of locking on variables

To the comment below, they are shared across threads:

void Main() 
{
    a.doit();
}

public class a
{
      private static int i = 1;
      public static void doit()
      {
        Thread ask = new Thread (new ThreadStart (prompt));

        ask.Start();
        Console.WriteLine(i);
        Console.Read();
        Console.WriteLine(i);
      }

    static void prompt()
    {
        Console.WriteLine ("Testing!");
        a.i++;
    }
}

4 Comments

Yes it will. But if you try to change value that's benn set in different thread, the original thread will still get old value because variable's not "shared". Thus you should mark it as volatile.
The original thread won't get the old value, it's unpredictable as to what value it will return, which is why locks are important. The volatile keyword simply does the lock work for you
As far as I understand the locks routine, volatile is different. While lock prevents other threads from accessing vulnerable parts of code (e.g. changing var's value), volatile marks field as accessible for all threads at the same time so it's like static field but for threads.
Locks don't prevent threads from accessing variables, it forces them to access them synchronously. Volatile does the same thing, and introduces a type of read-writer lock for accessing the variables. msdn.microsoft.com/en-us/library/aa645755(v=vs.71).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.