472,780 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 1498
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.