Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

Thread locking in C sharp

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
linuxfedora@yahoo.com.hk - 07 Sep 2007 11:34 GMT
When i try to do something like that:

Button On Click->Call ShowInfo();

class testing
{
private Object locker= new Object();

public void ShowInfo()
{
Console.WriteLine("Begin");

using(locker)
{
for (int j = 0; j < 10; j++)
{
Console.WriteLine("j=" + j);
}
}

Console.WriteLine("End");
}
}

then when i click the button very quick, the result will be like that:

Begin
j=0;
j=1;
Begin
j=0;
Begin

but the what the result i wanted is
Begin
j=0;
j=1;
j=2;
j=3;
j=4;
j=5;
j=6;
j=7;
j=8;
j=9;
End
Begin
j=0
.
.

What is wrong with my code? and how to do the same thing?
How to ensure some codes are completed before another thread is trying
to enter the code?Thanks
Patrice - 07 Sep 2007 11:57 GMT
If "using" is not a typo try "lock" instead ;-)

If this is a typo, make sure you don't create a new testing class instance
in which case you would lock another object...

> When i try to do something like that:
>
[quoted text clipped - 50 lines]
> How to ensure some codes are completed before another thread is trying
> to enter the code?Thanks
Peter Duniho - 07 Sep 2007 18:32 GMT
> [...]
> What is wrong with my code? and how to do the same thing?
> How to ensure some codes are completed before another thread is trying
> to enter the code?Thanks

As Patrice says, you need to use the "lock()" statement to protect a
block of code, not "using()".

Beyond that, you haven't explained your situation very well.  Does the
Button.Click event handler simply call ShowInfo() directly?  Or does it
create a new thread for each click?

I'm assuming the latter, based on the output you described, but you
don't actually explain that anywhere in your post.  If you want better
answers you need to be more specific in your questions.

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.