Windows Service Startup (Timeout (30000 milliseconds)) [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
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
Improve this question
I have created a windows service Setup file for my project in vb.net,but after windows service installation when i am trying to start that windows service its throwing the following Error:
The service did not respond to the start or control request in a timely fashion.
Timeout (30000 milliseconds) waiting for the Test service to connect.
What can i do?

You're limited to a 30 second startup time in a windows service. If the service doesn't start responding to ServiceControlManager calls after 30 seconds, it gets terminated.
This generally happens because you've put some long running code in your OnStart() method which doesn't complete in a timely fashion.
Try to keep the bare minimum of code in your Service Constructor & OnStart methods and then anything else if needs to do such as calling the database, loading data, IO, calling external services etc, should be done in the main application.

Have a look at your service OnStart() method? Are you doing any heavy-lifting there? Not throwing exceptions if something taking longer than expected?
It looks as if you trying to do stuff like this
class MyService
{
public void OnStart()
{
//blocks here
Thread.Sleep(TimeSpan.FromSeconds(31));
}
}
Instead you should do something like
class MyService
{
private Thread workerThread;
public void OnStart()
{
workerThread = new Thread(()=>
{
Thread.Sleep(TimeSpan.FromSeconds(31));
})
// doesn't block here
workerThread.Start();
}
}

Related

Best way to handle mail request and messages [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am building an MVC app and in this appllication there are actions that implies some things. So we wish to warn our customers / users using mailing system. I'm building both a local application and a web store, so I'll need to send a lot of mails sometimes.
I am currently using MvcMailer why does nicely its job, but my main concern is that since it occurs during a normal method call (ex: result of an operation, then:
MvcMailMessage msg = mailer.NewOrder(emailTo);
msg.Send();
And the message goes, it takes a while. And since this kind of operation might be called quite a few times, it will overall slow down the whole process, which I do not wish.
So my question is: how should I handle mail processing? Is there an asynchroneous thing I may use that will do the job? Do I store them in a database table and send them sometimes? I've heard about Task in windows .Net, but I've never used any, is that an option?
I'm looking for suggestions, so feel free to share your opinion! Thank you!
You can use SmtpClient.SendMailAsync using the async-await keywords
public async Task SendSmtpMailAsync()
{
SmtpClient smtpClient = new SmtpClient();
MailMessage mailMessage = new MailMessage("FromAddress", "ToAddress", "Subject", "Body");
await smtpClient.SendMailAsync(mailMessage);
// Possibly do more stuff here.
}
When you await on an asynchronous method, control yields back to the caller. What that means is that the ASP.NET can process messages in the meantime using the thread that returned to the ASP
NET ThreadPool from that same method. When the method finishes, it will return back to the awaited line and continue execution.
Note that using this async alone wont return the request to the caller, it will simply let you process more requests in the meanwhile. What you can do is use this method in correlation with a producer-consumer style collection, like BlockingCollection<T>, add your messages to it and return the response to you caller. In the background, use SendMailAsync to execute these requests.

Memory issue or not a best practice [closed]

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
Please let me know if the following design is bad for managing heap /memory and from a design pattern point of view also.
Here I am using a C# Timers.Timer on a windows service where the time tick event fires on multiple threads.
Here my main point is about heap memory consumption , since lots of objects being created in different threads in micro sec and also we cannot guarantee when GC will collect them. so this can be harmful to heap and performance issue later to the application.Am i correct.
timer_tick()
{
Test objTst=null;
try
{
objTst=new Test();
objTst.Process();
}
catch(execption e){}
finally
{
objTst =null;
}
}
Please let me know following design is bad for manage heap /memory wise and design pattern wise also.
This "pattern" serves no purpose. Setting the variable to null is not required. As soon as the method completes, and objTst goes out of scope, it will be eligible for garbage collection, even if you don't set it to null.
I would also recommend not having an empty exception handler that just swallows and ignores exceptions entirely. If nothing else, you should at least log the exception you receive.
In general, I would write this as:
private void timer_Tick(object sender, EventArgs e)
{
var tester = new Test();
tester.Process();
}
If you have an exception logging mechanism, you could wrap it in a try/catch to handle or log the exceptions, but don't just swallow them completely.

Debug.WriteLine doesn't work on different threads [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
var timer = new Timer
{
Enabled = true,
Interval = 500
};
Debug.WriteLine("new timer created");
timer.Elapsed += (s, e) =>
{
Debug.WriteLine("hello from timer");
// this doesn't get shown
// in the output window
}
What do I need to do to be able to see my last Debug.WriteLine() in the output window above?
You are actually not using separate threads, the System.Timers timer runs on the UI Message pump if it is available. If you have blocked the UI thread the timer will never run.
Check either that the UI thread is not blocked, use System.Threading.Timer that does not use the UI thread, or set SynchronizingObject to null so it will use a threadpool thread instead of the UI thread.
EDIT: Or like Hans said in his comment, you are running in the console and Main() is exiting before the timer has a chance to fire.

C# Having two programs reading and writing to the same text file [closed]

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
Is there a way to have the first program writing to the text file, whilst the program waits until the text file is not being used by another process.
You can use the FileSystemWatcher class in the second program, to see when the first program is done. The FileSystemWatcher will trigger an event if the file has been created, modified, or deleted.
More information: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
The best way is to implement a 3rd program which acts as a queue. Your first 2 programs send data to this program, which does the actual writing to the text file.
This 3rd program could be an MSMQ, or a Windows Service, or a set of web-services, or anything - it depends on your requirements...
To prevent the race condition. You will have to implement try-wait-retry option.
Try to read from file
if it fails then wait for a while and try again
Yes. You place each logical write onto the end of a queue. In another thread you read logical writes from the front of the queue and attempt to write them to the file (open file, write, close file); if an IOException occurs during this then you leave the write on the front of the queue, otherwise you remove it from the front of the queue. If both processes following this pattern then they will be able to interleave writes to the file.

Is there a "Sleep" command? [closed]

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...

Categories