TXMLDocumentAsyncLoadState (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example uses the AsyncLoadState property. To illustrate this, an XML file is loaded in a separate thread. The AsyncLoadState property is read from the main thread until the XML file is fully parsed.

Code

class TThreadLoad : public TThread {
private:
	void __fastcall Execute() {
		const HRESULT iniResult = CoInitializeEx(0, COINIT_MULTITHREADED);

		if (!((iniResult == S_OK) || (iniResult == S_FALSE))) {
			printf("Failed to initialize COM library.\n");
			return;
		}

		try {
			document->ParseOptions = document->ParseOptions +
				(TParseOptions() << poAsyncLoad);
			document->LoadFromFile(srcPath);
		}
		catch (Exception &ex) {
			printf("Exception inside thread.\n%ls\n", ex.Message);
		}
		CoUninitialize();
	}

public:
	_di_IXMLDocument document;

	TThreadLoad(bool CreateSuspended) : TThread(CreateSuspended) {
		document = interface_cast<Xmlintf::IXMLDocument>
			(new TXMLDocument(NULL));
		printf("TThreadLoad Ctor.\n");
	}
};

void Test_AsyncLoad() {
	TThreadLoad *thread = new TThreadLoad(true);

	thread->Priority = tpIdle;
	thread->Start(); // The thread will start loading the document from file.

	try {
		const int StateComplete = 4;
		int state;
		while ((state = thread->document->AsyncLoadState) != StateComplete) {
			printf("Async load state is %d\n", state);
		}
	}
	catch (Exception &ex) {
		printf("Exception outside thread.\n%ls\n", ex.Message);
	}
}

Uses