Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm working with a file watcher. It only needs to perform the action after no more events have fires for like 10 seconds.
For these types of problems I use a throttle function in JavaScript and I was wondering if C# can do something similar:
var t = null;
function throttleAction(fn){
if(t != null){
window.clearTimeout(t);
}
t = window.setTimeout(function(){
t = null;
fn();
}, 10000);
}
How would I implement something like this in C#?
Use System.Timers.Timer. Set the Interval property to your chosen interval, write your worker function and then pass it as a delegate to the Elapsed event. Finally, Start the timer.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Basically I want to create a method that can return a string message regarding on what currently happening inside a method
example:
public MainMethod()
{
//Execute One
//Execute Two
//Execute Three
}
upon using it I'm thinking of like this
something = delegate (string message) {console.writeline("{0}",message)};
the output would be
Execute One
Execute Two
Execute Three
Is this possible using delegate or lambda? if yes can I ask for an example on how I should correctly implement this? if no please help me with alternative.
Thanks
Use Func and Action. They make interacting with delegates much easier. Funcs have return values, Actions do not:
public MainMethod()
{
Action<string> writerAction = (message) => Console.WriteLine(message);
writerAction("Execute One");
writerAction("Execute Two");
writerAction("Execute Three");
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've got a large .net form with hundreds of input fields, sometimes users navigate off the form page without saving, what's the best way to check if a field value has changed when they try to navigate away? some c# function or javascript?
Don't take the control out of the users hand :-)
Ask him on exit, if he wants to save the changes. Maybe he did some changes by mistake, which you don't want to save.
You can achieve this by Javascript/Jquery. Something like:
$(document).ready(function() {
formmodified=0;
$('form *').change(function(){
formmodified=1;
});
window.onbeforeunload = confirmExit;
function confirmExit() {
if (formmodified == 1) {
return "New information not saved. Do you wish to leave the page?";
}
}
$("input[name='commit']").click(function() {
formmodified = 0;
});
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have simple question. How do I pause program? I want to change pictures very slowly.
My code:
private void button2_Click(object sender, EventArgs e){
Image picture1 = Program.Properties.Resources.picture1;
Image picture2 = Program.Properties.Resources.picture2;
Button1.Image = picture1
//Here I want pause
Button1.Image = picture2
}
If you want procedural code (like in your example), without timers and without locking the UI:
await Task.Delay(1000)
You can use Threads or Timers.
http://msdn.microsoft.com/en-us/library/swx5easy.aspx
http://programmingbaba.com/how-to-stop-system-threading-timer-in-c-do-net/
Or you can use Sleep method to puase your program.
http://www.dotnetperls.com/sleep
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm new to programming and I've noticed that callback functions are passed in as parameters to other functions
Otherfunction(argument,callbackfunction(code...code))
This seems cumbersome no? Why not just do the following:
Otherfunction(argument)
{
callbackfunction();
}
Callbackfunction()
{
//callback function is defined here.
}
Why inundate the parameter list of a function with a callback?
If you don't pass in the callback function, then the called function needs to know which function to call. This severely limits its reuse. Passing in a callback function also allows you to modify its behavior.
Consider the following:
void DoSomethingAndNotify(Action<string> notifyCallback)
{
// Do something
...
string result = "something was done";
notifyCallback(result);
}
void EmailNotifier(string message)
{
// Send message via email
}
void ConsoleNotifier(string message)
{
Console.WriteLine(message);
}
Then you can "do something" with different types of notifications, and you can add new notification types without ever having to change DoSomethingAndNotify().
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a send email method with a foreach, like this:
static void Main(string[] args)
{
foreach(var user in GetAllUsers())
{
SendMail(user.Email);
}
}
I need to improve that method. Using a multithread, because i dont want to wait the SendMail method executes each time for each user.
Any sugestions to do that?
Thanks
Try using a parallel foreach. I.e.
Parallel.ForEach(GetAllUsers(), user=>
{
SendMail(user.Email);
});
You can try something like this
private void Send()
{
Parallel.Foreach(GetAllUsers(), user =>
{
SendMail(user.Email);
});
}
I think the easiest way to do this would be thread pooling. .Net makes this pretty easy and you can read more about it here.