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

how to create class that only 1 thread can access it in MT app.

hi.
i have multi thread application in vb.net

is there a way NET support, so i can mark the class , to be access
only for 1 thread each time ?

if there is , small sytax sample will help

//what i need to add , so only 1 thread per time can access this class in
MultiThread app.
public MySharedClass

End Class
Nov 20 '05 #1
4 1536
No, you will have to manually synchronize all code that needs to be
protected in this way.

"Daylor" <ro******@hotmail.com> wrote in message
news:bu**********@news2.netvision.net.il...
hi.
i have multi thread application in vb.net

is there a way NET support, so i can mark the class , to be access
only for 1 thread each time ?

if there is , small sytax sample will help

//what i need to add , so only 1 thread per time can access this class in
MultiThread app.
public MySharedClass

End Class

Nov 20 '05 #2
Daylor,
As Marina suggested you need to manually do this.

I normally do something like:
public Class MySharedClass
Private Readonly m_padlock As New Object()

Private m_value1, m_value2 As String

Public Sub New(ByVal value1 As String, ByVal value2 As String)
m_value1 = value1
m_value2 = value2
End Sub

Public Sub Method1(ByVal value1 As String)
SyncLock m_padlock
If m_value1 <> value1 Then
m_value1 = value1
End If
End SyncLock
End Sub

Public Sub Method2(ByVal value2 As String)
SyncLock m_padlock
If m_value2 <> value2 Then
m_value2 = value2
End If
End SyncLock
End Sub
End Class
The m_padlock variable acts as the gate to the class, each method tries to
lock the gate (SyncLock) while the thread is "inside" the class.

For further details see:

http://msdn.microsoft.com/library/de...isualBasic.asp
Hope this helps
Jay

"Daylor" <ro******@hotmail.com> wrote in message
news:bu**********@news2.netvision.net.il... hi.
i have multi thread application in vb.net

is there a way NET support, so i can mark the class , to be access
only for 1 thread each time ?

if there is , small sytax sample will help

//what i need to add , so only 1 thread per time can access this class in
MultiThread app.
public MySharedClass

End Class

Nov 20 '05 #3
well the problem is more than that ,
what if my shared class has form in it.

i read that the thread that create the form, should call his methods.

does NET has simple way to call async way to do that ?
(i think when you use delegate , a thread from thread pool is used , in my
case i need
the thread that created the form to call his methods.)

its seems to be very basic issure, cause in alot of multi thread
applications
you have forms.

what is the way to call form in multithread app ?
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Daylor,
As Marina suggested you need to manually do this.

I normally do something like:
public Class MySharedClass
Private Readonly m_padlock As New Object()

Private m_value1, m_value2 As String

Public Sub New(ByVal value1 As String, ByVal value2 As String)
m_value1 = value1
m_value2 = value2
End Sub

Public Sub Method1(ByVal value1 As String)
SyncLock m_padlock
If m_value1 <> value1 Then
m_value1 = value1
End If
End SyncLock
End Sub

Public Sub Method2(ByVal value2 As String)
SyncLock m_padlock
If m_value2 <> value2 Then
m_value2 = value2
End If
End SyncLock
End Sub
End Class


The m_padlock variable acts as the gate to the class, each method tries to
lock the gate (SyncLock) while the thread is "inside" the class.

For further details see:

http://msdn.microsoft.com/library/de...isualBasic.asp

Hope this helps
Jay

"Daylor" <ro******@hotmail.com> wrote in message
news:bu**********@news2.netvision.net.il...
hi.
i have multi thread application in vb.net

is there a way NET support, so i can mark the class , to be access
only for 1 thread each time ?

if there is , small sytax sample will help

//what i need to add , so only 1 thread per time can access this class in MultiThread app.
public MySharedClass

End Class


Nov 20 '05 #4
Daylor,
well the problem is more than that ,
what if my shared class has form in it. As some would say: This changes every thing! ;-)

For a form, to ensure that a "method" is executed on the thread that created
the Win32 handle, you need to use the Control.Invoke method. Closely related
to this method is Control.BeginInvoke, Control.EndInvoke, and
Control.InvokeRequired.
does NET has simple way to call async way to do that ?
(i think when you use delegate , a thread from thread pool is used , in my
case i need
the thread that created the form to call his methods.) Only if you did BeginInvoke on the delegate, remember the thread where the
async method is running needs to use Control.Invoke to call into your Form,
however the Form (Main Thread) is free to call into any object, even if that
object can also be modified by the thread where the async method is running,
then you need to use the SyncLock statement I gave.

If that made sense.

I thought this was all covered in the link I gave...

Hope this helps
Jay

"Daylor" <ro******@hotmail.com> wrote in message
news:bu**********@news2.netvision.net.il... well the problem is more than that ,
what if my shared class has form in it.

i read that the thread that create the form, should call his methods.

does NET has simple way to call async way to do that ?
(i think when you use delegate , a thread from thread pool is used , in my
case i need
the thread that created the form to call his methods.)

its seems to be very basic issure, cause in alot of multi thread
applications
you have forms.

what is the way to call form in multithread app ?
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Daylor,
As Marina suggested you need to manually do this.

I normally do something like:
public Class MySharedClass


Private Readonly m_padlock As New Object()

Private m_value1, m_value2 As String

Public Sub New(ByVal value1 As String, ByVal value2 As String)
m_value1 = value1
m_value2 = value2
End Sub

Public Sub Method1(ByVal value1 As String)
SyncLock m_padlock
If m_value1 <> value1 Then
m_value1 = value1
End If
End SyncLock
End Sub

Public Sub Method2(ByVal value2 As String)
SyncLock m_padlock
If m_value2 <> value2 Then
m_value2 = value2
End If
End SyncLock
End Sub
End Class


The m_padlock variable acts as the gate to the class, each method tries to
lock the gate (SyncLock) while the thread is "inside" the class.

For further details see:

http://msdn.microsoft.com/library/de...isualBasic.asp


Hope this helps
Jay

"Daylor" <ro******@hotmail.com> wrote in message
news:bu**********@news2.netvision.net.il...
hi.
i have multi thread application in vb.net

is there a way NET support, so i can mark the class , to be access
only for 1 thread each time ?

if there is , small sytax sample will help

//what i need to add , so only 1 thread per time can access this class

in MultiThread app.
public MySharedClass

End Class



Nov 20 '05 #5

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

Similar topics

4
by: Daylor | last post by:
in win32 process , when u create new process,u have new main thread. i know,appDomain r logical procces,that exists in 1 win32 process. the q: is there way to create second appDomain (the...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
23
by: YinTat | last post by:
Hi, I learned C++ recently and I made a string class. A code example is this: class CString { public: inline CString(const char *rhs) { m_size = strlen(rhs);
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
0
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
2
by: Paul Wu | last post by:
From what I understand, in ASP.NET, each HTTP requests is serviced by a separate thread. So if my code uses a static Class with shared members and properties, I can manage concurrent access by using...
9
by: Marc Miller | last post by:
Hi all, I have 2 dev. machines, the 1st is Win 2000 with .NET 7.0 and the 2nd is XP Pro with .NET 2003. My Web Server is Win 2000 Server with IIS 5.0. I can create a new project on my test...
1
by: flowstudioLA | last post by:
I have a template class object that I use as a mesaging queue between threads. I use it as a static object that I initialize like so: foo.h class foo{ static LFQueue<const char*,100lfqMyQueue;...
3
by: andreas.zetterstrom | last post by:
I'm implementing some different c++ classes which I want to be thread safe. All these classes contain lists or arrays of some kind. I'm using protected sections to make them thread safe. The...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.