I'm developing a win forms application for Windows CE based devices.
I have to initialize all controls my dialog shows at runtime because I get the information about the UI layout from a backend system via WLAN every time the user performs an action on the screen.
The performance is not the best but it's ok in most cases. But sometime there is a thread in background that ends and the next function call in main thread needs 300-400 ms to return.
In debugger I can see the thread ID from the thread that ends. Is there any way to find out which thread is associated with this thread ID, so that I could keep the thread alive until the application is in an time uncritical part?
I think it's a thread from an UI element because I don't start threads manually.
Related
Maybe it's a simple question, but I don't get it. When should I use concureency in my javafx project? Is it right that I should use for every task, which do some action in the background, the Concurrency API? So every action in my controller class, which has nothing to do with the UI should be executed in a single task?
I really don't get it how to use this right....
Whenever you have a task that may take sometime to get executed or there is a possibility of delayed response, you do not want your JavaFX Application thread to wait for it, because, as long as the JavaFX Application thread waits for the response, the UI becomes unresponsive.
A few examples where you may want to use a background thread is :
An I/O operation
A web service call
From the JavaFX documentation :
Implementing long-running tasks on the JavaFX Application thread inevitably makes an application UI unresponsive.
On the other hand, if you have minor calculations or some task which can be completed in a jiffy (I am not sure if this is the correct word, but I hope you can relate to what I want to say) and will not put the JavaFX Application thread on wait, you can execute them on the same thread.
More specifically, my application is a network application, a kind of hub in which different endpoint connect and communicate. We need a graphical user interface to monitor the behavior of the participant to the hub, and etc....
Provided of course that the appropriate communication between thread is applied such that for updating the UI thread from another thread etc... does it matter that the GUI thread is the main thread or not.
Up until now, my Gui thread was a separate thread launch from my main thread. However a colleague told me that it was wrong.
Does anyone has some lessons learned or best practice that you could share with me on that subject ?
Many thanks
Maat
What do you mean by "the main thread"?
If you mean "the thread which calls main method", it doesn't matter.
If you mean "the thread which does important work for the application", it should definitely not be the same as GUI thread (which should never run any long-running methods or wait for anything except GUI events).
My application spawns a secondary thread that performs long database tasks in background.
For to access the DB in the secondary thread I implemented this good solution, and it works perfectly until I open a modal dialog: the execution of secondary thread in this case it is blocked until I close the Dialog.
How do I avoid this?
I want to leave the secondary thread free to perform database tasks for the antire life of my application, without interruption.
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.
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