简体   繁体   English

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

[英]How to asynchronously write CD/DVD using IMAPI?

I have been told to write a software to burn a CD synchronously/asynchronously as per user choice. 我被告知要根据用户的选择编写一个软件来同步/异步刻录CD。 I am using IMAPIv2 with C# for the project, and it does not provide the functionality explicitly to write the data asynchronously. 我正在将IMAPIv2与C#一起用于该项目,并且它不提供显式异步写入数据的功能。

In order to design the functionality, I have researched online resources, but in vain. 为了设计功能,我研究了在线资源,但徒劳无功。

Can someone explain what Synchronous/Asynchronous I/O is, in terms of burning an image on a disc? 有人可以在刻录光盘上的映像方面解释什么是同步/异步I / O吗?

Any help is appreciated. 任何帮助表示赞赏。

IMAPI does not provide in-build class/method to write data asynchronously. IMAPI不提供内置类/方法来异步写入数据。 But it is designed a way that it is possible with any technology that supports asynchronous programming. 但是,它是一种支持异步编程的技术。 The one you are using (C# as you mentioned in comments) does support it. 您正在使用的那个(您在注释中提到的C#)确实支持它。

IMAPI exposes interfaces those report status for progress and actions. IMAPI向接口公开那些报告状态的进度和操作。 All you need to do is use the threading to run the activity asynchronously; 您需要做的就是使用线程异步运行活动。 this will free up your UI and you can perform other activities. 这将释放您的UI,您可以执行其他活动。 Then, you may subscribe for the events those will report the status to you. 然后,您可以订阅那些将向您报告状态的事件。

Refer this project on CodeProject which uses BackgroundWorker for the same: 请在使用BackgroundWorker CodeProject上参考以下项目:

Multithreading 多线程

Burning or formatting media can take some time, so we do not want to perform these actions on the main UI thread. 刻录或格式化媒体可能需要一些时间,因此我们不想在主UI线程上执行这些操作。 I use the BackgroundWorker class to handle the multithreading of these lengthy tasks. 我使用BackgroundWorker类来处理这些冗长的任务的多线程。 The BackgroundWorker class allows you to set values within the thread and then call the ReportProgress method which fires a ProgressChanged event in the calling thread. BackgroundWorker类允许您在线程内设置值,然后调用ReportProgress方法,该方法将在调用线程中触发ProgressChanged事件。 When you are finished with your worker thread, it fires the RunWorkerCompleted event to notify the calling thread that it is finished. 完成工作线程后,它将触发RunWorkerCompleted事件以通知调用线程它已完成。

Following are DoWork and Update events: 以下是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); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#应用程序中没有IMAPI的情况下检测CD / DVD驱动器中的媒体类型? - How to detect media type in a CD/DVD drive without IMAPI in a C# application? 如何判断光驱(不是光盘)是CD还是DVD驱动器? - How can I tell if an optical drive (not the disc) is a CD or DVD drive? 如何通过序列号获取CD / DVD-ROM驱动器号 - How to get CD/DVD-ROM Drive Letter by Serial Number 如何获取 CD / DVD 驱动器上文件更改的通知? - How to get notifications for File Changes on CD / DVD drive? 在 Windows 10 上,如何使用 shell 执行动词“SendTo”以编程方式从 C# 将一组文件发送到 CD/DVD 刻录机 - On Windows 10, how to send programmatically from C# a set of files to the CD/DVD recorder using the shell execution verb "SendTo" 空白DVD /蓝光光盘(IMAPI)上的可用空间 - Available space on blank dvd / blu-ray discs (IMAPI) 如何使用 IMAPI2 检索和设置刻录速度? - How to retrieve and set burn speed using IMAPI2? 使文件无法在CD / DVD上访问 - Make file inaccessible on cd/dvd 如何发现USB存储设备和可写CD / DVD驱动器(C#) - How to discover USB storage devices and writable CD/DVD drives (C#) 如何在没有插入磁盘介质的情况下以编程方式区分CD驱动器和DVD驱动器? - How to programmatically differentiate between cd drive and dvd drive without a disk media inserted?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM