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.
Related
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.
I may be the only person still automating excel this way and I am looking at doing it in ways that better handle COM objects and such, but for now I just import Microsoft.Office.Interop.Excel and go for it. Something that gave me a lot of grief at first was hanging excel.exe processes, but there are tons of resources online for fixing that. At this point I am pretty confident in my abilities to handle it.
One thing I did notice and was wondering about is how does closing a windows forms application interact with excel.exe running in the background and likely more specifically the garbage collector. I have written quite a few excel automation projects and noticed that after I run my program .exe and it has completed there is an excel.exe process that hangs on until I close the pop up window from the windows forms application. Why is that happening?
Does vb.net not call garbage collect until after the window is closed? Looking at the code I write there is no reference to an "on close of application window" event and the garbage collector and release COM objects calls are all right in the code, so it seems curious to me that after the code has executed (which I take to mean garbage collect has been called, as in the code) there is an excel.exe process that stays up until I actually close the windows form application window.
Nothing urgent, more curiosity than anything, but any direct would be appreciated. Thanks.
Make sure you are calling the Quit method on the excel object at the end of your code block and you can garbage collect for good measure but it should close in a short while. I had the same problem but found I had to dispose manually.
I am getting a System.OutOfMemoryException from my vb.net application, it happened from one certain user. I am trying to figure out whether my application takes too much memory space or the PC has low memory. But she just upgraded her desktop as i3 Intel CPU and higher memory.
Is there any tool that I check memory consumption when code is executed as go through lines?
By the way, it is developed by vs2010 and It is a Windows application.
<--Edit-->
I found the problem from my application. I did not mention because I did not know what the problem was. I am using a Google map inside of vb.net application. It takes a memory whenever I search a map on web browser which is on a form . Even though I close the form, it is not disappeared from
memory. It is just going up. That goes away when I close MDI form. FYI..
The best tool we've worked so far is the JetBrains profiler (http://www.jetbrains.com/profiler/features/).
That's a common error. It normally happens when the application enters a loop that never ends... (infinite loop).
Is it a Console, WinForms, ASP, WPF?
Try to identify during which Event your application throws the exception and put a try catch to print out more details.
Just check out this cool WPF performance suite to check out if you have memory leaks:
Performance Toolkit
Or check out if you can use GC.Collect() to find the leak.
Also you can accecss in Process Explorer to any .Net memory data and peaks.
Regards
For couple of hobby projects of mine, I've been performing form transitions (fade-in/out, slide-left/right) using timer control, and I know its not the right way to do it. Using timer has its own disadvantages as it is a CPU hog if logic is complex and also, transitions are not smooth. So, I'm wondering how can I perform form transitions without using any timers and just by using native Windows API or any third-party library. I came across with FlowFX but found that it is limited only to .NET Compact framework.
Thanks...
We don't know what your timer handler is doing without a code sample, but using a timer to do animations is an acceptable method.
Here is another SO question and answer that might show you a better way of coding your timer handler.
The "not smooth" part could perhaps be overcome using double-buffering.
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.