none
如何编写这个create_task().then函数?(C++) RRS feed

  • 问题

  • create_task(captureMgr->InitializeAsync());
    captureMgr->StartPreviewAsync();
    capturePreview->Source = captureMgr;

    我需要执行第一行,在InitializeAsync()执行完以后执行第二三行。
    请问怎样将后两行代码放到create_task的then里?

    MediaCapture^ captureMgr = ref new MediaCapture();//这是captureMgr的定义

    capturePreview是一个XAML控件。


    2014年5月6日 5:21

答案

  • 试试看这个?

    MediaCapture^ captureMgr = ref new MediaCapture();
    create_task(captureMgr->InitializeAsync()).then(
    	[=]()
    {
    	captureMgr->StartPreviewAsync();
    });


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年5月8日 11:41
    版主
  • 把外部参数传入内部的意思

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年5月8日 11:44
    版主
  • 我发现不用等于号也可以,只要在中括号里填写需要引用的变量名就可以了。
    2014年5月10日 6:45

全部回复

  • 你可以用如下方式:

    create_task(captureMgr->InitializeAsync()).then() {captureMgr->StartPreviewAsync();}
    --James

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年5月7日 8:58
    版主
  • 又忘说了,语言是C++,而且then的函数似乎,应该放在then的小括号里面,你的解答编译错误。还有,如果代码是这样:

    MediaCapture^ captureMgr = ref new MediaCapture();
    create_task(captureMgr->InitializeAsync()).then(
    	[]()
    {
    	captureMgr->StartPreviewAsync();
    });
    那么会出现错误:
    1	IntelliSense:  封闭函数局部变量不能在 lambda 体中引用,除非其位于捕获列表中
    
    请问怎么办,能不能把captureMgr作为参数传过去?如果能,怎么写?

    2014年5月8日 11:33
  • 试试看这个?

    MediaCapture^ captureMgr = ref new MediaCapture();
    create_task(captureMgr->InitializeAsync()).then(
    	[=]()
    {
    	captureMgr->StartPreviewAsync();
    });


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年5月8日 11:41
    版主
  • 中括号里的等于号是什么意思?
    2014年5月8日 11:43
  • 把外部参数传入内部的意思

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年5月8日 11:44
    版主
  • 还有,出现警告:

    警告	1	warning C4451: “实验::Page4::Button1Click::<lambda_90df7cf370e9c6a66e1ca2ae1f4e50de>::captureMgr”: 在此上下文中使用 ref 类“Windows::Media::Capture::MediaCapture”会导致上下文中的对象的封送处理无效

    运行时出现错误:

    First-chance exception at 0x77641D4D in 实验.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x0441D0AC. HRESULT:0xC00D36BB 请求的操作中出现错误。
    
    WinRT 信息: 请求的操作中出现错误。
    
    堆栈跟踪:
    [外部代码]
    
    如有适用于此异常的处理程序,该程序便可安全地继续运行。

    2014年5月8日 11:46
  • 我发现不用等于号也可以,只要在中括号里填写需要引用的变量名就可以了。
    2014年5月10日 6:45