Modal dialog is blocking secondary thread run - multithreading

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.

Related

Compact Framework Thread ends in background --> Performance issue

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.

The JavaFX Concurrency | When to use it, how to use it right?

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.

Delphi - Creating a control that runs in its own process

HI
I have a control that accesses a database using proprietary datasets. The database is an old ISAM bases database.
The control uses a background thread to query the database using the proprietary datasets.
A form will have several of these controls on it, each using their own thread to access the data as they all need to load simultaneously.
The proprietary datasets handle concurrency by displaying a VCL TForm notifying the user that the table being opened is locked by another user and that the dataset is waiting for the lock to be released.
The form has a cancel button on it which lets the user cancel the lock wait.
The problem:
When using the proprietary datasets from within a thread, the application will crash, hang or give some error if the lock wait form it displayed. I suspect this is to do with the VCL not being thread safe.
I have solved the issue by synchronizing Dataset.Open however this holds up the main thread until the dataset.open returns, which can take a considerable amount of time depending on the complexity of the query.
I have displayed a modal progress bar which lets to user know that something it happening but I don't like this idea as the user will be sitting waiting for the progress bar to complete.
The proprietary dataset code is compiled into the main application, i.e. its not stored in a separate DLL. We are not allowed to change how the locking works or whether a form is displayed or not at this stage of the development process as we are too close to release.
Ideally I would like to have Dataset.open run in the controls thread as well instead of having the use the main thread, however this doesn't seem likely to work.
Can anyone else suggest a work around? please.
Fibers won't help you one bit, because they are in the Windows API solely to help ease porting old code that was written with cooperative multitasking in mind. Fibers are basically a form of co-routines, they all execute in the same process, have their own stack space, and the switching between them is controlled by the user code, not by the OS. That means that the switching between them can be made to occur only at times that are safe, so no synchronization issues. OTOH that means that only one fiber can be running within one thread at the same time, so using fibers with blocking code has the same characteristics as calling blocking code from within one thread - the application becomes unresponsive.
You could use fibers together with multiple threads, but that can be dangerous and doesn't bring any benefit over using threads alone.
I have used fibers successfully within VCL applications, but only for specific purposes. Forget about them if you want to deal with potentially blocking code.
As for your problem - you should make a control that is used for display purposes only, and which uses the standard inter-process communication mechanisms to exchange data with another process that accesses your database.
COM objects can run in out-of-process mode. May be in delphi it will be a bit easier to use them, then another IPC mechanisms.

cache coherency in application

cache coherency protocol is well known in multi-cores context which is in the hardware low-level, however, we will meet the similar case in the application domain. Recently, I am working on a project which has two threads updating shared objects.
UI thread is responsible for
displaying and updating(via users
action) objects.
Background replication thread
periodically updates the shared
objects if something is changed by
other users.
since we have lots of objects(50,000~100,000), each thread have to copy part of objects into its own buffer, updating the shared objects is serial.
UI thread will not update object
each time when users update.
Background replication thread will
update objects immediately once
changes are found and then notify UI
thread to refresh.
So this brings the question, if there is a object updated by two threads, how is conflict sovled? Is there any common idiom to handle this case?
The simplest way to handle this is to use a mutex. The UI locks the mutex before it reads the value, then unlocks it afterwards. The background thread locks the mutex before it updates the value, then unlocks it afterwards.
You can have the update thread send a notification message to the UI telling it to reread the shared object, as you suggested.

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