Solved

Remoting (client/server): Writing to remote object ,(synclock)

Posted on 2008-09-30
17
503 Views
Last Modified: 2013-11-07
Hi!

I have a question regarding writing to a remote object.

I have a server that receives information from a client, this client execute several rows of code, each row sends data to the server and each call is to the same function on the server.

So on the client side I have this.
Private sub SendData
  Server.WriteData = 10
  Server.WriteData = 11
  Server.WriteData = 12
  Server.WriteData = 13
  Server.WriteData = 14
  Server.WriteData = 15
  Server.WriteData = 16
and so on&.
End sub

So we call the same function several times but each write takes some time, the WriteData function on the server is pretty large.

So my question is, is this going to work? Or must I have a synclock on the server function or does the remoting stuff takes care of that?
0
Comment
Question by:AWestEng
  • 11
  • 6
17 Comments
 
LVL 11

Expert Comment

by:AkisC
ID: 22612547
I did not undestood your issue well.
Maybe you can get some idea from my answer at http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23692399.html
I use this approach with success for 40 concurrent users.
0
 
LVL 1

Author Comment

by:AWestEng
ID: 22612804
ok, let's take this instead,
If I have a object in the server 1 and server 2 and server 3 is both trying to write to this object in server 1, do I need a syncklock on the object for server 1 then?
 
0
 
LVL 11

Expert Comment

by:AkisC
ID: 22613240
Synclock I usually use in multitreading environment of a single app, and needs a lot of attention for deadlocks
if you are confortable with Synclock then it is ok. Synclock tell the code to treat each request separatelly. Its hard to debug thought (especially in remoting)

I'd recoment using Queue function of vb.net so you put your .WriteData to first-come-first-served

0
 
LVL 1

Author Comment

by:AWestEng
ID: 22613495
oki. have you any example on the Queue function.
 
0
 
LVL 11

Accepted Solution

by:
AkisC earned 500 total points
ID: 22614504
Just some guide lines. It is pretty simple I beleive
The Queue works like FIFO system. First In First Out. The item added in Queue is , first get out from Queue.

Enqueue (add) items in Queue
-and-
Dequeue (remove from Queue )
-or-
Peek the item from Queue.

'//import the Systems.Collections namespace by using the Imports System.Collections statement
'//
Dim MyQueue As New Queue(4) 'say 4 here. if adding an object to the queue causes the size to exceed, then the queue is automatically resized to allow the object to be added.

When a job comes to .writedate istead of calling your sub or function add it to the queue
MyQueue.Enqueue(MyJob)
Now what MyJob variable should have?... all the parameters so you can call your sub or function
Lets say you define MyJob as string and make it something like string1 & "," & string2 (so you can split it later using ",")
{Have some var to trigger when an item is entered into the queue}

Items of the queue are accessed one at a time with the Dequeue method which removes an element from the queue:
MyString = MyQueue.Dequeue
Now you have your string... split it and call your .writedata sub or function

Also you may use the Peek method which doesn't remove the element: (not usefull for your job...)
MyString = MyQueue.Peek

You can loop through the items of the queue using GetEnumerator method.
Dim QueueEnumerator As IEnumerator= MyQueue.GetEnumerator()
While QueueEnumerator.MoveNext
 Dim ObjectType As System.Type
 ObjectType = QueueEnumerator.Current.GetType
 msgbox("object name =" & ObjectType.Name)
 msgbox(" string representation =" & QueueEnumerator.Current.ToString & "")
End While
'// you may use it in a timer to see if all items have been processed

The Clone() method can be used to create a copy of a queue:
Dim MyOtherQueue As Queue()
MyOtherQueue = MyQueue.Clone()

You must "play around" to grasp the idea.

Have fun coding... ;-)

0
 
LVL 1

Author Comment

by:AWestEng
ID: 22617239
nice thx n8,
 I'm not sure how this works in remoting and calling the same function at the same time. I register the interface that all the other servers and clients can communicate with. all ok there..
Iif 3 other servers calls the same function via the registerd interface how does the remote stuff works then. Does it Queue it up self or must I do that? and if I not Queue it up what happens when 3 other servers calls the main server at the same time?
 
0
 
LVL 11

Expert Comment

by:AkisC
ID: 22618184
Let's see the statment
Dim X as Integer
X = 1
X = X + 1  'a programmer thinks that this line is a single operation
From the computers perpective is not: It is: 1.-Move the value x into a register, 2.-move the value 1 into another register, 3.-add the values to a third register and at the end 4.-copy the added value to memory address of value x

