static void publish(int domain_id, int sample_count)
    {
        // --- Create participant --- //

        /* To customize participant QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.DomainParticipant participant =
            DDS.DomainParticipantFactory.get_instance().create_participant(
                domain_id,
                DDS.DomainParticipantFactory.PARTICIPANT_QOS_DEFAULT,
                null /* listener */,
                DDS.StatusMask.STATUS_MASK_NONE);
        if (participant == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_participant error");
        }

        // --- Create publisher --- //

        DDS.PublisherQos publisher_qos = new DDS.PublisherQos();
        participant.get_default_publisher_qos(publisher_qos);


        /* If you want to change the Partition name programmatically rather than
         * using the XML, you will need to add the following lines to your code
         * and comment out the create_publisher() call bellow.
         */
        /*
         * String[] partitions = { "ABC", "foo" };
         *
         * publisher_qos.partition.name.ensure_length(2, 2);
         * publisher_qos.partition.name.from_array(partitions);
         * DDS.Publisher publisher = participant.create_publisher(
         *                      publisher_qos,
         *                      null,
         *                      DDS.StatusMask.STATUS_MASK_NONE);
         *
         */
        DDS.Publisher publisher = participant.create_publisher(
            DDS.DomainParticipant.PUBLISHER_QOS_DEFAULT,
            null,
            DDS.StatusMask.STATUS_MASK_NONE);
        if (publisher == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_publisher error");
        }

        Console.WriteLine("Setting partition to '{0}', '{1}'...",
                          publisher_qos.partition.name.get_at(0),
                          publisher_qos.partition.name.get_at(1));

        // --- Create topic --- //

        /* Register type before creating topic */
        System.String type_name = partitionsTypeSupport.get_type_name();
        try
        {
            partitionsTypeSupport.register_type(
                participant, type_name);
        }
        catch (DDS.Exception e)
        {
            Console.WriteLine("register_type error {0}", e);
            shutdown(participant);
            throw e;
        }

        /* To customize topic QoS, use
         * the configuration file USER_QOS_PROFILES.xml */
        DDS.Topic topic = participant.create_topic(
            "Example partitions",
            type_name,
            DDS.DomainParticipant.TOPIC_QOS_DEFAULT,
            null /* listener */,
            DDS.StatusMask.STATUS_MASK_NONE);
        if (topic == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_topic error");
        }

        // --- Create writer --- //

        /* In this example we set a Reliable datawriter, with Transient Local
         * durability. By default we set up these QoS settings via XML. If you
         * want to to it programmatically, use the following code, and comment out
         * the create_datawriter call bellow.
         */

        /*
         * DDS.DataWriterQos writerQos = new DDS.DataWriterQos();
         * publisher.get_default_datawriter_qos(writerQos);
         *
         * writerQos.reliability.kind = DDS.ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS;
         * writerQos.history.depth = 3;
         * writerQos.history.kind = DDS.HistoryQosPolicyKind.KEEP_LAST_HISTORY_QOS;
         * writerQos.durability.kind = DDS.DurabilityQosPolicyKind.TRANSIENT_LOCAL_DURABILITY_QOS;
         *
         * DDS.DataWriter writer = publisher.create_datawriter(
         *  topic,
         *  writerQos,
         *  null,
         *  DDS.StatusMask.STATUS_MASK_NONE);
         */

        DDS.DataWriter writer = publisher.create_datawriter(
            topic,
            DDS.Publisher.DATAWRITER_QOS_DEFAULT,
            null,
            DDS.StatusMask.STATUS_MASK_NONE);
        if (writer == null)
        {
            shutdown(participant);
            throw new ApplicationException("create_datawriter error");
        }
        partitionsDataWriter partitions_writer =
            (partitionsDataWriter)writer;

        // --- Write --- //

        /* Create data sample for writing */
        partitions instance = partitionsTypeSupport.create_data();

        if (instance == null)
        {
            shutdown(participant);
            throw new ApplicationException(
                      "partitionsTypeSupport.create_data error");
        }

        /* For a data type that has a key, if the same instance is going to be
         * written multiple times, initialize the key here
         * and register the keyed instance prior to writing */
        DDS.InstanceHandle_t instance_handle = DDS.InstanceHandle_t.HANDLE_NIL;

        /*
         * instance_handle = partitions_writer.register_instance(instance);
         */

        /* Main loop */
        const System.Int32 send_period = 1000; // 1 second

        for (int count = 0;
             (sample_count == 0) || (count < sample_count);
             ++count)
        {
            Console.WriteLine("Writing partitions, count {0}", count);

            /* Modify the data to be sent here */
            instance.x = count;

            try
            {
                partitions_writer.write(instance, ref instance_handle);
            }
            catch (DDS.Exception e)
            {
                Console.WriteLine("write error {0}", e);
            }

            if ((count + 1) % 25 == 0)
            {
                // Matches "ABC" -- name[1] here can match name[0] there,
                // as long as there is some overlapping name
                publisher_qos.partition.name.set_at(0, "zzz");
                publisher_qos.partition.name.set_at(1, "A*C");
                Console.WriteLine("Setting partition to '{0}', '{1}'...",
                                  publisher_qos.partition.name.get_at(0),
                                  publisher_qos.partition.name.get_at(1));
                publisher.set_qos(publisher_qos);
            }
            else if ((count + 1) % 20 == 0)
            {
                // Strings that are regular expressions aren't tested for
                // literal matches, so this won't match "X*Z"
                publisher_qos.partition.name.set_at(0, "X*Z");
                Console.WriteLine("Setting partition to '{0}', '{1}'...",
                                  publisher_qos.partition.name.get_at(0),
                                  publisher_qos.partition.name.get_at(1));
                publisher.set_qos(publisher_qos);
            }
            else if ((count + 1) % 15 == 0)
            {
                // Matches "ABC"
                publisher_qos.partition.name.set_at(0, "A?C");
                Console.WriteLine("Setting partition to '{0}', '{1}'...",
                                  publisher_qos.partition.name.get_at(0),
                                  publisher_qos.partition.name.get_at(1));
                publisher.set_qos(publisher_qos);
            }
            else if ((count + 1) % 10 == 0)
            {
                // Matches "ABC"
                publisher_qos.partition.name.set_at(0, "A*");
                Console.WriteLine("Setting partition to '{0}', '{1}'...",
                                  publisher_qos.partition.name.get_at(0),
                                  publisher_qos.partition.name.get_at(1));
                publisher.set_qos(publisher_qos);
            }
            else if ((count + 1) % 5 == 0)
            {
                // No literal match for "bar"
                publisher_qos.partition.name.set_at(0, "bar");
                Console.WriteLine("Setting partition to '{0}', '{1}'...",
                                  publisher_qos.partition.name.get_at(0),
                                  publisher_qos.partition.name.get_at(1));
                publisher.set_qos(publisher_qos);
            }


            System.Threading.Thread.Sleep(send_period);
        }

        /*
         * try {
         *  partitions_writer.unregister_instance(
         *      instance, ref instance_handle);
         * } catch(DDS.Exception e) {
         *  Console.WriteLine("unregister instance error: {0}", e);
         * }
         */

        // --- Shutdown --- //

        /* Delete data sample */
        try
        {
            partitionsTypeSupport.delete_data(instance);
        }
        catch (DDS.Exception e)
        {
            Console.WriteLine(
                "partitionsTypeSupport.delete_data error: {0}", e);
        }

        /* Delete all entities */
        shutdown(participant);
    }