Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
It's frustrating how this forum does not seem to understand questions. So, as a result of them deleting my question I thought; "Why not just repost it with simple details".

I have a nice void here:

C#
public void ReloadChunks()
{
 // -- A small hint: Look how this is not created in the thread.
 VariableNotInTheThread = 1;
}


So, I can do this without a thread but it lags my UI. So, therefor I would like to make it threaded. But you can NOT set non static variables in threads. My question is how would I do that?
Posted
Comments
Richard C Bishop 8-Apr-13 10:32am    
That is how you are supposed to ask questions. Provide samples of your code and ask specific questions about it.
Ian A Davidson 8-Apr-13 11:13am    
Still not convinced it's a very good question. Where and how is "VariableNotInTheThread" defined? You say "Look how this is not created in the thread", but you don't have a thread in your example - From where are you calling this "ReloadChunks" method? If you're accessing a different object instance from your thread then of course the variable will not be the same because it will be part of the other object. You could make the variable static, but then for anything but the simplest of assignments you will need to ensure that you use suitable locking access (e.g. Interlocked.Exchange). Keeping the variable it non-static, if you access the same instance of the object created in one thread from another thread then it will be setting the same value. You would still of course need to make sure your class is thread-safe (again, for example, by using appropriate locking mechanisms when accessing shared data). Regards, Ian.
[no name] 8-Apr-13 11:57am    
"frustrating how this forum does not seem to understand questions", then you understand how we feel when questioners do not understand how to ask a question.
Sergey Alexandrovich Kryukov 8-Apr-13 16:40pm    
There is nothing much to feel about or be frustrated. I noticed that those inquirers who ask reasonable questions are much more satisfied. It tells me something... :-)
—SA

1 solution

Of course you can set a static variable in a thread, but think if it makes any sense or not. But if a variable is static, it can be accessed by some other thread. If it is not, there is no a problem (there is a potential problem), but then, there is no need to make an object static. So, if it is static, you can do this:
C#
static internal int VariableNotInTheThread = 0; // or something
static internal object LockObject = new object();
// ...

public void ReloadChunks()
{
    lock (LockObject)
       VariableNotInTheThread = 1;
}


So, if you use anything across threads, you should synchronize it; in this case, with the lock. Static objects are evil, as a rule of thumb.

However, this is a bad technique which should rarely be used. For one of good techniques, please see my answers where I introduced the very useful thread wrapper which, in particular, allows you to share objects in an encapsulated way, and avoid static objects:
How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^] (this one with lock).

—SA
 
Share this answer
 
v2
Comments
Yvar Birx 8-Apr-13 10:35am    
Thanks a lot for your answer. The thing is, they should not be static as it's actually a void I used to use without a thread, but now it's a thread I can not access the variables.
Sergey Alexandrovich Kryukov 8-Apr-13 10:38am    
What void?! It's unrelated to having a static member. And the access is not related to static...
—SA
Yvar Birx 8-Apr-13 10:39am    
"ReloadChunks". Access is related to being static. Let's suppose you make a public integer, you can NOT access it via another thread.
Sergey Alexandrovich Kryukov 8-Apr-13 10:43am    
No. Unfortunately, you don't understand the bare basics. "public" in unrelated to "static", and unrelated to threads, it is related to access, formally.
—SA
Yvar Birx 8-Apr-13 14:44pm    
So, let's assume I will make a variable:
public Chunk[] Chunks;

I could set it via:

public void ReloadChunks()
{
Chunks = new Chunk[9];
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900