Click here to Skip to main content
15,890,947 members
Home / Discussions / C#
   

C#

 
AnswerRe: Array List to Application Settings. Pin
Subin Mavunkal25-Aug-11 20:49
Subin Mavunkal25-Aug-11 20:49 
Questionsocket class in vista uac Pin
Aisha sharma24-Aug-11 22:08
Aisha sharma24-Aug-11 22:08 
AnswerRe: socket class in vista uac Pin
Dave Kreskowiak25-Aug-11 9:57
mveDave Kreskowiak25-Aug-11 9:57 
GeneralRe: socket class in vista uac Pin
Aisha sharma25-Aug-11 21:37
Aisha sharma25-Aug-11 21:37 
GeneralRe: socket class in vista uac Pin
Cheongwadae26-Aug-11 17:19
Cheongwadae26-Aug-11 17:19 
QuestionPredicate Conditions for Find, First...Methods Pin
share_holder24-Aug-11 21:46
share_holder24-Aug-11 21:46 
AnswerRe: Predicate Conditions for Find, First...Methods PinPopular
Wayne Gaylard24-Aug-11 22:21
professionalWayne Gaylard24-Aug-11 22:21 
QuestionLooking for FIFO Lock in .net 2.0? [modified] Pin
Firo Atrum Ventus24-Aug-11 19:08
Firo Atrum Ventus24-Aug-11 19:08 
Hi guys, I'm currently working on a ship management system. The problem lies in the main bridge between the ships and the server. When there is more than 700 ships, the bridge doesn't send the message to the server in FIFO order, causing the display in the application to appear as jumping back and forth.

After a bit of debugging (and searching of course), I found that the root of the problem is within lock(){} which process data within a list just before sending it to server. There are 10 Threads managing this List, All of them does the same job (The previous version which use just 1 list doesn't have this error but it's very slow and it has a huge memory usage). This is because lock release thread according priority, causing the latest ship data to be sent before the older ship data. I do realize that .net doesn't have any FIFO Lock and I do realize the existence of ConcurrentQueue<t>[^] and other thread safe collection, but this application must run on .net 2.0 (dunno why). So is there any solution to this problem? (It's not necessarily a FIFO Collection, a FIFO Lock will do)

So far, I tried replacing lock with this class:
C#
using System.Threading;

public sealed class QueuedLock
{
    private object innerLock;
    private volatile int ticketsCount = 0;
    private volatile int ticketToRide = 1;

    public QueuedLock()
    {
        innerLock = new Object();
    }

    public void Enter()
    {
        int myTicket = Interlocked.Increment(ref ticketsCount);
        Monitor.Enter(innerLock);
        while (true)
        {

            if (myTicket == ticketToRide)
            {
                return;
            }
            else
            {
                Monitor.Wait(innerLock);
            }
        }
    }

    public void Exit()
    {
        Interlocked.Increment(ref ticketToRide);
        Monitor.PulseAll(innerLock);
        Monitor.Exit(innerLock);
    }
}

But it doesn't help. In fact, it increases the error percentage from 20% (Simulated with a generator that generate 100.000 ship data) to 23% WTF | :WTF: [Edit:Miscalculation on error count Blush | :O ]

So, does anyone here have an idea on how to solve this? Any ideas will be greatly appreciated.
Excuse me for my improper grammar and typos.
It's because English is my primary language, not my first language.

My first languages are C# and Java.
VB, ASP, JS, PHP and SQL are my second language.
Indonesian came as my third language.
My fourth language? I'm still creating it, I'll let you know when it's done! Big Grin | :-D

modified on Thursday, August 25, 2011 4:56 AM

AnswerRe: Is there any FIFO collection in .net 2.0? Pin
DaveAuld24-Aug-11 21:26
professionalDaveAuld24-Aug-11 21:26 
GeneralRe: Is there any FIFO collection in .net 2.0 Pin
Firo Atrum Ventus24-Aug-11 21:55
Firo Atrum Ventus24-Aug-11 21:55 
GeneralRe: Is there any FIFO collection in .net 2.0? Pin
DaveAuld24-Aug-11 22:07
professionalDaveAuld24-Aug-11 22:07 
GeneralRe: Is there any FIFO collection in .net 2.0? Pin
Firo Atrum Ventus24-Aug-11 22:11
Firo Atrum Ventus24-Aug-11 22:11 
GeneralRe: Is there any FIFO collection in .net 2.0? Pin
DaveAuld24-Aug-11 22:39
professionalDaveAuld24-Aug-11 22:39 
GeneralRe: Is there any FIFO collection in .net 2.0? Pin
Firo Atrum Ventus24-Aug-11 22:52
Firo Atrum Ventus24-Aug-11 22:52 
AnswerRe: Looking for FIFO Lock in .net 2.0? Pin
Luc Pattyn25-Aug-11 2:03
sitebuilderLuc Pattyn25-Aug-11 2:03 
QuestionHow to register .ocx file from code Pin
SajjadZare24-Aug-11 17:11
SajjadZare24-Aug-11 17:11 
AnswerRe: How to register .ocx file from code Pin
Hari Om Prakash Sharma24-Aug-11 17:31
Hari Om Prakash Sharma24-Aug-11 17:31 
GeneralRe: How to register .ocx file from code Pin
SajjadZare24-Aug-11 18:47
SajjadZare24-Aug-11 18:47 
AnswerRe: How to register .ocx file from code Pin
Shameel24-Aug-11 21:41
professionalShameel24-Aug-11 21:41 
QuestionStrange windows forms control bug? Pin
venomation24-Aug-11 13:21
venomation24-Aug-11 13:21 
AnswerRe: Strange windows forms control bug? Pin
simplefolk25-Aug-11 3:45
simplefolk25-Aug-11 3:45 
Questionthread safety Pin
CodingYoshi24-Aug-11 11:52
CodingYoshi24-Aug-11 11:52 
AnswerRe: thread safety Pin
SledgeHammer0124-Aug-11 12:10
SledgeHammer0124-Aug-11 12:10 
GeneralRe: thread safety Pin
CodingYoshi24-Aug-11 16:24
CodingYoshi24-Aug-11 16:24 
AnswerRe: thread safety PinPopular
Luc Pattyn24-Aug-11 12:10
sitebuilderLuc Pattyn24-Aug-11 12:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.