menu

arrow_back How do I make the UI "wait" for an operation?

by
1 vote
There is a desktop utility. It reads a large table from a file.
How to make the UI is not blocked while the file is being processed?
Please advise me on a good guide to threads.
I didn't get banned from Google, but I can't find it myself, since I'm new to dotNet. The material is too complicated.
The asynchronous method is unnecessary.
The only thing you need is to have the status "Processing file..." in the window while the file is being loaded, but the window itself is active.

UPD: The problem with threads is that there can be multiple files. And each new file is added to the MyTable object via the add_file method. So we have some kind of state to keep between file reads.

UPD2: Differently, how to change an object that was created in a UI thread in a new thread and not block the UI thread ?

2 Answers

by
1 vote
(new Thread(() => {
Invoke(new Action(() =>
{
mylabel.Text = "Обработка файла...";
}));

//Тут обработка файла
Invoke(new Action(() =>
{
mylabel.Text = "Обработка файла завершена!";
}));

})).Start();

8 Comments

Ah, if only it were that easy. Updated the question
Vladislav , Primitive types yes, but add something complex to the
Invoke(new Action(() =>
{
//сюда код для выполнения в основном потоке
}));
P.S. Or BeginInvoke for asynchronous execution
twobomb So I can safely change an object that was created in the current thread in a new thread?
Vladislav , Not quite clear specified, well in any case this way will work for one file and for several, states can be stored in separate variables, it is not clear what kind of states.
twobomb Yes, that's right, my inattention.
freeExec It says so, and all items are changed through Invoke
twobomb Thank you! I'll try
twobomb You can't just change UI elements in any thread. This can only be done in the thread in which they were created, i.e. in the main thread.
by
1 vote
Read the article on the rsdsn website about streams. I recommend it to everyone. Flows is a topic you have to dive into completely.

1 comment

Vladislav