简体   繁体   English

通知ActionBlock完成到另一个class

[英]Notify the ActionBlock is completed to another class

I have written a method to implement TPL ActionBlock , which will do a function to find the XPath of the element I am Posting to the block.我编写了一个方法来实现 TPL ActionBlock ,它将执行 function 来找到我要发布到块的元素的 XPath。 I am sending the element from a real-time application (whenever the user finds an element Post it to the block).我正在从实时应用程序发送元素(每当用户找到一个元素时将其发布到块中)。 Now, I want to check the block is completed or not when I call a save button from another class.现在,当我从另一个 class 调用保存按钮时,我想检查块是否完成。 The logic is if the ActionBlok is completed when we click the save button element will save with some save logic, otherwise show a message box not yet ready.逻辑是当我们单击保存按钮元素时,如果ActionBlok完成,将使用一些保存逻辑进行保存,否则显示尚未准备好的消息框。 For the first time, this idea is working, but from the second element onwards the Actionblock is not accepting any.这是第一次,这个想法是有效的,但从第二个元素开始, Actionblock不再接受任何元素。 I am using block.Complete() and block.Completion.Wait() for the save check.我正在使用block.Complete()block.Completion.Wait()进行保存检查。 Now I can show I am calling the code/logic.现在我可以证明我正在调用代码/逻辑。

  1. Once create an element, post it ActionBlock创建元素后,将其发布到ActionBlock
    Class1第一类
......
jobQ.Enqueue(familarizedElement);
......
  1. Inside the ActionBlock ActionBlock
    Class2 2 类
public class TPLCommonDataFlow
{
    private ActionBlock<Element> _jobs;
    public static bool TPLFlowStatus = false;
    public static string JobStatus = string.Empty;
    public TPLCommonDataFlow()
    {
        var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions()
        {
            MaxDegreeOfParallelism = 2,
        };

        _jobs = new ActionBlock<Element>((job) =>
        {
            Thread.Sleep(5);
            File.WriteAllText("C:\\Temp\\A.txt", "Started" + _jobs.InputCount.ToString());
            JobStatus = "started";
            Familiarization.FindAndUpdateXPath(job);
            File.WriteAllText("C:\\Temp\\A.txt", "Finished");
        }, executionDataflowBlockOptions);
        _jobs.Complete();


        //Wait for all messages to propagate through the network
        _jobs.Completion.Wait();

        //Checking all jobs are completed or not, if completed changing the boo, value.
        if (_jobs.InputCount == 0)
        {
            TPLFlowStatus = true;
            JobStatus = "stoped";
        }
    }
}
  1. For the save I am checking the TPLFlowStatus boolean为了保存,我正在检查 TPLFlowStatus boolean
    Class3三级
if (class2.TPLFlowStatus == true)
{
    //Save Logic
}
else
{
    //Message Box showing Not Ready
}

Now what I want is to check each time the job is completed or not for each element in the save logic.现在我想要的是在每次作业完成时检查保存逻辑中的每个元素是否完成。 If the block having two elements in Queue and one is finished, then the MessageBox needs to popup once the save button pressed.如果Queue中有两个元素的块和一个已完成,则MessageBox需要在按下保存按钮后弹出。 If all completed in the block, then need to go to the save logic.如果在block中全部完成,那么需要go来保存逻辑。

The issue your seeing is that when a block is completed it will no longer accept new messages, you've told the block your done sending messages.您看到的问题是,当一个块完成后,它将不再接受新消息,您已经告诉该块您已完成发送消息。 To start sending a new batch of messages you can either keep the block alive by not calling complete and tracking the batch completion another way or you can just reset the block.要开始发送新的一批消息,您可以通过不调用完成来保持块活动并以另一种方式跟踪批处理完成,或者您可以重置块。 A simple worker like this might be what you're looking for:像这样的简单工人可能是您正在寻找的:

public class DataflowWorker
{
    private ActionBlock<string> _jobs;

    private void BuildJobsHandler()
    {
        _jobs = new ActionBlock<string>(x => Console.WriteLine(x) /*Do your work in the action block*/);
    }

    public async Task Enque(string element)
    {
        await _jobs.SendAsync(element);
    }

    public async Task CompleteJobsAndSave()
    {
        _jobs.Complete();
        await _jobs.Completion;
        //Save data when complete
        BuildJobsHandler();
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM