473,403 Members | 2,222 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,403 software developers and data experts.

Thread sync problem

Hi

I'm currently writing a VB.NET service that uses external
Dlls, like this:

Private Declare Function ReadSettings Lib "mydll.dll" ()

Now, how can I protect this function (ReadSettings) from
being called more then once a time?
I tried to use

System.Threading.Monitor.Enter(ReadSettings)
ReadSettings()
System.Threading.Monitor.Exit(ReadSettings)

but this caused a reference null exception. So, can
anyone tell me how I can protect external functions?
Thanks
Gordon
Nov 20 '05 #1
2 4012
Gordon Knote wrote:
Hi

I'm currently writing a VB.NET service that uses external
Dlls, like this:

Private Declare Function ReadSettings Lib "mydll.dll" ()

Now, how can I protect this function (ReadSettings) from
being called more then once a time?
I tried to use

System.Threading.Monitor.Enter(ReadSettings)
ReadSettings()
System.Threading.Monitor.Exit(ReadSettings)

but this caused a reference null exception. So, can
anyone tell me how I can protect external functions?
Thanks
Gordon


You need to use an object in your Monitor.Enter. So you would do
something like this...

Public Class Resource

Private Object lockObject = New Object()
Public Function ThisIsTheFunction() As Integer
Try
Monitor.Enter(lockObject)
ReadSettings()
Catch (Exception ex)
' Handle your exceptions
Finally
Monitor.Exit(lockObject)
End Try
End Function

End Class

You can also use SyncLock as well. It is really just a shorthand for
using the Monitor object and the Try/Catch/Finally stuff ;)

HTH,
Tom Shelton

Nov 20 '05 #2
> You can also use SyncLock as well. It is really just a shorthand for
using the Monitor object and the Try/Catch/Finally stuff ;)
Which would become...

' ///
Public Class Resource

Private lockObject As New Object

Public Function ThisIsTheFunction() As Integer

SyncLock lockObject
ReadSettings()
End SyncLock

End Function

End Class
' ///

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
"Tom Shelton" <to*@mtogden.com> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl... Gordon Knote wrote:
Hi

I'm currently writing a VB.NET service that uses external
Dlls, like this:

Private Declare Function ReadSettings Lib "mydll.dll" ()

Now, how can I protect this function (ReadSettings) from
being called more then once a time?
I tried to use

System.Threading.Monitor.Enter(ReadSettings)
ReadSettings()
System.Threading.Monitor.Exit(ReadSettings)

but this caused a reference null exception. So, can
anyone tell me how I can protect external functions?
Thanks
Gordon


You need to use an object in your Monitor.Enter. So you would do
something like this...

Public Class Resource

Private Object lockObject = New Object()
Public Function ThisIsTheFunction() As Integer
Try
Monitor.Enter(lockObject)
ReadSettings()
Catch (Exception ex)
' Handle your exceptions
Finally
Monitor.Exit(lockObject)
End Try
End Function

End Class

You can also use SyncLock as well. It is really just a shorthand for
using the Monitor object and the Try/Catch/Finally stuff ;)

HTH,
Tom Shelton

Nov 20 '05 #3

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

Similar topics

13
by: William Stacey | last post by:
Using the following code sample: public byte Get() { // <= Possible to switch Here?? lock(syncLock) { //Do something in Get(). } }
11
by: Nenad Dobrilovic | last post by:
Hi, I have big problem. I made generic form which can be rendered, and as a result of that action, I get System.Windows.Forms.Form object. Rendering must be done in GUI thread (one which has...
2
by: TOMERDR | last post by:
Hi, I have a sync manager class which span a thread which run a tight loop(Sync function) I would like that after the loop is finished,every 5 minutes the thread will run again I want also...
5
by: jzlondon | last post by:
Hi, I have a question that I wonder if someone might be able to help me with... I have an application which handles real-time financial data from a third party source. The data comes in via...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
8
by: mtsweep | last post by:
Hi, I started a background thread to preform some time intensive tasks for a GUI frontend. This background thread uses a C++ object which requires a windows message loop so I started one in it...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
0
by: samuel84 | last post by:
Hi All, I have a SynchEngine Class which has a Synch() method and throws a StatusChange event at various stages of sync, i would like to run this sync process in a new thread as i need to...
7
by: Boki | last post by:
Multi-thread read/write to a single file. I have two processing threads, thread A and thread B; and I called my queue as Q. Thread A will feed data into Q by user input, the timing is random....
7
by: reyesflaco | last post by:
I am developing an application using asp.net 2.0. I created all my business objects in my app_code folder. As of right now, all my classes are public. In my aspx pages, I am declaring the class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.