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 6 years ago.
Improve this question
I'm doing some masks for SAP B1 using c#.
I'd need to know how to create a function that, automatically (for examples every 15 minutes), take some data and put its on a database.
The function is already done but how can I create the automatic execution in background?
Best regards and thanks in advance for the reply,
Lorenzo
Timer is what you need:
var timer = new System.Threading.Timer(
e => Method(),
null,
TimeSpan.Zero,
TimeSpan.FromMinutes(15));
This will call Method() every 15 minutes.
Timer info : https://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
You could use Timer like #AmbroishPathak suggested. Also, as mentioned in the comments, you can use the Windows Task Scheduler to run your script or executable. The advantage of this is that the process won't be running in the background while it's not doing work.
You can see the details of how to schedule a task here.
The following answer describes this as well: windows scheduler to run a task every x-minutes?
To summarize the accepted answer there, you create the task to run once a day. After that, you can double-click on the task to bring up its Properties window and go to the "Triggers" tab. Under "Advanced Settings" you should be able to set it to run every x number of minutes.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want a Method in C# which is going to make a break for 5 Seconds between 2 lines of codes.
Thread.sleep(5000);
isn't working because the rest of the code does also make a break. Have you got any Idea how to solve this?
falling.timespeed = 0.6f;
falling.fallingl = -50f;
// here I want a break for 5 Seconds
falling.timespeed = 1f;
falling.fallingl = -200f;
Are you familiar with the asynchronous features in modern c#?
In c# 5 and above:
async Task DoSomeWork()
{
// Do the first part of the work
await Task.Delay(5000); // Asynchronously wait for a 5 second timer to expire.
// Do the second part of the work
}
Note that, depending on the threading context that you call this method, part 2 may be executed on the same thread, or a different thread.
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 6 years ago.
Improve this question
In my WebApi project I have an endpoint that should delay a small block of code.
Let's make an example:
I need to implement a mechanism that permit the client to book for a resource. The booking time should only have a duration of 120 seconds and then expire.
In terms of code I have something like this:
//booking
foreach (var item in list) {
item.Status = ItemStatus.Booked;
}
await _context.SaveChangesAsync();
//Setup a delayed "thread" which removes the booking
Task.Delay( TimeSpan.FromMinutes( 2 ) )
.ContinueWith( x => {
//loop on the list and set the status to "ReadyForSale"
//if this is still in the Booking
} );
return;
I would like to understand if a solution like this satisfy my requirement. The current thread should not be blocked from the delayed task and I need to find a way to pass the list of items to the delayed task.
I think there is a risk that the task will never run. What if the sever crashes or IIS decides it needs to recycle the pool.
There is the risk that the state would never be restored.
I would probably set a bokedAt DateTime field in the database and then check if that time compared to now is more than two minutes to determine if the item is booked or not. Perhaps even a computed column that checks this and returns the state.
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 8 years ago.
Improve this question
I want to read sms from my GSM modem.
I wrote C# code.
This code run when I click start button.
I want to my program read sms when received, not click button.
thanks.
Your program is keyed to activate when you press a button, some method is called. You need to call this method when SMS data is received instead. This could be done using threads (SMS thread and main thread showing data) although it could just as easily be done using a cycle. In pseudo code:
while (don't quit) {
display page;
check for sms data;
sleep for small time to allow other OS programs to run also;
}
This is a "tight loop" and can use excessive amounts of CPU time depending on the code of the actual steps. For a tight program loop one simple method is to apply some sort of sleep method.
There are other ways to do the same thing, visitor pattern could probably be used, threads, etc...
It seems that you are only lacking the cycle. Your programs is probably more like:
while (don't quit) {
display page.
wait for button press.
}
although that flow wouldn't be obviously apparent at first glance without studying your program flow.
If you are using triggers (the button press is probably a trigger) you can trigger on a timer that fires as often as you want (100ms, 1 second, whatever) that checks for SMS data when fired, if there is SMS data it updates the form.
Many, many ways to do this. A quick Google for "program flow" doesn't find any useful links at first glance that would explain the many ways you could do this. Perhaps looking at other's code would help. I've often searched open source repositories for code I could look at to see how someone else did something.
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 9 years ago.
Improve this question
My system clock is going crazy randomly at any moment and changing the system clock's date/time to a random one. It's not the lithium battery nor a virus because I checked. Also it's not something from the Windows.System.Time itself.
I want to create a process that will, on an interval, check to see if the system clock's date/time matches the global date/time and if not, it would sync.
I need this to run in the background. I am not even sure if a Windows process is correct way to accomplish this. I am open to any other solutions as well.
Create a new c# empty project. Click on the project and go to Properties change the output type to Windows Application (This will remove the console).
Create a new class example: Example.cs
Write the static entry point eg:
public class Example
{
static void Main(string[] args)
{
}
}
Insert your code in the Main routine.
This will create a process that contains no console/window/service.
I'm guessing this is what you want.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In a lot of languages, there's a
wait(milliseconds)
or
sleep(milliseconds)
command, that just makes your program not execute any more code until the sleep is up (this doesn't mean it stops the code that's running, it just pauses from running more. Is there a command like this in C#?
Thread.Sleep(time) doesn't work, it pauses all code that is currently executing as well (basically freezes your program)
Thread.Sleep(int milliseconds)
is what you're looking for
In C#, everything is running on one thread by default. If you want you can create another thread to run a specific piece of code. Then, you can sleep that thread so your app won't freeze.
Check this question out for more information: How do I run a simple bit of code in a new thread?
Yeap:
Thread.Sleep(milliseconds)
http://msdn.microsoft.com/es-es/library/system.threading.thread.sleep(v=vs.80).aspx
Yeah there is.
Thread.Sleep(milliseconds)
For example, if you use Thread.Sleep(5000) , it will make thread sleep for 5 seconds. Read more # MSDN
Thread.Sleep(milliseconds) should do it for you
You'll want to verify the syntax as I'm just wingin' it, but this will go into a non-blocking loop for the designated amount of time...
// Set the wait time
double waitTime = 5.0;
// Get the current time
DateTime start = DateTime.Now;
// Check the wait time
while(DateTime.Now.Subtract(start).TotalSeconds < waitTime)
{
// Keep the thread from blocking
Application.DoEvents();
}
That assumes you're using Windows Forms. If you're using WPF:
//Application.DoEvents();
this.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { }));
Again, you'll want to verify the syntax first...