Create different thread in node.js - node.js

can't we create different thread in node.js ?? I want to pool database for every 1 sec in different thread and run main program in one thread. I used setInterval function but, it seems, it is blocked or not acting as expected.
I am new at node,Nice reply would be nice.

This question has been already answered here. But to resume, you cannot create Thread (except if you use add-on) but you can run new NodeJs process.

setInterval won't work in this case. You probably need to write a batch script separated from the main program and invoke it using crontab. Checkout crontab man page

Related

Node.js, not works only in single thread by default

I have a question, Node.js uses libuv inside of u core, to manage its event loop and by default works whit 4 threads and process queue whit limit of 1024 process.
Process queue limit
Threads by default
So, because most programmers say it's single thread?
By default, node.js only uses ONE thread to run your Javascript. Thus your Javascript runs as single threaded. No two pieces of your Javascript are ever running at the same time. This is a critical design element in Javascript and is why it does not generally have concurrency problems with access to shared variables.
The event driven system works by doing this:
Fetch event from event queue.
Run the Javascript callback associated with the event.
Run that Javascript until it returns control back to the system.
Fetch the next event from the event queue and go back to step 2.
If no event in the event queue, go to sleep until an event is added to the queue, then go to step 1.
In this way, you can see that a given piece of Javascript runs until it returns control back to the system and then, and only then, can another piece of Javascript run. That's where the notion of "single threaded" comes from. One piece of Javascript running at a time. It vastly simplifies concurrency issues and, when combined with the non-blocking I/O model, it makes a very efficient system, even when lots of operations are "in flight" (though only one is actually running at a time).
Yes, node.js has some threads inside of libuv that are used for things like implementing file system access. But those are only for native code inside the library and do NOT make your Javascript multi-threaded in any way.
Now, recent versions of node.js do have Worker Threads which allow you to actually run multiple threads of Javascript, but each thread is a very separate environment and you must communicate with other threads via messages without the direct sharing of variables. This is relatively new to nodejs version 10.5 (though it's similar in concept to WebWorkers in the browser. These Worker Threads are not used at all unless you specifically engage them with custom programming designed to take advantage of them and live within their specific rules of operation.

How to run the timer tasks asynchronously?

Hi am developing a windows phone 8 app using C# and xaml.
My previous team has developed some code, they have used many methods in timer control.
when it is updating all the methods are calling and its blocking the UI.
Is there any another way to use the timers asynchronously so that the UI can not be blocked.
Thanks in advance
you should use the "xaml binding" to updating the UI
There are several Timer classes in Windows Phone.
System.Windows.Threading.DispatcherTimer: runs on UI thread and makes UI unresponsive when executed.
System.Threading.Timer: executes on a ThreadPool thread, therefore can not update UI directly. For an example how to make a cross-thread call to update UI, see the example in the link.
There is also a ThreadPoolTimer, seems it works like a System.Threading.Timer - runs on ThreadPool thread, just has different methods. But I have not used it.
So to answer your question, if a timer event blocks UI, then it is likely a DispatcherTimer, replace it with System.Threading.Timer, reference the code sample in the previous link.
Did you try using the DispatcherTimer?
How to Increment timer asynchronously ?
Or else you could go with the CountdownTimer.
How do you run a synchronous timer in C#?
as #kennyzx said there are many ways to do it, but the choice is yours.
a sample on CountdownTimer from the Toolkit

Multithread Form Application (.NET 4.0)

I'm currently working on a solution that has two projects, a console and a form application. The console application is the main entry point to my application, and from the console the user would run the form application.
The problem is, when the user boots the form application the rest of the business logic (from the console app) won't run until the form is closed. My first thought was to use a background worker for the form, but the business logic in the form project already uses a background worker (and I only have two CPUs...). Perhaps this could be my ignorance for multithreading, but is there a way to do this?
Any thoughts are much appreciated!
Cheers
Well, this is pretty unusual. In general, it doesn't make a lot of sense to provide the user with a nice GUI and still leave a console window up and interactive.
But yes, calling Application.Run() or Form.ShowDialog() is going to block the thread. It has to, the message loop needs to be running to keep the GUI alive. If you do this, be sure to put the [STAThread] attribute on the Main() method.
The only other decent alternative is to start a thread. This isn't a problem, a UI thread doesn't burn any CPU cycles. Code only ever runs when the user does something, it's otherwise idle 99% of the time. Be sure to call the thread's SetApartmentState() method before you start it, STA is required.

Linux service and Source for cron job

I am new to linux and writing a service in C++ which spawns multiple threads and I am starting the service by calling it from init.d, but how should I send the terminate signal to my application from the script , so that my service terminates all the threads and exits.
And also where can I find the source code for any linux services. e.g. /etc.init.d/rc5.d/S14cron . It will be helpful in understanding how to implement a service.
The classic reference for this kind of question is Steven's "Advanced Programming in the UNIX Environment". You can find the source code to this text book here.
Depends what your application does.
Personally I'd keep a thread just for handling signals and call sigprocmask in the other threads to stop signals being delivered to them.
The main thread / signal handling thread (it is usually a good idea to make this the main thread) can then send a message to its threads to tell them to finish what they're doing and quit.
Alternatively, if you like the principle of crash-only, you could just call exit_group and be done with it :)

How can you detect if your MFC application is not responding?

How can you detect if your MFC application is not responding?
Either the same application can start a separate thread, or some other application can run its own thread and periodically call SendMessageTimeout() to send the WM_NULL message to the application in question. If it times out it means that the application is irresponsive.
If you're asking how to do it from within the process itself, you can't, it's a paradox. A blocked process can't detect if it is not responding. It'd be like someone waking himself up to ask himself if he's sleeping.
Based on this and your other question, I'd guess you have a long-running operation and you want the user to wait until it's finished. If they click your window before it's done they get "not responding" and may terminate the application too early.
You need to perform the long-running operation on a separate thread. Here's a great starting point: CodeProject article

Resources