Click here to Skip to main content
15,921,295 members
Home / Discussions / C#
   

C#

 
AnswerRe: Generic Dictionary with weak reference value Pin
Simon P Stevens18-Nov-08 0:14
Simon P Stevens18-Nov-08 0:14 
QuestionNaming Conventions Pin
AmbitiousBeginner17-Nov-08 23:29
AmbitiousBeginner17-Nov-08 23:29 
AnswerRe: Naming Conventions Pin
dan!sh 17-Nov-08 23:39
professional dan!sh 17-Nov-08 23:39 
AnswerRe: Naming Conventions Pin
Manas Bhardwaj17-Nov-08 23:39
professionalManas Bhardwaj17-Nov-08 23:39 
AnswerRe: Naming Conventions Pin
Simon P Stevens17-Nov-08 23:50
Simon P Stevens17-Nov-08 23:50 
AnswerRe: Naming Conventions Pin
Eddy Vluggen18-Nov-08 2:39
professionalEddy Vluggen18-Nov-08 2:39 
AnswerRe: Naming Conventions Pin
Nagy Vilmos18-Nov-08 2:43
professionalNagy Vilmos18-Nov-08 2:43 
QuestionExternal libraries / Threading / Critical sections Pin
J4amieC17-Nov-08 23:25
J4amieC17-Nov-08 23:25 
I am writing a class that wraps and helps me deal with calls to an external c++ library from C#. My class is used in a webservice environment, so must be sensitive to the multi-threaded nature of web requests, however I dont think the c++ library was ever written with this in mind so I must be careful how I call it.

To simpify things the C++ library has a few methods I must call

Init - must be called only once, the first time my class is used
SendData - used to start a communication
ReceiveEvent - must be called on a timer with a short interval, used for receiving events back from the external library (and I use it to raise events in my class)
ShutDown - must be called once before the application exits

Now this structure I think lends itself to a Singleton which takes care of the Init/Shutdown, and a factory method to create my instance of a "Call". Feel free to suggest another approach if you think im wrong there.

The problem im having is in ensuring that "Init" has been called before any other thread manages to call SendData. eg

Thread1 calls SingletonObject.Instance.CreateCall() - this starts the processof calling "Init"
Thread2 calls SingletonObject.Instance.CreateCall() - however as Init has not yet completed this fails

I have tried all sorts of variations of lock() and Mutex, but im not seeing the behaviour I expect. Here is some cut-down code which will hopefully demonstrate what im attempting (Methods prefixxed "X25" are the external calls). Any help appreciated as always.

public class Xot
{
    #region Static
    static Xot()
    {
    }
    private static readonly Xot instance = new Xot();

    public static Xot Instance { get { return instance; } }
    #endregion


    private Dictionary<int,> calls;
    private bool initialised = false;
    private Timer waitTimer;
    
    private Xot()
    {
        calls = new Dictionary<int,>();
        waitTimer = new Timer(this.ReadEvents, null, Timeout.Infinite, Timeout.Infinite);

        // Initialise the library, no other thread should be able to call 
       //Instance.CreateCall until this has completed
       Console.WriteLine("Initialising Xot");
        if (!this.initialised)
        {
            lock (Xot.Instance) // this doesnt seem to do it nor does lock(this)
            {
                int rc = X25.HsX25DllInit(
                               "10.80.105.102",
                               0,
                               1024,
                               2,
                               1,
                               4095,
                               1);
                if (rc == X25.HSX25DLL_RC_OK)
                {
                    initialised = true;
                    this.waitTimer.Change(0, 15);
                }
                else
                {
                    throw new Exception(String.Format("Failed to initialised Xot. RC={0}", rc));
                }
            }
        }
    }   
   
     // Will a finaliser work here?
    ~Xot()
    {
        if (this.initialised)
        {
            Console.WriteLine("Shutting Down Xot");
            int rc = X25.HsX25DllShutDown(0);
            if (rc != X25.HSX25DLL_RC_OK)
            {
                throw new Exception(String.Format("Failed to shut down X25 [RC={0}]", rc));
            }
            this.waitTimer.Change(Timeout.Infinite, Timeout.Infinite);
            this.initialised = false;
        }
    }

    private void CheckInit()
    {
        if (!this.initialised)
            throw new InvalidOperationException("Not initialised");
    }

    public XotCall CreateCall()
    {
        this.CheckInit();
        XotCall call = new XotCall(this);
        return call;
    }
}

AnswerRe: External libraries / Threading / Critical sections Pin
Simon P Stevens18-Nov-08 0:08
Simon P Stevens18-Nov-08 0:08 
QuestionBinary Tree - Next Highest Value Pin
Map Idea17-Nov-08 23:12
Map Idea17-Nov-08 23:12 
AnswerRe: Binary Tree - Next Highest Value Pin
Simon P Stevens17-Nov-08 23:44
Simon P Stevens17-Nov-08 23:44 
Questionrestart a console application Pin
caradri17-Nov-08 23:05
caradri17-Nov-08 23:05 
AnswerRe: restart a console application Pin
Simon P Stevens17-Nov-08 23:33
Simon P Stevens17-Nov-08 23:33 
QuestionFlip Pin
vinay_K17-Nov-08 23:00
vinay_K17-Nov-08 23:00 
AnswerRe: Flip Pin
dan!sh 17-Nov-08 23:06
professional dan!sh 17-Nov-08 23:06 
GeneralRe: Flip Pin
vinay_K17-Nov-08 23:28
vinay_K17-Nov-08 23:28 
GeneralRe: Flip Pin
dan!sh 18-Nov-08 0:12
professional dan!sh 18-Nov-08 0:12 
AnswerRe: Flip Pin
sph3rex17-Nov-08 23:06
sph3rex17-Nov-08 23:06 
QuestionHow to register COM component and use in c# window application Pin
veereshIndia17-Nov-08 22:38
veereshIndia17-Nov-08 22:38 
AnswerRe: How to register COM component and use in c# window application Pin
JoeSharp17-Nov-08 22:57
JoeSharp17-Nov-08 22:57 
GeneralRe: How to register COM component and use in c# window application Pin
veereshIndia17-Nov-08 23:26
veereshIndia17-Nov-08 23:26 
GeneralRe: How to register COM component and use in c# window application Pin
JoeSharp17-Nov-08 23:34
JoeSharp17-Nov-08 23:34 
GeneralRe: How to register COM component and use in c# window application Pin
veereshIndia17-Nov-08 23:44
veereshIndia17-Nov-08 23:44 
AnswerRe: How to register COM component and use in c# window application Pin
Simon P Stevens17-Nov-08 23:02
Simon P Stevens17-Nov-08 23:02 
AnswerCP IGNORE: Crossposted about a dozen times... Pin
Dave Kreskowiak18-Nov-08 2:09
mveDave Kreskowiak18-Nov-08 2:09 

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.