473,385 Members | 1,324 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,385 software developers and data experts.

use of lock keyword in single threaded applications.

Hi,

Once I wrote a singlethreaded program containing a somewhat large tree
structured set of objects. A treeview was assigned this structure and it was
possible to drag and drop objects - i.e. reorganize the tree structure. Each
object, tagged to a tree node, had a ref to its own userform, however, after
a drag drop operation a non-reproducable bug sometimes occured stating that
the reference was lost and an exception occured. A more .Net experienced
programmer told me that it possibly was the memory manager that was
reorganizing my objects in the memory at the same time I reorganised them and
that I should try to use the lock keyword while I reorganised the tree
structure. Could he be right?

And if yes...
I've searched the net for information on this but all articles I've found
revolves around multithreaded applications. Do you know any articles that
covers this use of the lock keyword - I would like to learn to do it right?

Best regards Jesper, Denmark.
Nov 16 '05 #1
1 4360
Jesper,

I'd advise putting your fingers in your ears whenever this "more .Net experienced programmer" says anything.

If there is only one thread in this application he is talking complete rubbish. Even if there is more than one thread he is still talking rubbish with respect to the garbage collector moving things while your application is executing.

The Garbage collector definitely does move things around in memory, but it suspends all managed threads while it does it and patches up any references before it resumes the threads so this memory management is invisible to your application code.

The lock keyword (which is simply a C# language construct automating the use of the Monitor class) only synchronizes between your application threads not threads the runtime uses internally. If you only have one application thread then all you achieve with lock is to add overhead to your application.

Are you absolutely sure you only have one thread? There are a number of actions that can cause multiple threads to execute in your code without you explicitly creating a thread via the System.Threading.Thread class: certain use of timers; invoking delegates asynchronously; use of remoting to name a few.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi

Once I wrote a singlethreaded program containing a somewhat large tree
structured set of objects. A treeview was assigned this structure and it was
possible to drag and drop objects - i.e. reorganize the tree structure. Each
object, tagged to a tree node, had a ref to its own userform, however, after
a drag drop operation a non-reproducable bug sometimes occured stating that
the reference was lost and an exception occured. A more .Net experienced
programmer told me that it possibly was the memory manager that was
reorganizing my objects in the memory at the same time I reorganised them and
that I should try to use the lock keyword while I reorganised the tree
structure. Could he be right?

And if yes...
I've searched the net for information on this but all articles I've found
revolves around multithreaded applications. Do you know any articles that
covers this use of the lock keyword - I would like to learn to do it right?

Best regards Jesper, Denmark.

Nov 16 '05 #2

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

Similar topics

1
by: Jinlin | last post by:
I found a interesting problem in C# and couldn't explain it. The code to reproduce it is very simple: 1. Create a windows application in C#. 2. Listen to the activated event on the default form...
61
by: km | last post by:
Hi all, is true parallelism possible in python ? or atleast in the coming versions ? is global interpreter lock a bane in this context ? regards, KM
9
by: Jane | last post by:
Our db2diag.log is full of messages like this: 2004-05-31-17.15.10.383766 Instance:tminst1 Node:000 PID:394948(db2agent (TMDB1) 0) TID:1 Appid:GA140956.EF26.03A4B1202647 data management ...
0
by: Bruce Pullen | last post by:
DB2 v7.2 (FP7 - DB2 v7.1.0.68) on AIX 5.2.0.0. We're seeing unexpected single row (then commit) insert locking behaviour. We're seeing Applications that already hold row-level W locks in...
2
by: Rob | last post by:
Hi. I'm having difficulty calling a 3rd party COM object that must be called in the apartment (single) threaded model. As far as I know, there are 3 ways todo this: 1 - use "aspcompat=true"...
5
by: Marco van de Voort | last post by:
Hi, does sb recognize (aspects of) the following problem? Or better, know a solution or direction to search? At work I've inherited a series of delphi applications that access a common...
4
by: UJ | last post by:
What's the easiest/best way to open a single file from multiple applications but so only one can read it at a time? I tried a mutex but had problems. I noticed there is something called a...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
33
by: llothar | last post by:
I'm afraid that the GIL is killing the usefullness of python for some types of applications now where 4,8 oder 64 threads on a chip are here or comming soon. What is the status about that for...
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.