harriyott.com

Wednesday, November 01, 2006

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: ]

2 Comments:

Anonymous said...

Thanks a lot..

Anand

December 06, 2006 5:57 AM  
Anonymous said...

Great stuff.

February 24, 2007 9:05 AM  

Post a Comment

Links to this post:

Create a Link

<< Home