Simon Harriyott

Starting the Distributed Transaction Coordinator from C#

The DTC is needed when a Winforms app is calling more than one stored procedure as part of a transaction. As the DTC is just a service, the standard ServiceController class can be used. A reference to System.ServiceProcess must be added first, and the code is as follows:

// Find the Distributed Transaction Controller service

ServiceController service = new ServiceController("MSDTC");

if (service.Status != ServiceControllerStatus.Running)

{

    try

    {

        service.Start();

        // Wait for up to 10 seconds for the service to start

        TimeSpan timeSpan = TimeSpan.FromSeconds(10);

        service.WaitForStatus(ServiceControllerStatus.Running, timeSpan);

    }

    catch (TimeoutException exc)

    {

        MessageBox.Show(exc.Message);

    }

}



[Tags: ]
1 November 2006