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 / December 2005

Tip: Looking for answers? Try searching our database.

C++ sytnax for thread locking

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bill - 29 Dec 2005 16:12 GMT
All,

I have a WindowsForm app and am happily getting data from my DLL which
is on a different thread than my UI. I have now arrived at the
situation where I need to lock the data that is being communicated.
Unhappily, again, I  can find no examples using C++ to lock threads in
a .NET Windows Form application. All of the examples are in either C#
or VB. The namespace that works for C#:

using namespace System::Threading;

does not support the "CSingleLock singleLock(&m_CritSection);" that is
shown in some examples. And, it does not also support the "lock(this);"
shown in other examples. How is thread locking done with C++ in a
Windows form app?

TIA

Bill
Richard Grimes - 29 Dec 2005 17:58 GMT
> All,
>
[quoted text clipped - 11 lines]
> "lock(this);"
> shown in other examples.

Take a look at code in C# that locks data using something like ILDASM.
This code:

lock(this)
{
 //some code
}

is implemented like this:

Monitor.Enter(this);
try
{
 //some code
}
finally
{
  Monitor.Exit(this);
}

lock() is just syntactic sugar, you can use Monitor in your own code.

> How is thread locking done with C++ in a
> Windows form app?

The only think I should warn you (but I guess you know anyway) is that
you should never use a blocking thread synchronization on the GUI
thread. The reason is that when the thread is blocked it will not be
able to update the UI and as a consequence you'll find that the UI will
freeze.

Richard
Signature

Fusion Tutorial: http://www.grimes.demon.co.uk/workshops/fusionWS.htm
Security Tutorial:
http://www.grimes.demon.co.uk/workshops/securityWS.htm

bill - 29 Dec 2005 18:58 GMT
Richard,

Coincidentally, after my posting and before your response arrived I was
reading your excellent book "Programming with Managed Extensions for MS
Visual C++ .NET". Actually I poke around in it from time to time
looking for various things. In any case, I found your example on page
240 of using the ReaderWriterLock() to be clear and to the point. I
will say that I needed to stick a "return true" into your Data() class
before it would compile.

All of that being said, I finally ended up with the following:

          if (0 == Interlocked::Exchange(&usingResource, 1))
          {
           // do stuff

           //Release the lock
           Interlocked::Exchange( &usingResource, 0);
          }

Thanks for your response,

Bill
David Browne - 29 Dec 2005 19:30 GMT
>> All,
>>
[quoted text clipped - 32 lines]
>
> lock() is just syntactic sugar, you can use Monitor in your own code.

Further, typically in C++ a wrapper object with a destructor is used to
manage resource lifetime.  In C++/CLI an object with a destructor or a
Disposable object declared in automatic storage will be automatically
cleaned up as it leaves scope.  This is similar to the using block of C#.
EG

#include "stdafx.h"

using namespace System;

ref class Lock
{
 Object^ o;

 ~Lock()
 {
   System::Threading::Monitor::Exit(o);
   Console::WriteLine("Monitor Exited.");
 }

public:
 Lock(Object^ objectToLock)
 {
   o = objectToLock;
   System::Threading::Monitor::Enter(o);
   Console::WriteLine("Monitor Entered.");
 }
};

int main(array<System::String ^> ^args)
{
   Console::WriteLine(L"Hello World");
   Object^ o = gcnew Object();

   {
     Lock lock(o);
     Console::WriteLine("Automatic Lock is in scope");
   }

   Console::WriteLine("Automatic Lock is out of scope.");

   Console::ReadKey();

   return 0;
}

Outputs:

Hello World
Monitor Entered.
Automatic Lock is in scope
Monitor Exited.
Automatic Lock is out of scope.

David

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.