Prevent a form from freezing [duplicate] - vb.net

This question already has answers here:
How to prevent UI from freezing during lengthy process?
(4 answers)
Closed 5 years ago.
I'm working on my project and automatically sending email, but my form is affected and freezes until the process was finished. How to work on it without freezing current window? I'm working on VB.Net.

Your send e-mail code is running synchronously with the UI thread. You will need to run that code on a separate thread in order to maintain UI responsiveness.
Take a look at the following to get you started:
Safe, Simple Multithreading in Windows Forms, Part 1
Multithreading in Windows Forms Controls
The first link is quite old but it shows how to accomplish what you're attempting using the Thread class. If you don't care about synchronizing with your form and setting properties on controls, then this will do what you want. However, I would prefer the second link...
The second link uses the BackgroundWorker. This is the prefered method, at least for me. You can fire-and-forget if you don't care about being notified of the process (sending e-mail in this case) completing. However, with this class you can also work directly with the form and it's controls from the BackgroundWorker's RunWorkerCompleted event.

Related

Synchronize to Form Thread - without usinig Form

using Windows Forms it is possible to synchronize incoming data (like from events) which runs on another thread as that one which has created the form. However it is not allowed to update the form directly, therefore InvokeRequired and Invoke are used. Works great for me.
Now I want to move the logic part of my application (contains serial, networking, ... hence the threads) into a library. Also I want to have the synchronization in there, so there is no need to take care about that externally.
For now I create and pass a dummy form to my library, which is ugly, but works. At least some kind of. .Net Core has no Windows Forms. (3.0 has, but Windows only).
Even after reflecting the Form class I was not able to reproduce the synchronization process, so I could add it to my classes.
Does anyone know how to archive the same behaviour without Windows.Forms?
I use .Net Framework 2.0 and VB.Net, but C# examples are welcome as well

Visual Basic .NET how to finishing application without closing form

I am studying Visual Basic .NET
I would like to know that finishing application on the form without closing form.
For example, I am running task(without using thread) on the form.
When I click "stop" button, i would like to finish (like go to the end of program) without closing form.
I tried disposing or application.close. It was stopping and go to the end of the code. However, it was closing the form.
is there anyway that just finish task and keeping the form ?
Please read BackgroundWorker Class Sample for Beginners.
Designing applications that block the UI thread and then hope to be able to react to events during potential idle times is asking for trouble. Your UI will never be fully responsive and it will be a nightmare for anyone else to maintain and/or debug. The UI thread is intended to do one thing, interact with the UI. Long-running tasks are meant to be run on background threads and potentially split across multiple threads and/or cores.

Running Secondary Threads in Background Continuosly

I need to create a new thread which will have infinite while loop, so that it won't block main UI thread. The newly created thread is getting killed when I send the application to background mode, But I need to run it in background. Any help?
There is a difference between a Background Thread and a Background Task in iOS, but the underlying answer remains the same; don't run a background thread unless you simply cannot avoid it. Furthermore, you can't just spin off a thread and have it run if your app wants to support backgrounding in iOS.
First, you should read the documentation. It is extensive and provides many examples.
Then, if you have any specific questions not covered by the docs (or to clarify the docs), ask 'em here!

Threading advice VB.NET

Firstly, I've never used threads, but have found lots of examples on the internet about their use but nothing that obviously answers my question.
I have a class that loads and manipulates a file(s). It is fairly CPU intensive so I intend to put it in its own thread so that the GUI remains responsive. However, I would also like to use a progress bar to indicate the current status of the file operations.
The question is, what is the best way of approaching this, i.e. How do I get my file class to tell the app where it's up to? Do I have to add thread specific code to my class? Or is there an interface I can implement? Or, am I approaching this all wrong. Additionally (sorry, another stupid question) I assume I need an indicator in my file class to tell the thread when it's finished?
I'm using VS2010 and intend to build the app with WPF (if that's relevant)
Thanks for any advice,
Bob
Since you are intending to use WPF, use a Dispatcher:
Build More Responsive Apps With The Dispatcher
(Otherwise, for Winforms use a BackgroundWorker)

Best way of Multithreading

I have a college assignment due quite soon, and I need to be able to call a C++ dll that takes a long time (possibly infinte, it relies on user input)to execute. Im calling this through VB. My VB GUI freezes up when this happens, and I would like to keep the GUI responsive, so that the user can stop this possibly infinte loop.
Can anyone suggest the best/fastest way of doing this?
A bit of background, the C++ is trying to keep score on a snooker table using a webcam, and while the VB scoreboard updates easily, I would like to script it so that the analysis is almost continuous, while still allowing the user to interact. Currently the project requires the user to press a button to start the shot analysis, but it would be preferable if the program scripted itself. I have only realised this problem now and the deadline is very soon.
Update: Our lecturer suggested an option to solve the problem, but it would appear that most options here and the one he suggested will not work for us as the processing time required for the webcam image capture is too great to handle due to hardware constraints. Thanks for taking the time to help, it was much appreciated!
The best way to handle threading in VB.NET is via the System.Threading namespace.
You might also look into Application.DoEvents()
Try the system.Threading as Mark said, also try looking at Background Worker Process which is a bit simpler in VB.NET. Readup here
I would definitely use the Background Worker process. You can drag it onto your form and use the DoWork sub routine to actually do the work that is freezing your GUI thread. You can also use the ReportProgress event to actually provide progress back to your form.
As for your question regarding two separate threads, If both steps take a long time to complete I would run them in the same thread one after the other.
The one thing that could bite you with using the thread is cross-threading. In the context of your problem this means not having your second thread update form controls.
A really good resource for how to implement this code along with dealing with cross-threading is this PDF.
I also should point out that if you are using .net 1.0/1.1 you can still do the multi-threading, but don't have the luxary of having a background worker control. You'd just have to create a new thread from the System.Threading Namespace.
Just as an alternative, you could have your C++ actually processing in the background all the time. When called from VB, it would just be retrieving data from it or sending it a command (start, quit, ???) all of which would return instantly.
This could also give you more reliability since C++ would never miss video frames while VB was collecting the garbage or doing the dishes or whatever VB does in the background--C++ is going to let you be closer to a real time system.
RE: the comment about how.
What I'd probably do is have my VB programs send "Messages" to the C++ (As I said). A message is just a way to think of a function/method call--but generally they return quickly.
The "Start" message would tell the C++ code to start it's thread running and return. This is a Linux C++ thread howto, I'm not sure if you need to do something different in windows (I'd hope not, but I haven't used C++ as my main dev. language in decades).
If that doesn't work, just google "C++ Threads"
Sending a "Stop" message would stop the thread (and probably free resources).
A "Get Data" call would go to the location that the C++ thread used to store data, grab it and return.
Sorry to be so general, I'm pretty heavily Java these days.
I'm surprised nobody has suggested using a BackgroundWorker yet.