vb.net writing background multithreading - vb.net

My application will creates an Excel from DB, creating an excel file takes few minutes of time and till that time the user dont want to wait for the excel to complete, he should start working with the application. Once the excel have been completed the appilcation should po-up message saying, 'Report Sucess'.
can any one tell me how to do this, whtr background thread will help me in this ?
if yes pls provide me some sample, newbie to threading....

It depends what you do during the Excel creation.
A very simple and effective way to create and control a background thread is using a BackgroundWorker.
It very easy to implement.
And it's thread safe
You can find a working example I've prepared (BackgroundWorkerDoStuff).

Related

Running a block of code in the background of AppleScriptObjC app?

Is it possible to have a block of code constantly running or 'repeating' in AppleScriptObjC, whilst the rest of your script is still active? I've got a block of code that I want to repeat in the background that checks to see if a folder exists and creates it if it doesn't, but if I set it to repeat indefinitely then I lose the ability to access the other buttons and code blocks in my app?
Apologies if there isn't enough info here or it's a bit confusing!
I'd probably create a repeating NSTimer to do the check at a specified interval. That would avoid blocking your main thread.
Depending on the app, perhaps you could also just check for the existence of the folder and create it just in time, when you really have a need for it.

vba switch process based on time [duplicate]

This question already has answers here:
Multi-threading in VBA
(8 answers)
Closed 6 years ago.
How can you switch between processes based on time?
++++
I have an Excel macro sending XML requests to Google, it has 3 important processes.
The process sending the XML requests. This process is limited to 10 requests per second.
A loading bar with the ability to quit the program
The Excel Application itself (it should be editable while the program is running)
The macro is currently using the sleep function (from kernel32.dll) to delay the processes, and the DoEvents function to make sure both process are run (and so the application can be modified in the background).
Ex. Process 1
Do While True
Call doSomething
DoEvents 'handles processes 2 and 3
Sleep 100
Loop
This method delays the entire application. Editing the Excel Worksheet in the background is laggy because of the 100ms delay, and attempting to quit the program is also delayed.
To remove this lag, I would like processes 2 and 3 to be running and switch control to process 1 when it is time to send a request. After the request is sent, process 1 can give control of the program back to processes 2 and 3. Does anyone have a solution to this?
Bonus: VBA seems to have little capability for multi threading/processing, but if anyone can think of a way to run all these processes at once I'd love to know about it
The implementation details are quite involved for each of the things you are looking for but they can be easily found by searching. So I'll suggest an approach for each of your questions.
Use Application.OnTime to trigger a macro to run after a given time interval.
Multiple threads can be executed, but it's tricky. You'll need to either
Invoke another instance of Excel and make it run your Macro, that way your main thread is not affected or
Put your macro into a separate text file (vb-script file) and call get it executed via Shell. That way you can continue executing the existing Macro and the user regains control of the UI, but the script fetches new data in the background.
Execute part of your code in a separate instance of Excel and then pump in the results into your current spreadsheet via DDE (older but fairly reliable technology) or by building something called an RTD server.
Ample examples of each of the above points are available on this site and elsewhere through Google.

VB Continuously Transfer/dispaly data from a file to another

My friend and I (i'm only helping) are writing a code in VB that continuously get voltage and display it; that part works correctly. The next time is to continuously transfer it to another file and have it display it. Although we can transfer the data and display it, it doesn't display continuously, we have to refresh it(click start then stop on and on) in order to view the data. Any help in figuring how to fix this is greatly appreciated, and I apologize for not posting the code as we are not allowed to.
Consider using a timer to refresh the display.
I can't help much more without more context for the technology used for the UI.

VBA maintaining the program in memory

Sorry I don't know if this is something simple, or even where the problem fits in the greater scheme of programming.
So in my unsophisticated ways, my programs have always been of the scheme: 1. start program, 2. wait while program runs, 3. program is done and gone.
What I am doing now is creating a table from a long list of transactions (10,000s of). The table has several combo boxes for the user to select filters. Right now, every time the user changes a filter, the entire log is re-processed, which takes half a minute or a minute.
What I would rather do is have the trade log held in memory, or somehow latently but more immediately available. But not have the program "spinning" in the background. So the user could go about using Excel unaware that the program is ready in the background in case they want to update the table later, or not.
Does that make sense? If it can't be done in VBA, I'd still be curious how it would be done in another environment, say C#, if it could be. Thanks.
If the frequency of updates to the options trade is low enough you could separate reading and processing the option trades from the filtering process:
Step 1 - Refresh - read the logs and process them, storing the results in global containers (arrays, collections, dictionaries, objects ...)
Step 2 - User requests - show form - user chooses filters - show/store results extracted from the global containers.
There are several options
Firstly, is the code correctly structured? For example, do you really need to re-process everything or can a re-write be more efficient?
If you cannot avoid resource intensive code, notify the user with a progress bar or message. Also consider the use of DoEvents which frees up the operating system so that Excel can process other events.
DoEvents is slow and dirty. Even better look at this link DoEvents is slow!!! Here are faster methods
Rewrite your code to work asynchronously. Create a class, a handler and deal with each transaction asynchronously.
You could write some VBScript/Javascript and push the task out to run independently of Excel/VBA. Eg there's an example Here
Don't use VBA :)
Edit: How are you filtering? If you're iterating through thousands of items in an array testing for criteria it can be very slow. Excel's Advanced Filter is very quick and could process hundreds of thousands of rows with multiple criteria quickly.
When a macro in Excel VBA runs, the user cannot use Excel anymore, running the VBA "stucks" the whole program.
Here are a few tips to find a workaround for your problem :
Keep the vba running : load the data a first time when launching the combobox and then display results to the user every time he asks for but keep a combobox loaded so that vba keeps its context and memory
Load the data in Excel Worksheet, even hidden and then use it when the user asks for some data
Give us more info on what you are doing, from where you are loading the data, how you can cache it, what is your current code, what you tried... so that we can help you more
Regards,
Max

How can I protect a process I start from within my vb.net program?

I've created a small application that basically reads and writes to a single Excel.exe process. It's basically a timer that records the time I use on projects and then store it in an Excel sheet. This works great, however, I've noticed that if I open Excel manually, work on some sheets and whatnot, save and exit etcetc, the process my software use gets broken or something. The same thing that happens if I manually close the excel.exe process and my software doesn't "know".
So I was wondering if it's possible to protect the excel.exe process somehow? To make sure it can't be closed or tampered with in the meantime?
Let me suggest an alternative approach that does not require you to have an Excel process running all the time (after all, this also consumes a lot of system resources):
Let your application record your information. Every now and then -- for example, after a work entry has been finished or a specific time has elapsed -- open the Excel sheet, write the data, and close it again (also closing the Excel process that you are automating). This save operation should not take more than a few seconds and it will (mostly) prevent the problem you are experiencing.
In fact, since Office automation is always a bit painful, an even better way would be to output your data without requiring an Excel process. To do this, you could use
one of the third-party Excel libraries available for .net,
a CSV or HTML file, which can be opened by Excel, or
open the Excel file as a database with ADO.NET.
You cannot protect a process, but you can check the process.HasExited property to find out whether the process has terminated and take action based on that.
Add an exception handler. Either call non-throw methods if possible.