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

synclock and mutex

cj
I take it a mutex is like a synclock but visible outside the program?
Jun 1 '07 #1
2 3703
"cj" <cj@nospam.nospamwrote:
>I take it a mutex is like a synclock but visible outside the program?
Yea, that's a pretty good working definition.

There are some differences, but at the end of the day they're both
True/False flags for "Busy / Not Busy".

The Monitor (the thing that underlies the SyncLock) has some nice methods on
it like TryEnter and PulseAll that make it easy to use in some situtations.
It also has the handy VB keyword SyncLock that deals with the Try/Finally
semantics required to use a Monitor in an Exception safe way.

On the other hand, the Mutex is also a WaitHandle, and therefore works with
all of that infrastructure (WaitOne, WaitAll, WaitAny, etc), which makes it
easy to use in some situations. For example, you can register a WaitHandle
with the CLR threadpool, and get a callback when the WaitHandle is set.
There are some scenarios where this behavior is very handy.

Personally, I tend to use:
- Monitor (Synclock) for almost all of my locking.
- ManualResetEvents for almost all of my Waiting ("Is this done yet? Call me
back when it's done.")
- Mutex for ensuring an application is "single instance".

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
Jun 1 '07 #2
cj
Thanks. I've used synclock to make sure multiple threads don't trample
on each other when they are using a common class to say increment a
counter (to keep track of how many threads/transactions are currently
being handled) or write a log file. I was using:

<STAThread()Public Sub main(ByVal CmdArgs() As String)
Dim procArray() As System.Diagnostics.Process
procArray =
System.Diagnostics.Process.GetProcessesByName(Syst em.Diagnostics.Process.GetCurrentProcess.ProcessNa me)
If procArray.Length 1 Then
Dim thisProcHandle As Int32
thisProcHandle =
System.Diagnostics.Process.GetCurrentProcess.MainW indowHandle.ToInt32
For Each proc As System.Diagnostics.Process In procArray
Try
If Not proc.MainWindowHandle.ToInt32 = thisProcHandle Then
If IsIconic(proc.MainWindowHandle) Then
ShowWindow(proc.MainWindowHandle, 9)
End If
SetForegroundWindow(proc.MainWindowHandle)
End If
Catch
End Try
Next
Exit Sub
End If
Dim mainForm As New Form1
Application.Run(mainForm)
End Sub

in vb2003 for single instance apps. VB2005 has that covered for what
I'm doing in the project properties.

Mutex just came up when I wanted to run 2 instances of an app and have
one know it was A and the other B. I'm counting on nobody starting a
3rd or it too would become B which really wouldn't cause much of a
problem. I just want to know which app processed what data and separate
logs.

Anyway, thanks for the info.

Chris Mullins [MVP] wrote:
"cj" <cj@nospam.nospamwrote:
>I take it a mutex is like a synclock but visible outside the program?

Yea, that's a pretty good working definition.

There are some differences, but at the end of the day they're both
True/False flags for "Busy / Not Busy".

The Monitor (the thing that underlies the SyncLock) has some nice methods on
it like TryEnter and PulseAll that make it easy to use in some situtations.
It also has the handy VB keyword SyncLock that deals with the Try/Finally
semantics required to use a Monitor in an Exception safe way.

On the other hand, the Mutex is also a WaitHandle, and therefore works with
all of that infrastructure (WaitOne, WaitAll, WaitAny, etc), which makes it
easy to use in some situations. For example, you can register a WaitHandle
with the CLR threadpool, and get a callback when the WaitHandle is set.
There are some scenarios where this behavior is very handy.

Personally, I tend to use:
- Monitor (Synclock) for almost all of my locking.
- ManualResetEvents for almost all of my Waiting ("Is this done yet? Call me
back when it's done.")
- Mutex for ensuring an application is "single instance".
Jun 1 '07 #3

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

Similar topics

1
by: JennaS | last post by:
Hi. I'm new to multithreading applications in .NET. What exactly is the difference between using synclock on a variable or using mutex.waitone/mutex.releasemutex There is too much stuff out there on...
4
by: Ram | last post by:
Hey, I have about 5 Shared variables, that I want to be locked for access when they are written to. I'v read in the MSDN about the SyncLock statement, and I'm prety sure that's what I'm looking...
12
by: Keith Langer | last post by:
I have some questions about whether synclock is necessary in a few different scenarios: 1) I have a Queue class which is shared between two threads. Thread 1 pushes objects onto the queue and...
4
by: fred | last post by:
If I have multiple threads running a Sub as below then a number of threads can be held up at the SyncLock. When it becomes free which thread goes first. Is it just by chance which thread goes first...
4
by: Jeff Stewart | last post by:
Specifically, I don't understand the parameter that Synclock accepts. How is a reference type a lockable entity? What -is- a reference type? Is it a number? Is it a value at a specific memory...
1
by: fred | last post by:
I have a VB application that is using MS Access as its database. To avoid connection delays the application creates one connection to the database at start-up and maintains that single connection...
7
by: Chris Dunaway | last post by:
Suppose I have several threads that need to access the same object. ThreadA successfully acquires the lock using SyncLock. ThreadB attempts to acquire the lock and blocks and then ThreadC attempts...
2
by: Spam Catcher | last post by:
Hi all, I'm hosting a remoting service in IIS. I have a function which I only want one concurrent access at a time. Will this prevent multiple users from accessing the function at a paritcular...
2
by: HONOREDANCESTOR | last post by:
I have a buffer that needs to be locked sometimes, because 2 processes update it. So I made the buffer into a class and whenever there is code that affects it, I sandwich the code between ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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

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.