Code Monkey home page Code Monkey logo

Comments (3)

dmachaj avatar dmachaj commented on August 18, 2024 2

This example program does not initialize the MTA in the process. The background tasks therefore run outside the MTA and the "you didn't initialize COM" error is raised.

The call to foo works around this issue because the implementation of StorageFolder::GetFolderFromPathAsync has a side effect of initializing the MTA in the process.

I think the appropriate fix would be to have your code initialize the MTA before it tries to use it. One way that can be done is by calling CoIncrementMTAUsage. Simply call that before the call to bar and the crash is avoided.

from cppwinrt.

heroboy avatar heroboy commented on August 18, 2024

Thank you.
I had never think apartment is a property of process.
I tested:

std::thread{ []() {
	winrt::init_apartment();
	::Sleep(10000);
} }.detach();

std::thread{ []() {
	auto type = winrt::impl::get_apartment_type();
	printf("%d\n",type.first);
} }.join();

I think once a thread is initialized as MTA. All thread not initialized will be considered as MTA.

from cppwinrt.

heroboy avatar heroboy commented on August 18, 2024

But I find that initialized with single threaded will also make the ContextCallback success.

void test()
{
	winrt::com_ptr<IContextCallback> ctx;
	CoGetObjectContext(IID_IContextCallback, ctx.put_void());
	assert(ctx);

	TrySubmitThreadpoolCallback([](PTP_CALLBACK_INSTANCE pci, PVOID p) {
		
		auto x = winrt::impl::get_apartment_type();
		winrt::com_ptr<IContextCallback> ctx;
		*ctx.put_void() = p;
		winrt::init_apartment(winrt::apartment_type::single_threaded);
		ComCallData data;
		HRESULT hr = ctx->ContextCallback([](ComCallData*) {
			return S_OK;
		}, &data, IID_ICallbackWithNoReentrancyToApplicationSTA, 5, nullptr);
		printf("[%d]hr=%d\n", GetCurrentThreadId(), hr);

	}, ctx.detach(), 0);
}

from cppwinrt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.