Click here to Skip to main content
15,669,039 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to know if there is a simple way to fire the below block of code by declaring using an event in my program. I want the below block of code to execute ONLY ONCE and IMMEDIATELY, whenever and each time during a cycle of my loop, a boolean called FireBlockofCode evaluates to true.

I understand I may have to use a delegate but I am not quite sure how.

A big thanks to all you wonderful gurus!

What I have tried:

Console.WriteLine("starting a process");
z = z log(x);
Updated 4-Jan-22 11:36am

The simplest way is to make the boolean a property instead of field, and to execute your "immediate code" in the property setter.
Share this answer
Admin BTA 4-Jan-22 14:25pm    
I am not very familiar with properties and property setter. Is there an easy way to code this?
OriginalGriff 4-Jan-22 14:33pm    
You are kidding, right?
You can't write this on your own:
private int _MyProperty;
public int MyProperty
   get { return _MyProperty; }
      _MyProperty = value;
That's very, very basic C# code ...
Admin BTA 4-Jan-22 16:11pm    
No I am not kidding. This code will be running at a rate of thousands if not millions of times a second. So the implementation will matter in terms of efficiency. I am not sure how the compiler will process your particular code.
OriginalGriff 5-Jan-22 2:01am    
If you are really "I am not very familiar with properties and property setter" then you shouldn't be at all involved in high volume, high efficiency apps - you don't have the basics down solidly so what makes you think you are cope with the volume of data you seem to be expecting?
I'd strongly suggest that you learn the language and framework pretty thoroughly before you go any further with this - you are setting yourself up for a major fail here if you don't.
Admin BTA 5-Jan-22 7:59am    
Maybe you don't know the language as well as you think you do.
The property-approach Griff suggested is one step in your journey. But there is more to it.

You did not tell us anything about the "Process" that you claim gets started; it by itself will take time and resources that may matter a lot. If it is an actual Windows Process, you can forget it right away; Windows can't handle such avalanche of processes. If it is something inside your app, involving a lot of objects, some graphics, whatever, you'd better tell us much more about the application domain.

And whatever it is, you most certainly can't do it without any threading or timers: your urgent code sleeps for one second, hence blocking the only thread that seems to exist in your current approach. And you can't have thousands or even millions of threads at the same time either, even if all of them are sleeping.

If your problem is solvable at all, it is bound to happen along these lines:
1. have a chronological job list;
2. When you start a process, log it (your Console.WriteLine) and somehow add a "job" to the joblist (sorted!); the job consists of executing
z = z log(x);  // not sure what you intend, this would not compile!

at a time corresponding to DateTime.Now.AddSeconds(1);
3. have a timer that periodically (say once every msec) checks the job list to see whether something should be done.

Several remarks are due:
1. You didn't tell anything about how the processes interact; if they pass data one to another, you probably/certainly need some synchronization means.
2. Windows timing isn't very accurate. Example: Thread.Sleep(x) really generates a delay that most of the time equals at least x msec (not exactly x msec, and occasionally shorter!).
3. If
isn't a swift operation, you may have to come up with a threading scheme that spreads the work over a number of threads (a few or tens, no more), and then again (1) will apply here too.
4. And finally, I can't ignore the impression what you are aiming for is well over your head.

Share this answer
Admin BTA 4-Jan-22 17:50pm    
No windows are involved. Everything is procedural. No graphics, etc.

Details of the loop are not necessary, except that the loop cannot be impeded. No data passing, except if you consider the boolean a communication with whatever is outside of the loop.

Loop runs block at several hundred thousand iterations per second, so I am timing various implementations, including one involving an event, which I have little experience with.

I am not sure how the compiler processes the event but it be timed. Still no code examples of how to implement event-- my question was clear enough. Warmest Regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900