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 / July 2006

Tip: Looking for answers? Try searching our database.

Intra-thread lock

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cj_junktrap@mail.com - 29 Jul 2006 18:52 GMT
Hi

Is there something like an intra-thread lock?  I'm trying to prevent
re-entrant code, and the lock statement will only prevent one thread
from entering the critical section while another thread is in it -
since my code is triggered by a gui event, it's not impossible that it
could be called twice from the same thread.  I could simulate a lock by
having a private variable "busy" and setting that to true when I start
processing, false when I stop, and check it before I begin processing -
but then either I have to throw an exception if I'm already busy
processing (which I'd prefer not to do), or I'd have to use a polling
wait loop to simulate a blocking call - which seems ugly.

Any ideas of a better way to do this?

Thanks
Markus Stoeger - 29 Jul 2006 19:16 GMT
> Hi
>
[quoted text clipped - 10 lines]
>
> Any ideas of a better way to do this?

I'm not 100% sure if I fully understand what you mean. While GUI event
is being processed, you can be sure that it will _not_ be fired a second
time because the windows message loop is blocked until your processing
is done (unless you are calling something like Application.DoEvents in
your processing code, which I hope you don't anyway ;-).

Anyway, you cannot have the current thread wait until the current thread
is done processing something else. That would always result in a dead lock.

The method with the "busy" variable would work. If busy is already true
-> handle it! Either by throwing an exception or by enqueuing the new
work item somewhere for later processing.

hth,
Max
cj_junktrap@mail.com - 30 Jul 2006 11:37 GMT
> > Hi
> >
[quoted text clipped - 26 lines]
> hth,
> Max

Well, I am using DoEvents, as I mentioned in the other thread, but even
without that it could still be called twice.  Picture this:  I have a
form, with two buttons.  In my form constructor, or somewhere, I create
an instance of my object.  Each button calls myObject.DoSomething(),
which, say, displays another form using form.Show().  When this other
form is closed, it raises an event or calls a delegate.  Now the user
clicks on button 1,  but doesn't close the form yet; then clicks on
button 2.  My DoSomething() method will then be called again.

Basically, my problem is that I have an app with potentially many
forms; one of them will trigger the opening of a 2nd form.  I want the
user to still be able to use the other forms, so I can't open this 2nd
form using ShowDialog, since it will be modal with respect to the
entire app.  If I could make it modal only with respect to its parent
form, that would solve all my problems.
Lloyd Dupont - 31 Jul 2006 06:21 GMT
> Well, I am using DoEvents, as I mentioned in the other thread, but even
> without that it could still be called twice.  Picture this:  I have a
[quoted text clipped - 11 lines]
> entire app.  If I could make it modal only with respect to its parent
> form, that would solve all my problems.

Apart from the fact you are confused I hardly see any problem in what you
tryed to achieve.
Not being sure of what is your problem exactly I wrote a a possible (and
possibly irrelevant) solution below.

class AnObject : Object
{
   bool done = false;
   public void DoSomethingOnce()
   {
         if(done)
           return;
       DoItDamnIt();
       done = false;
   }
   void DoItDamnIt()
   {
       // whatever it is you want to do
   }
}
class MyForm : Form
{
   AnObject anObj;
   Button perhaps, maybe;

   public MyForm(AbObject anObj)
   {
       this.anObj = anObj;

       perhaps = new Button();
       perhaps.Location = new Point(10, 10);
       perhaps.Parent = this;
       perhaps.Text = "Perhaps";
       perhaps.Click += delegate(object sender, EventArgs e) { new
MyForm(anObj).Show(); };

       maybe = new Button();
       maybe.Location = new Point(80, 10);
       maybe.Parent = this;
       maybe.Text = "Maybe";
       maybe.Click += delegate(object sender, EventArgs e) {
anObj.DoSomethingOnce(); };
   }
}
Carl Daniel [VC++ MVP] - 29 Jul 2006 19:22 GMT
> Hi
>
[quoted text clipped - 11 lines]
>
> Any ideas of a better way to do this?

Use ThreadPool.QueueUserworkItem to effect a queue of work.  Depending on
the function, it may be appropriate to simply ignore attempts at re-entry -
only you can decide for sure.

-cd
Daniel MOSMONDOR - Mosh - 30 Jul 2006 11:26 GMT
i guess that second gui event can't come while first is processed due to
design of gui events that are in fact window messages.

other than that, use simple bool variable to cover possibility of
rentrancy - for example when you have form_onSize that you want to hande and
you have resizing the form from withing the event...

bool _inResize=true;

void form_onSize(EventArgs)
{
   if (!_inResize) {
        _inResize=true;
        //  do something
        ...
        ...
        _inResize=false;
   }
}

> Hi
>
[quoted text clipped - 12 lines]
>
> Thanks
cj_junktrap@mail.com - 30 Jul 2006 11:39 GMT
> i guess that second gui event can't come while first is processed due to
> design of gui events that are in fact window messages.
[quoted text clipped - 15 lines]
>     }
> }

Yes, but then the second time it's called it will simply return without
doing anything, which isn't what I want.  I want it to wait until the
busy flag is false, but without a polling loop.

> > Hi
> >
[quoted text clipped - 12 lines]
> >
> > Thanks

Rate this thread:







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.