Now if a single user is performing the task, everything is ok.
If 2 users (or more) are requesting the same task from the same function the computer could stop the operation at any step point of the 1 user to serve the 2 user, so there is a good chance that values will mess up

Your question "at the same time?" has different meaning for humans and different for computers.
If your function takes 1/10 of a second to complete then it is very difficult for 2 users to hit a button at the same time, e.g. from 2 workstations.
-but- if your function takes 2sec to complete then it is more likely that 2 users can hit a button at the same time and then the computer has to stop the first request at some point to give the 2nd request priority. So public or global variables will probably mess up

As for the queue
You create it. As I wrote before, instead of calling the main function (at the remote computer) call a function lets call it addJob(parameters as the params of your main function)
At this point add the job to the queue. (Enqueue)
When finished proceess the queue with Dequeue
 MyString = MyQueue.Dequeue

finally loop the queue using GetEnumerator method to see if all jobs are finished
I can understand your question "Does it Queue it up self?" the answer is no:you fill it up AND the answer is also yes: when a second request comes the queue is the same memory (Just like when to computer send a print job to a printer)

I hope I did not confuse you!








 
0
 
LVL 1

Author Comment

by:AWestEng
ID: 22618452
I really appreciate that you helping me. thx man. :)
I read in a blog about remoting that if you use singelton server that the remoting suff will queue up the request that other server/clients calls, but this is not true then?
sorry for all the question, I just trying to get the hole picture. :)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:AkisC
ID: 22621833
I have no knowledge of 'singelton server'
I shall create a Queue example and post it back. It will take me some time because I'm working on a project with dead end sunday

0
 
LVL 1

Author Comment

by:AWestEng
ID: 22644816
thx man, that would be great if you could
0
 
LVL 1

Author Comment

by:AWestEng
ID: 22694488
I tink the Queue methods will be best as you said..
I'll try to make some code,
0
 
LVL 11

Expert Comment

by:AkisC
ID: 22695485
I have not forgot the queue project...
It will take some time. -but- I haven't finished my dead line (past already) of the proj Im working on.
If you make some code, post it so I can take a look
0
 
LVL 1

Author Comment

by:AWestEng
ID: 22706095
AkisC:
do you know anything about process, I really need someone that knows what he's talking about.
http://www.experts-exchange.com/Programming/Languages/.NET/Q_23810766.html
 
0
 
LVL 1

Author Comment

by:AWestEng
ID: 22826355
I'm going to start working on this next week,, so then I will probably need some help. :)
0
 
LVL 1

Author Comment

by:AWestEng
ID: 22866227
Hi AkisC!
Now it's time for the Queue stuff, So if you have any time left I really would appreciate if you have any examples,
 
0
 
LVL 1

Author Comment

by:AWestEng
ID: 22871351
Hi
I have a question, the remoting stuff I use will add to the queue and another thread will dequeue so I must protect the queue in some way or don't I? so the remoting is not adding at the same time as the other thread is removing,
 
I found this code
http://www.codeguru.com/columns/dotnet/article.php/c4609
 

 

''' <summary> 

''' Add items to the queue. 

''' </summary> 

Public Sub Enqueue() 

    Dim counter As Integer = 0 

    

    Monitor.Enter(Me._queue) 

    While counter < MAX_QUEUE_SIZE 

        ' Wait while the queue is busy 

        Monitor.Wait(Me._queue) 

        

        ' Add one item to the queue 

        Console.WriteLine("Adding item {0} to queue", counter.ToString()) 

        Me._queue.Enqueue(System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)) 

        

        ' Release the queue 

        Monitor.Pulse(Me._queue) 

    End While 

    Monitor.[Exit](Me._queue) 

End Sub 
 

''' <summary> 

''' Remove items from the queue. 

''' </summary> 

Public Sub Dequeue() 

    Monitor.Enter(Me._queue) 

    

    ' Release the queue because we just locked it 

    ' setting up the region lock 

    Monitor.Pulse(Me._queue) 

    

    ' Wait while the queue is busy 

    ' and exit on the timeout when the first thread stopped 

    While Monitor.Wait(Me._queue, 500) 

        ' Remove the first item and display it 

        Dim counter As Integer = CInt(Me._queue.Dequeue()) 

        Console.WriteLine("Removed item {0} from queue", counter.ToString()) 

        

        ' Release the queue 

        Monitor.Pulse(Me._queue) 

    End While 

    Monitor.[Exit](Me._queue) 

End Sub 

Open in new window

0
 
LVL 1

Author Comment

by:AWestEng
ID: 22979334
Hi AkisC!
Do have time to check the code?  :)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now