开发者

queuing async operations?

I'm using a c# COM library that provides an async mechanism (actually it is PDFCreator, imposed by my customer). Converting a doc file to a PDF file requires two things :

  • a PrintFile method that starts the conversion
  • a ready event to indicates the conversion is finished

[Edit] : There is not the Begin/End methods of the async patterns. I have to start the printfile method, and wait for the event to be raised

this is OK when I have only one file to convert, but I'm struggling with b开发者_开发技巧atch conversion of several files because of this async pattern.

How can I manage a queue of files to convert ?

I'd like my application (a WPF one) simply "enqueue" a file to convert.... Then my worker object dequeue files one by one, ensuring only one conversion at a time can occurs.

Please note that I'm using C# 4, and I'd like (because I'm learning) to use Tasks if it's an advantage.


If the PDFCreator exposes an APM based interface (i.e BeginFoo / EndFoo methods) then you can wrap those methods within a Task:

Task<int> bytesRead = Task<int>.Factory.FromAsync( 
    stream.BeginRead, stream.EndRead, buffer, 0, buffer.Length, null);  

there is an article here: http://blogs.msdn.com/b/pfxteam/archive/2009/06/09/9716439.aspx which eplains this in more detail.

Once you have a group of tasks can you start them queue them via a custom TaskScheduler or the default one: (http://msdn.microsoft.com/en-us/library/dd997402.aspx), it looks as though you can use the default task scheduler in your scenario.

task.Start()

Note: if you want to have these tasks notify the UI when they finish, you could use a continuation, (http://msdn.microsoft.com/en-us/library/dd270696.aspx) which can notify the UI when the task has finished. Note to marshall back to the UI thread you'll need to use a a SynchronizationContext or Dispatch.BeginInvoke.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