• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 511
  • Last Modified:

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

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
AWestEng
Asked:
AWestEng
  • 11
  • 6
1 Solution
 
AkisCCommented:
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
 
AWestEngAuthor Commented:
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
 
AkisCCommented:
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
AWestEngAuthor Commented:
oki. have you any example on the Queue function.
 
0
 
AkisCCommented:
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
 
AWestEngAuthor Commented:
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
 
AkisCCommented:
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
 
AWestEngAuthor Commented:
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
 
AkisCCommented:
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
 
AWestEngAuthor Commented:
thx man, that would be great if you could
0
 
AWestEngAuthor Commented:
I tink the Queue methods will be best as you said..
I'll try to make some code,
0
 
AkisCCommented:
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
 
AWestEngAuthor Commented:
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
 
AWestEngAuthor Commented:
I'm going to start working on this next week,, so then I will probably need some help. :)
0
 
AWestEngAuthor Commented:
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
 
AWestEngAuthor Commented:
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
 
AWestEngAuthor Commented:
Hi AkisC!
Do have time to check the code?  :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now