none
Concept thread RRS feed

  • Question

  • Hello,

    I have a small or aaybe a big problem.

    Current state:

    public class A
    {
        int SendMessage(string);
    }

       MainThread

    private A CurrentA;
    
    public void Step1()
    {
       int ret = CurrentA.SendMessage("Step1");
    }
    
    public void Step5()
    {
       int ret =  CurrentA.SendMessage("Step5");
    }
    
    
    public void Step9()
    {
       for ( int i=0; i<100; i++ )
       {
           int ret =  CurrentA.SendMessage("Step5" + i.ToString());
       }
    }

    Step1 and Step5 is synchronous and waits until the function returns.

    Now, should be:

       Step9() I have to call and process asynchronously.

    How can I solve this? It could happen that Step1 is called prematurely before Step9 is completely processed.

    Do I have to call the function SendMessage for everyone in one thread?

    Start the thread for each call?

    Process is

    Cycle (Loop)
          Step1 --- Step5  ----Step9(Thread, because needs time)

    Every time Step1, Step5, Step9, then Step1, Step5, Step9

    Can be Step1, Step5, Step1, then Step9,  that is wrong.

    Thanks for your help and a good sample.

    Greetings Markus


       

         


    Thursday, January 16, 2020 6:10 PM

All replies

  • If those steps need to happen in order, then they aren't really asynchronous, are they?

    It sounds to me like your whole cycle/loop should be a single thread.

        pubic void Cycle()
        {
            while( !stop )
            {
                 Step1();
                 Step5();
                 Step9();
            }
        }

    Doesn't that solve the problem?


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Thursday, January 16, 2020 6:18 PM
  • Hello,

       pubic void Cycle()
        {
            while( !stop )
            {
                 Step1();
                 Step5();
                 Step9();
            }
        }


    that’s solve not the problem.

       pubic void Cycle()
        {
            while( !stop )
            {
                 Step1();
                 Step5();
    }  like this |    Step9(); parallel
      }

    After Step 5 I call Step9, but the whole process should go to Step1
    From Step5 to Step1 needs time. That the reason for Step9.

    If I make it all step by step, needs too much time.
    The idea is, Step1 and 5 synchron Step9 parallel.

    But what‘s happen, when Step5 to Step1 is faster as Step9

    Any idea?

    Greetings Markus
    Thursday, January 16, 2020 8:51 PM
  • So you want to run step 1 and step 5 in order and at the same time have step 9 such that step 9 may finish before or after either step 1 or 5 (that is how async works by the way).

    "It could happen that Step1 is called prematurely before Step9 is completely processed."

    Or do you mean that Step 9 should run first and then step 1 and 5. Or perhaps you mean that you want to run step 1 and then step 5 and then you want to start step 9 and wait for it to finish and then loop back around and start step 1 again?

    EDIT: By the way none of these calls are async so if you could provide a more correct sample of how you're calling these methods sync/async it would be beneficial.


    Michael Taylor http://www.michaeltaylorp3.net


    Thursday, January 16, 2020 9:54 PM
    Moderator

  • After Step 5 I call Step9, but the whole process should go to Step1
    From Step5 to Step1 needs time. That the reason for Step9.
    If I make it all step by step, needs too much time.
    The idea is, Step1 and 5 synchron Step9 parallel.

    Hi Markus,

    Thank you for posting here.

    It would be nice if you could describe the problem in more detail.

    I don't quite understand the meaning of these sentences.

    Looking forward to your reply.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 17, 2020 9:03 AM
  • Hello All,

    Current state.

    Works well for Step1 to 8.

    For each step I send a message and get an answer back. Synchronous.
    Now a new step 9 is added, which has to report a lot of data.
    When the step 9 is finished, the idea is to start a new thread or Task.Run().
    Meanwhile the product go outside, during which time the details should be reported.
    public class A
    {
        string Product;
        int PosX;
        int PosY;
        inr State;
    
        int SendMessage(string step)
        {
           string wholeString = $"{Product}{step}{State}{PosX}{PosY}";
    
           int ret = ExternSystem.CallStep(wholeString);  // this is a DLL function from a third party.
        }
    }

    Process steps 2020

    CurrentA.SendMessage("Step1");

    CurrentA.SendMessage("Step2");

    CurrentA.SendMessage("Step3");

    CurrentA.SendMessage("Step4");

    ………….
    ………...

    CurrentA.SendMessage("Step8");

    How can I solve this?

    If step1 for the next product is faster than the details of step9, step1 must wait otherwise there is an error.
    Step1 to step 9 is an endless process until it is stopped.
    Please help me and thanks in advance. Is a cycle, I want to make a lot of products.


    Hope clear now. If not please ask me.

    Greetings Markus

    Monday, January 20, 2020 11:33 AM
  • You keep mentioning task/Start.Run but you don't seem to have any of that in anything you've posted. Clarifying why you think this important or what you mean by it would be useful.

    You also didn't clarify whether the earlier steps can repeat again while step 9 is running. If they can then either all this code is reentrant and they are async or this is just one really big function that is called on demand so you can have any # of these "functions" running at the same time.

    //Run, wait for each to complete Step1(); ... Step8(); //Run, don't wait to complete var task = Step9Async(); //Do whatever else you want to do while you're waiting for //work to complete, it is important that at some point //you do check the task results

    //Check results task.Wait();

    Task Step9Async () { //Do async work }

    The assumption here is that this function needs to wait until all the steps are completed. Somebody has to verify step9 completes. If this is truly a fire and forget process then you have to build into your actual step code logic to determine if something failed.

    After this "function" completes you can do whatever you want because steps 1-9 have been run. You could repeat the call or whatever. Again, it is completely dependent upon how the above code is triggered to begin with.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, January 20, 2020 4:22 PM
    Moderator
  • Hello Michael,

    It's a process machine. It produces a product step by step.

    The product leaves the machine, although the software should process step 9.

    Step8 

       calls Step9

          Must not be blocking, because the conveyor belt should drive the product out of the machine, so the next product can be run in.

    That means I have to check, at step 1, the step 9 is ready, otherwise wait, maybe after a timeout message.

    CurrentA.SendMessage("Step1");

    Can you be clear on that? Unfortunately, I didn't get it.

    //Run, don't wait to complete
    var task = Step9Async();
    How do I get the Step9 asynchronous and how can I check at step1 is the step 9 finished?
    Many thanks for tips in advance.

    Greetings Markus

    Monday, January 20, 2020 5:07 PM
  • You have to make Step9 an async method. Once it is then calling it does not block the caller.

    "How do I get the Step9 asynchronous and how can I check at step1 is the step 9 finished?"

    This doesn't make sense to me. You say it needs to be async but if Step1 cannot run again until Step9 is complete then it isn't async. Async means you can continue on and don't wait for the response. Who calls the code that runs steps 1-9? What happens after step 9 runs? What happens while step 9 is running. What happens when this function is called again while steps 1-8 or 9 are running?


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, January 20, 2020 5:40 PM
    Moderator
  • Hello Michael,

     

    >What happens after step 9 runs?

    The product goes outside the machine (mechanical process that takes some time)

    >What happens while step 9 is running.

    The same, the product goes outside the machine (mechanical process that takes some time)

     

    >What happens when this function is called again while steps 1-8 or 9 are running?

    It is an assembly line like this https://www.asm-smt.com/de/produkte/printing-solutions/

    That's exactly what needs to be locked. How do I do it correctly?

     

    Step 1 to 8 is step by step.  à  synchronous

     

    Step9 makes two

    • Send details to server, database
    • Moves the product out of the machine, lets the next product into the machine.

     

    The controller is a PLC controller.

     

     

    Thanks for an example in advance.

        With best regards Markus

     

    Or like this, a whole line.  https://set-electronics.com/manufacturing/smt-production/

     

    Monday, January 20, 2020 6:06 PM
  • Hi,

    Let me first state that this is not a solution, but an explanation based on my understanding of the problem.

    The meaning of op should be that steps 1 to 8 are on the same thread, and step 9 is on another thread.

    They do not interfere with each other, but these two threads belong to a whole. Just like the picture shows.

    Markus, is my understanding correct?

    If not, please let us know below my reply so as not to cause trouble for others.

    I may not log into msdn in the next two weeks, so I had to post my understanding for others to refer to.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 21, 2020 9:30 AM
  • Hello Timon,
    That is right.
    Two threads. Step9 start after Step8 is finished.
    Step1 can only start or wait until Step9 is finished.
    Or timeout.
    Thanks for help. Do you have a good sample? If not I will close the thread.
    Greetings Markus
    Tuesday, February 11, 2020 5:06 PM
  • Hi,

    Maybe you can use Task.WaitAll Method

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 14, 2020 9:13 AM