如何使用IMAPI异步写入CD / DVD?

问题描述 投票:1回答:1

我被告知要编写一个软件来根据用户选择同步/异步刻录CD。我正在为项目使用带有C#的IMAPIv2,并且它没有显式地提供异步写入数据的功能。

为了设计功能,我研究了在线资源,但是徒劳无功。

在刻录光盘上的图像方面,有人能解释一下同步/异步I / O是什么吗?

任何帮助表示赞赏。

c# asynchronous dvd-burning imapi
1个回答
0
投票

IMAPI不提供内置类/方法来异步写入数据。但它的设计方式可以使用任何支持异步编程的技术。你正在使用的那个(你在评论中提到的C#)确实支持它。

IMAPI公开那些报告进度和操作状态的接口。您需要做的就是使用线程异步运行活动;这将释放您的UI,您可以执行其他活动。然后,您可以订阅那些将向您报告状态的事件。

请参考CodeProject上使用thisBackgroundWorker项目:

多线程

刻录或格式化媒体可能需要一些时间,因此我们不希望在主UI线程上执行这些操作。我使用BackgroundWorker类来处理这些冗长任务的多线程。 BackgroundWorker类允许您在线程中设置值,然后调用ReportProgress方法,该方法在调用线程中触发ProgressChanged事件。当您完成工作线程时,它会触发RunWorkerCompleted事件以通知调用线程它已完成。

以下是DoWorkUpdate事件:

private void backgroundBurnWorker_DoWork(object sender, DoWorkEventArgs e)
{
    MsftDiscRecorder2 discRecorder = null;
    MsftDiscFormat2Data discFormatData = null;

   try
    {
        //
        // Create and initialize the IDiscRecorder2 object
        //
        discRecorder = new MsftDiscRecorder2();
        var burnData = (BurnData)e.Argument;
        discRecorder.InitializeDiscRecorder(burnData.uniqueRecorderId);

       //
        // Create and initialize the IDiscFormat2Data
        //
        discFormatData = new MsftDiscFormat2Data
            {
                Recorder = discRecorder,
                ClientName = ClientName,
                ForceMediaToBeClosed = _closeMedia
            };

       //
        // Set the verification level
        //
        var burnVerification = (IBurnVerification)discFormatData;
        burnVerification.BurnVerificationLevel = _verificationLevel;

       //
        // Check if media is blank, (for RW media)
        //
        object[] multisessionInterfaces = null;
        if (!discFormatData.MediaHeuristicallyBlank)
        {
            multisessionInterfaces = discFormatData.MultisessionInterfaces;
        }

       //
        // Create the file system
        //
        IStream fileSystem;
        if (!CreateMediaFileSystem(discRecorder, multisessionInterfaces, out fileSystem))
        {
            e.Result = -1;
            return;
        }

       //
        // add the Update event handler
        //
        discFormatData.Update += discFormatData_Update;

       //
        // Write the data here
        //
        try
        {
            discFormatData.Write(fileSystem);
            e.Result = 0;
        }
        catch (COMException ex)
        {
            e.Result = ex.ErrorCode;
            MessageBox.Show(ex.Message, "IDiscFormat2Data.Write failed",
                MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
        finally
        {
            if (fileSystem != null)
            {
                Marshal.FinalReleaseComObject(fileSystem);
            }
        }

       //
        // remove the Update event handler
        //
        discFormatData.Update -= discFormatData_Update;

       if (_ejectMedia)
        {
            discRecorder.EjectMedia();
        }
    }
    catch (COMException exception)
    {
        //
        // If anything happens during the format, show the message
        //
        MessageBox.Show(exception.Message);
        e.Result = exception.ErrorCode;
    }
    finally
    {
        if (discRecorder != null)
        {
            Marshal.ReleaseComObject(discRecorder);
        }

       if (discFormatData != null)
        {
            Marshal.ReleaseComObject(discFormatData);
        }
    }
}

void discFormatData_Update([In, MarshalAs(UnmanagedType.IDispatch)] object sender,
                           [In, MarshalAs(UnmanagedType.IDispatch)] objectprogress)
{
    //
    // Check if we've cancelled
    //
    if (backgroundBurnWorker.CancellationPending)
    {
        var format2Data = (IDiscFormat2Data)sender;
        format2Data.CancelWrite();
        return;
    }

   var eventArgs = (IDiscFormat2DataEventArgs)progress;

   _burnData.task = BURN_MEDIA_TASK.BURN_MEDIA_TASK_WRITING;

   // IDiscFormat2DataEventArgs Interface
    _burnData.elapsedTime = eventArgs.ElapsedTime;
    _burnData.remainingTime = eventArgs.RemainingTime;
    _burnData.totalTime = eventArgs.TotalTime;

   // IWriteEngine2EventArgs Interface
    _burnData.currentAction = eventArgs.CurrentAction;
    _burnData.startLba = eventArgs.StartLba;
    _burnData.sectorCount = eventArgs.SectorCount;
    _burnData.lastReadLba = eventArgs.LastReadLba;
    _burnData.lastWrittenLba = eventArgs.LastWrittenLba;
    _burnData.totalSystemBuffer = eventArgs.TotalSystemBuffer;
    _burnData.usedSystemBuffer = eventArgs.UsedSystemBuffer;
    _burnData.freeSystemBuffer = eventArgs.FreeSystemBuffer;

   //
    // Report back to the UI
    //
    backgroundBurnWorker.ReportProgress(0, _burnData);
}

© www.soinside.com 2019 - 2024. All rights reserved.