Run function aynchronously in server asp vb.net - vb.net

I am developing a web application in asp.Net(VB). I am trying for some calculations on a button click.The calculation takes almost 30 min to complete .
I need to make the process independent; that means i will click the button and the process should start in the server so that I can close the page itself. Please advise me a propee approch to achieve this .

You should use the Parallel Task Libray.
First of all, you have to do the calculations not in the click event handler but in a separate async function and call that inside the click event. This way you don't block the Page Life Cicle.
Private Sub MyButton_Click(sender As Object, e As EventArgs) Handles MyButton.Click
DoSomeCalculations(Params)
End Sub
Private Async Sub DoSomeCalculations(Params)
'Do stuff
End Sub
If you intend to publish the app to Azure, take a look at Azure WebJobs. It is the best solution for your case scenario.
Anyway... can I ask what kind of calculations take half an hour?

Related

How can I handle application startup without using default form mybase.load

I am having trouble figuring out how to run code on application startup. The scenario is, I need to check if a user setting exists, if it does not, then I need to open a company configuration form. I tried creating a module to store application level events, but get an error:
BC31418 Visual Basic AND VB.NET in modules must specify a 'WithEvents' variable qualified with a single identifier.
Module ApplicationEvents
Private Sub MyApplication_Startup(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
If My.Settings.xmlpath = "" Then
Dim f As New CompanySetup()
f.ShowDialog()
f.Dispose()
End If
End Sub
End Module
I really do not want to put the IF statement into the default form mybase.load handler, since this application is in early stages, and the default form may change, leading me to have to move this code around. Any help on this error, or suggestions of how to get the IF statement to occur on application startup without tying it into a sub on the default form would be greatly appreciated.
Get rid of that module. Open the Application page of the project properties and click the View Application Events button. That will open the proper code file in which to create a Startup event handler. You can do that using the second and third drop-downs in the Navigation Bar at the top of the code window.

VB.net How do I tell my program to wait until it has completed a task before it carries out the next one?

I have a program that is works fine one minute but as soon as my internet connection slows it stops working.
This is because I have everything set by timers.
I have tried slowing the intervals and still it messes up from time to time.
There must be a way for the program to wait until it has completed one task and then automatically carry out the next task instead of hooking the next task up to a timer.
Here is an example of what I mean:
WebBrowser1.navigate("http://www.google.com")
[IF COMPLETED THEN MOVE ON TO NEXT TASK]
WebBrowser1.Document.GetElementById("gbqfba").InvokeMember("click")
I'm pulling my hair out trying to work it figure it out lol
The code in that method should end at the Navigate call. When the WebBrowser has finished loading the specified page, it will raise its DocumentCompleted event. You handle that event and process the page in the event handler. You can then call Navigate to load the next page and the process continues. By handling the event you don't have to care how long it takes to load the page. you know that you'll process it the moment it is loaded; not sooner, not later.
Threading.Thread.Sleep(1000) ' 1000 milliseconds is 1 second
Looking at your code, why not use the events?
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
WebBrowser1.Document.GetElementById("gbqfba").InvokeMember("click")
End Sub

visual basic .net continue application

I just stat to study visual basic .net
I would like to ask about how to continue application.
For example
Private Sub UI_BT_SAVE_STOP_Click(sender As Object, e As EventArgs) Handles UI_BT_STOP.Click
Stop
End Sub
then , application will stop on the middle of processing(like a stop button on the visual studio)
I was trying to make continue button, but I can not find continue function to change my application status from stop to continue.
is there anyway to continue application again ?
thanks
There's nothing quite like that but you could always try importing the sleep function, seen here:
https://stackoverflow.com/a/469358/1504882
This will suspend everything the application is doing though.

VB.NET Windows Service Main Code

I am trying to learn how a Windows Service works using vb.net and I have no clue where to put my main code. what I mean by main code is the code that needs to be run every couple of seconds. So far, I was able to write a text file using onStart method provided by the service its self. I have installed the service manually and made it run.
do i need to create some kind of threading that trigger the main code? and in which method.
Thanks.
Use a Timer. They start an event on every tick. The tick can be modified to the length you want it to be.(Interval)
Here's a great article/tutorial on them: http://www.dreamincode.net/forums/topic/58033-using-timer/
Add a timer to your application by going to the toolbox and clicking a Timer and adding it to the form. Then go to the properties and add what interval you would like it to do an event(in milliseconds) the set the enabled property to true.
Click on the timer icon below the form designer and you will be directed to the code editor with the Timer_Tick event. Then in when I put the comment 'what you want it to do, this is the code that will be executed every tick(the interval what you selected, for example 1000 is a second)
Code:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'what you want it to do
End Sub
If I am being unclear here is a tutorial: https://www.youtube.com/watch?v=6wWZIuOAyM4

Visual Basic 2008 - New Form on a different thread

My “form1” is just a simple page with buttons that launch different forms which do all the work, the "form1" code for the first four buttons is below.
What I want is for each form to run in a separate thread.
Public Class Main
Private Sub btnDownLoadStockPrices_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownLoadStockPrices.Click
LoadStocksFromDownloadSite.Visible = True
End Sub
Private Sub btnLoadOptionsIntoDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadOptionsIntoDatabase.Click
LoadOptionsIntoDatabase.Visible = True
End Sub
Private Sub btnVerifyDatabases_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerifyDatabases.Click
VerifyDatabase.Visible = True
End Sub
Private Sub btnAnalyzeStock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyzeStock.Click
AnalyzeSingleStock.visible = True
End Sub
End Class
I’ve found plenty of code examples to have different buttons on a single form run a subroutine in a separate thread, but for some reason I can’t seem to apply it to an entire form.
I think it’s something simple, like I need to tie each new thread as a handle to each forms “load” routine, but I just can’t get it to work. I don’t care about “synchronizing” threads at all, as each form is really a completely different functional program.
Any help would be much appriciated!
This isn't very common; generally it's best to limit all UI stuff to a single thread. But if you're convinced that you need each form to run on a separate thread, you must take into account the Windows API event handling model. The [over]-simplified version is that each form must have its own message loop to remove event messages from the queue and process them, so if you want to open a form on a new thread, you need to create that message pump.
The easiest way to do that is using the Application.Run method, and let the .NET Framework handle creating that message pump for you. For example:
Dim frm As Form1 = New Form1()
Application.Run(frm)
From looking at the code shown in your question, I can't discern any possible reason why those forms would need to run on separate threads. You can call the Show method of multiple forms so that they will be displayed on the screen at the same time. They won't block each other as long as you don't use the ShowDialog method, which displays each as a modal dialog. This is the way so many applications display multiple toolbox windows and other kinds of forms on the screen at the same time.
If you need to do some type of processor-intensive calculation, you still don't need to run each on a separate thread. Spin up a background thread (the BackgroundWorker class makes this very simple) and update the appropriate form's UI using the Invoke method.
You can certainly do this on Win32 but I don't know how well this maps over to .net.
The essential issue is that window handles have thread affinity. So you really need all interaction with them to happen in that thread. Essentially this means that you create all the window handles associated with that form in its thread. You also need to run a message loop in the thread.
The reason that people usually run all the UI out of the main thread and handle long-running actions in separate threads is that it is easier that way. You should ask yourself again why you want to do it this non-standard way.
I suspect you are not quite seeing the full picture. The need for threads in a desktop app principally arises when you have long running actions. Usually you want to keep your UI responsive and providing feedback for the long running action. Doing so leads to threads.
However, in your proposed solution you now have a multitude of extra threads and complexity, and you are still faced with the original problem! A long running action on one of your forms will hang it unless you perform that action in a separate thread, and once again we reach the standard solution to the problem.