Question

I've built a little camera capture daemon which captures a sequence of images from an attached DSLR using Canon's EDSDK and Wayne Hartman's C# wrapper.

Capturing works great and very reliably when I call takePhotograph() from a test button click handler on the form itself. However, when I try to call takePhotograph() from socketServer_MessageReceived(), it's very unreliable and frequently causes the app stop responding. After tracing the call stack, it looks like the entire order of calls gets jumbled up, ultimately causing the EDSDK to hang up when calling EdsDownload() prematurely (before all images have been captured).

I'm coming from a non-multithreaded environment (Flex/ActionScript), and have a hunch I'm just doing something elementarily wrong related to my handlers.

Here's the gist of my code:

    private SocketServer socketServer;

    private void initSocketServer()
    {
        socketServer = new SocketServer();
        socketServer.Start( Convert.ToInt16( serverPortField.Text ) );
        socketServer.MessageReceived += new EventHandler<SocketEventArgs>( socketServer_MessageReceived );
    }

    private void socketServer_MessageReceived ( object sender , SocketEventArgs e )
    {
        Console.WriteLine( "[CaptureDaemon] socketServer_MessageReceived() >> " + (String)e.Data );

        var serializer = new JavaScriptSerializer();
        serializer.RegisterConverters( new[] { new DynamicJsonConverter() } );

        dynamic obj = serializer.Deserialize( (String)e.Data , typeof( object ) );

        if ( (String)obj.destinationID != "captureDaemon" )
            return;

        switch ( (String)obj.messageID )
        {
            case "capture":
                takePhotograph( obj.body.successiveShotDelay , obj.body.successiveShots );
                break;
        }
    }

    private void testCaptureButton_Click ( object sender , EventArgs e )
    {
        takePhotograph( 500 , 4 ) );
    }
Was it helpful?

Solution

Did you try to wrap the takePhotograph with the Form.Invoke (this.Invoke)? There's a chance that if it works from the GUI then it will also work when you force a correct thread for the call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top