How to start/stop a feature by calling a struct method

swift struct example
swift struct array
swift struct inheritance
swift call a struct
how to call a method in c#
swift struct vs class
swift copy struct
swift nested struct

Please see the following example.

Playground link

type runner struct{}

func (r *runner) StopProcessing() {
    // how to stop?
}

func (r *runner) StartProcessing() {

    go func() {
        for {
            fmt.Println("doing stuff")
            time.Sleep(1 * time.Second)
        }
    }()

}

As you can see I have a struct which does stuff, it's "running". It starts running when I call the run.StartProcessing() method. It then fires an endless running for{}-loop in a goroutine. Great, but I also want to be able to stop this process. And I really don't know how to achieve this. Any help is highly appreciated.

You can use a context to get timeouts and cancelation, without requiring any extra API.

type runner struct{}

func (r *runner) StartProcessing(ctx context.Context) {
    go func() {
        for {
            select {
            case <-ctx.Done():
                return
            default:
            }
            fmt.Println("doing stuff")
            time.Sleep(1 * time.Second)
        }
    }()
}

This gives you the flexibility to set a timeout, or cancel it at any time. You can also make use of existing contexts which may want to timeout or cancel sooner without your knowledge.

// normal timeout after 10 seconds
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
run.StartProcessing(ctx)

// decide to cancel early
time.Sleep(3 * time.Second)
cancel()

How to start/stop a feature by calling a struct method, It starts running when I call the run.StartProcessing() method. It then fires an endless running for{} -loop in a goroutine. Great, but I also want to be able to stop this  Method calls on structs require a managed pointer to the struct (which can be anywhere in memory - on the stack, in an argument list, in another object on the heap) the method is to be executed on as the this pointer. Method calls on reference types require an object reference.

By using a channel to signal when to break out of a goroutine. The relevant part of your code would look something like this

type runner struct {
    stop chan bool
}

func (r *runner) StopProcessing() {
    r.stop <- true
}

func (r *runner) StartProcessing() {
    r.stop = make(chan bool)
    go func() {
        for {
            fmt.Println("doing stuff")
            time.Sleep(1 * time.Second)
            select {
            case _ = <-r.stop:
                close(r.stop)
                return
            default:
            }
        }
    }()
}

You can see a full example here https://play.golang.org/p/OUn18Cprs0I

Stop Using Structs! - Commencis, Class; Anything coming from NSObject; Function; Closure So value types don't have shared state, and they have an auto-copying feature. `release` calls of POC's Struct (on the left) and Class (on the right) in a function  The way we do that is via two customizations located in the Other category: Disable auto start/stop system (method A) and Disable auto start/stop system (method B). Here’s more of a technical explanation on what they do: the first one sets the minimum voltage below which the system won’t activate to an unfeasible/unreachable value.

You might try something like this... You might not need atomic but this works.

package main

import (
    "fmt"
    "time"
    "sync/atomic"
)

var trip = int64(0)

type runner struct{}

func (r *runner) StopProcessing() {
    atomic.AddInt64(&trip, 1)
}

func (r *runner) StartProcessing() {
    go func() {
        for {
            if trip > 0 {
                break
            }
            fmt.Println("doing stuff")
            time.Sleep(1 * time.Second)
        }
    }()
}

func newRunner() *runner {
    return &runner{}
}

func main() {
    run := newRunner()
    run.StartProcessing() 

    // now wait 4 seconds and the kill the process
    time.Sleep(4 * time.Second)
    run.StopProcessing()
}

Languages, Design Methods, and Tools for Electronic System Design: , stop, show total time and wait writeTicksToDisplay(totalTime)(display) await That means, it is the calling code that stops the measurement if the StartStop button is pressed and all the shown control flow structures and value-types including structs and arrays. Completing the remaining features is an ongoing process. This will only work in C++. Functions in structs are not a feature of C. Same goes for your client.AddClient(); call this is a call for a member function, which is object oriented programming, i.e. C++. Convert your source to a .cpp file and make sure you are compiling accordingly.

High Performance Computing, The basic idea is to intercept these function calls and to execute some of them in 1.11 demonstrates the simplicity of this feature: the struct mixed_prec_exp_t wrapper when no object of that Gotcha component is within a start/stop region. Fortunately, every car with stop/start comes with another feature: a button to turn it off. This content is created and maintained by a third party, and imported onto this page to help users

Write safe and efficient C# code, New features in C# enable you to write verifiable safe code with better performance. modifier or the method calls only readonly members of the struct. properties must be read-only, including auto-implemented properties. Start thread with member function. I have a small class: class Test { public: void runMultiThread(); private: int calculate(int from, int to); } How its possible to run method calculate with two differents set of parametrs(for example calculate(0,10), calculate(11,20)) in two threads from method runMultiThread()?

Methods, A method is a code block that contains a series of statements. Methods are declared in a class, struct, or interface by specifying the access The value can be returned to the caller by value or, starting with C# 7.0, by reference. the array's contents is not necessary if the calling function passed the array  Methods (C# Programming Guide) 07/20/2015; 10 minutes to read +8; In this article. A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method.

Comments
  • Linking to code helps the community less. The playground links are nice, but the core important code should be placed in the question.
  • Possible duplicate of how to stop a goroutine
  • Beat me to it. Probably best to close the channel before the break.
  • Agreed. Updated the code for both the answer and playground. Also, I think returning out of the function is more definite than breaking out of the loop
  • That was how I did it when I was about to post my version.
  • The channel here does not need to be closed, nor should the receiver be responsible for closing it.
  • @MadWombat: closing a channel is not a cleanup action, it's a type of message. The advice to not have the receiver close a channel is generally sounds advice to avoid sending on a closed channel.
  • Using global variables to keep temporary state is generally frowned upon
  • Also, using int64 where its value will ever be 0 or 1 is not a good thing either. Although you can call StopProcessing() repeatedly and it will keep incrementing. Also, since there is a single goroutine running, no need to use atomic updates.
  • You cannot read a value concurrently, even with an atomic write. You must safely load the value, or better yet, use the builtin concurrency features of the language, like a channel.