473,326 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Fields are global to all threads of a method running in a class?

C#.Net question for everyone

I have a class that has a method that i run async from the Form.

This class has one field string called log. I need this since i have
an event that can write to it sometimes, and not only my async method.

Now im noticing this log seems to be updated by both of the running
threads. Is there any way to resolve this without locking the log
variable and therefore causing my thread to be locked up as well?
Nov 16 '05 #1
3 1516
MrFile,

You will have to use the lock statement or the Monitor class (which is
what the lock statement uses) in order to protect access to this variable.
Whenever you read or write from this variable, it basically has to be
protected (since it is coming in on two separate threads). Locking the
access doesn't mean that it will deadlock, it just means it will block
access to that variable for the duration of what you are trying to do. As
long as you don't perform some very lenghty operations in the lock segment,
or call Enter on the Monitor class without calling Exit, you should be fine.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MrFile" <we********@yahoo.com> wrote in message
news:9a**************************@posting.google.c om...
C#.Net question for everyone

I have a class that has a method that i run async from the Form.

This class has one field string called log. I need this since i have
an event that can write to it sometimes, and not only my async method.

Now im noticing this log seems to be updated by both of the running
threads. Is there any way to resolve this without locking the log
variable and therefore causing my thread to be locked up as well?

Nov 16 '05 #2
MrFile <we********@yahoo.com> wrote:
C#.Net question for everyone

I have a class that has a method that i run async from the Form.

This class has one field string called log. I need this since i have
an event that can write to it sometimes, and not only my async method.

Now im noticing this log seems to be updated by both of the running
threads. Is there any way to resolve this without locking the log
variable and therefore causing my thread to be locked up as well?


It's not entirely clear what you mean, but there are two golden rules
which sound like they're important in this situation:

1) Never update the UI from any thread other than the UI thread
2) Never access shared data without locking it

I would suggest not locking the field itself, but locking another
object whose use is purely for locking purposes.

If you want each thread to have its own independent copy of a variable,
you might like to look at ThreadStaticAttribute.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Hi MrFile,

How Nicholas and Jon said you have to protected the data that can be
accessed from more than one thread. The other way of protecting a variable
is to update it form only one thread. In other words when you want to update
the variable let this work to be done by one and only one thread. The UI
threads are easier to let do something. So if you don't want to block your
worker threads until updating the filed (however I don't think you should
have problems with that as long as it won't happen all the time and this
operation should be pretty quick) let an UI thread do it. Call some
control's BeginInvoke method and send a delegate to a method that will do
the actual update.

--
HTH
B\rgds
100 [C# MVP]

"MrFile" <we********@yahoo.com> wrote in message
news:9a**************************@posting.google.c om...
C#.Net question for everyone

I have a class that has a method that i run async from the Form.

This class has one field string called log. I need this since i have
an event that can write to it sometimes, and not only my async method.

Now im noticing this log seems to be updated by both of the running
threads. Is there any way to resolve this without locking the log
variable and therefore causing my thread to be locked up as well?

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Oleg | last post by:
Hello there: I've been trying to create two different sets of required fields in one form and to use a radiobutton as sort of a switcher between these sets. In my HTML form there are two...
9
by: David Poundall | last post by:
I have a thread class and I want to be able to track its usage within an application. FYI the class launches aplications in their own thread when the 'launch' method is called. That works OK ...
3
by: MrFile | last post by:
C#.Net question for everyone I have a class that has a method that i run async from the Form. This class has one field string called log. I need this since i have an event that can write to it...
22
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
12
by: Grant | last post by:
I am having great difficulty understanding this and any code samples I find online are kilometres long and complicated to understand...Please could someone give me a simple exampe of how to get a...
6
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the...
4
by: MSDousti | last post by:
Hi I have written a VB .NET app, which uses several threads. I thought that when the user closes the main window (when MainForm.closed event occures, and I call application.exit) all running...
35
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
2
by: Jon Slaughter | last post by:
I was wondering if maybe allowing "fields" for methods. The reason is to encapsulate the data that is mainly used by the method and to prevent the need of having to create new variables every time...
7
by: Jon Slaughter | last post by:
I have some static fields in a class to keep track of "global" information but this information is local to each form that the class is used on. e.g., the class represents a base...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.