0

工作流程

每次观看文件触发事件时,我都需要执行Show created file name

WatchFilesActivity : NativeActivity

    protected override void Execute(NativeActivityContext context)
    {
        var fileSystemWatcher = new FileSystemWatcher(context.GetValue(Path));
        fileSystemWatcher.IncludeSubdirectories = context.GetValue(IncludeSubdirectories);
        fileSystemWatcher.Filter = context.GetValue(Filter);

        var bookmark = context.CreateBookmark(ResumeFileCreatedBookmark);
        context.GetExtension<FileSystemWatcherExtension>().Start(fileSystemWatcher, bookmark);
    }

延期

    public class FileSystemWatcherExtension : IWorkflowInstanceExtension
    {
        WorkflowInstanceProxy instance;
        Bookmark bookmark;

        public void SetInstance(WorkflowInstanceProxy instance)
        {
            this.instance = instance;
        }

        IEnumerable<object> IWorkflowInstanceExtension.GetAdditionalExtensions()
        {
            yield break;
        }

        public void Start(FileSystemWatcher fileSystemWatcher, Bookmark bookmark)
        {
            this.bookmark = bookmark;
            fileSystemWatcher.Created += new FileSystemEventHandler(FileCreated);
            fileSystemWatcher.EnableRaisingEvents = true;
        }

        void FileCreated(object sender, FileSystemEventArgs e)//When the file arrives
        {
            instance.BeginResumeBookmark(bookmark, e.FullPath, CompleteResume, null);
        }

        void CompleteResume(IAsyncResult ar)
        {
            var result = instance.EndResumeBookmark(ar);
        }
    }

这很好用,但只有一次,在此之后主机关闭。

我不能放置 WhileActivity 因为我需要处理连续的非常快速的文件创建,并且传入文件的处理时间(在这种情况下显示创建的文件名)大于文件创建速率

谢谢!

4

1 回答 1

2

对于初学者,我会将Show created file name活动设为 Watch files活动的子活动,以便它可以控制它的执行。接下来,我将添加一个书签恢复回调,以便Watch files活动可以对回调中的Show created file name活动做出反应并安排时间 。

或者,您可能希望使用 BookmarkOptions.MultipleResume 创建书签,以便您可以处理任意数量的文件事件,并且仅在您想要这样做时继续。

public class WatchFilesActivity : NativeActivity
{
    public Activity Child {get; set;}

    protected override void Execute(NativeActivityContext context)
    {
        var fileSystemWatcher = new FileSystemWatcher(context.GetValue(Path));
        fileSystemWatcher.IncludeSubdirectories = context.GetValue(IncludeSubdirectories);
        fileSystemWatcher.Filter = context.GetValue(Filter);

        var bookmark = context.CreateBookmark(ResumeFileCreatedBookmark, OnFileCreated, BookmarkOptions.MultipleResume);
        context.GetExtension<FileSystemWatcherExtension>().Start(fileSystemWatcher, bookmark);
    }

    protected void OnFileCreated(NativeActivityContext context, Bookmark bookmark, object value)
    {
        var fileName = (string)value
        context.ScheduleActivity(Child);
    }
}

注意:在记事本中输入的代码,因此可能会出现拼写错误。

如果您需要将文件名传递给子活动,您可以使用 ActivityAction 来做到这一点。有关使用 ActivityFunc 的示例,请参见此处,该示例与 ActivityAction 类似,只是它也有返回值。

于 2011-08-19T08:16:40.910 回答