Multi threading question - mysql

I'm trying to make a server program for a game, I need to run 3 extra threads in a windows form app. all the threads needs to be able to access the same variables, (thread1: is going to be pulling user commands from my database and executing them, thread2: is going to be updating all the users data and thread3: is going to be sending all the new variables back to the database.) I've looked into background threads and I'm not sure if thats going to do what i want them todo (they look like they could run my code but it looks like they would have a hard time working with the same variables for the all the threads.)
So i guess my question is, is this possible and what is best way to accomplish this, would background workers and invoke do the trick? what problems would i be looking at doing that, anyone have any better idea.
On a side note i had another question thats related ive tried a bunch of times to accomplish this using just simple threads. Now from what i understand doing all of my work on another thread should not lock up my ui, however it does. maybe im doing thread the wrong way, my code looks something like
dim newthread as new system.threading.thread(addressof runmycode)
newthread.start()`
this code isn't exact but why would using threading like this lock up the UI? Shouldn't it be running separately?

You need to create a 'new' instance of the object class to be 'threaded' and expose the function you want to call, e.g.
Public Class MyNewCode
Public Function runmycode()
' My code goes here '
End Function
End Class
Public Class frmMain ' or under an onClick event or whatever.... '
Dim s As New MyNewCode ' This is the class above '
t = New System.Threading.Thread(AddressOf s.runmycode) ' This is the function above '
t.start()
End Class

Here's a tutorial I wrote a little while back that walks you through creating a basic multi-threading program in VB2008. With VB2010 you can remove the delegates if you want but the idea is pretty much the same.
Creating a simple multi-threaded VB.Net application

Related

Relative name to get information from Form in MS Access

I would like to know if it is possible in a Saved Query on MS Access to use WHERE clause getting value from a Form independent of MS Access installed language?
In a MS SQL QUERY WHERE clause that should be equal to a text in a form, I wrote it like this
= Form!Form_name!text_name
This work fine if all user have MS English version installed, however, this is not true in my case, as some users have a Portuguese version. In their version, the right way to call the same syntax should be:
= Formulários!Form_name!text_name
So, the problem is that Access does not recognize this considering language version. Due to this, if a user with Portuguese version try to run the query, it will pop up an insert box.
I've look throughout many sources but none could help me to solve this.
Thank you all in advance!
I can't test this as I don't have a portuguese version of Access but I avoid calls to form parameters because they are brittle. For instance the form has to be open or the query fails in wierd ways. If I must use a form's parameter I wrap the parameter in a public function (usually in that forms code behind). A public function usually gets picked up by intellisense even in the designer but a public variable does not.
My style is to synthesize a property with a private global scope variable and public get and set functions. Then in the designer you can call: get-myforms-property instead of Form!Form_name!text_name. When the form opens and closes I usually call set-myforms-property and set it to be equal to the actual form property.
In this way you also get the advantages of wrapping. For instance you can choose what value to return when the form is closed rather than just having the query fail.

Adobe AIR Sqlite assync transactions

Here is the problem that I want to implement a solution, I have some data that needs to be writen in a database,data comes from network and it could arrive in "parallel", also for performance reasons the code is using async connection. The problem is that I have some bugs in my implementation ,I want to rewrite this code so it also uses transactions and rolloback,handle all possible errors and the code should be clean and easy to understand.
I am unable to find example code to read to get inspired or something to use directly, looking on what others done would help a lot.
So anyone know or has such example code?
I think I need to write some code that will allow me to chain this methods :begin->execute->commit->end transaction and in case of errors rollback
Update
what I want to find or implement is something like
Update2 so where things get confusing is when you want to insert an array of stuff, you will need to use a loop and then use the execute method async, and then when it succeds insert the next element and so on, this makes it a little more complicated especialy if I want to abstract this ito have all this section of code in a single place and not all over my code.
function executeAssyncAsTransaction(statement:SqlStatement,responder:Responder):void{
//step 1 begin transaction
//step 2 execute
//step 3 commit
//step 4 end transaction
//step 5 handle errors and rollback
//use responders above no event listeners
}
I think I know how to implement this but I know that there is a chance I do it wrong (my current implementation that is in production crashes sometimes on some machines so I I know is hard to write 100% error free code)
I am, also thinking to implement a queue that will store my statements if needed(if database is busy) so I don't get an error and have to try again later
Use event listeners, error and callback functions in Adobe Air for JavaScript like this:
var conn = new air.SQLConnection();
conn.addEventListener(air.SQLEvent.OPEN, openHandler);
conn.addEventListener(air.SQLErrorEvent.ERROR, errorHandler);
// The database file is in the application storage directory
var folder = air.File.applicationStorageDirectory;
var dbFile = folder.resolvePath("DBSample.db");
conn.openAsync(dbFile);
function openHandler(event)
{
air.trace("the database was created successfully");
}
function errorHandler(event)
{
air.trace("Error message:", event.error.message);
air.trace("Details:", event.error.details);
}
For ActionScript read these ressources:
http://de.slideshare.net/peterelst/introduction-to-sqlite-in-adobe-air-1627545

Alternative to global variables

Hi I am doing a simple script where I want to track what step I am up to and use the result from a button click handler.
1)I cannot pass the variable as it is an event
2)Cannot use global variables as they seem to be constants only once set
Is there any way to set and object or variable multiple times and access the current value from within a handler function?
Found several examples suggesting a hidden widget, as well as that being a poor solution I also struggled to retrieve the value once set. IE it had a .setValue but no .getValue
Help please this is not a difficult thing in any other language I have tried but new to GAS
Cheers
There are more options - one, as you mentioned is to use a hidden widget. Although there is no .getValue(), it can be accessed through e.parameter within the click handler.
Two, for small amounts of data, you can use ScriptProperties / UserProperties and CacheService
Third, you can use the script DB or a spreadsheet if you are dealing with large amounts of data.
Having said all this, it would be better if you can post some code of what you're trying to achieve. Many times, code speaks louder than words.
Private Cache is intended for this type of thing https://developers.google.com/apps-script/reference/cache/

How to wait for MySQL To Update in VB.NET?

I have been working on something that checks an MySQL Database to check something - however the program stops responding because it is constantly checking the database. Is it possible to have it wait a few seconds to recheck the database? I have tried sleep() but it is giving a strange error:
A call to PInvoke function
'Game!WindowsApplication1.Form1::Sleep' has unbalanced the
stack. This is likely because the managed PInvoke signature
does not match the
unmanaged target signature. Check that the calling convention
and parameters of the
PInvoke signature match the target unmanaged signature.
I have been looking into this for quite a while and i am in a predicament. I do need the MySQL databases to be checked very often. I tried making a web browser refresh before checking it again - but it started to lag the application.
Code:
function updateit()
' SQL Code goes here, it succeeds.
updateit() ' Update it again.
return true
end
updateit()
Your code example shows a recursive function with no base case. The result of that is always a stack overflow (an uncatchable exception in .Net).
Don't call your updateit() function from within the function itself. Instead, just write a loop to call it over and over.
Try doing your checks from a separate thread. Try dragging a BackgroundWorker onto your form and putting your check in that to make your program more responsive. I've never seen that error before though. Is it System.Threading.Thread.Sleep() or something specific to VB?
Looking at your code it looks like you've got infinite recursion. That will cause a stackoverflow... try
while(true)
'SQL code
end

How can I save a global variable value after dynamically loading a reference?

I have a Access 2003 database that will dynamically load MDB databases as a library reference. The reason for this is this database is a menu front-end for 60+ application databases. Rather than deal with permanently referencing all these databases, the menu front-end will dynamically reference what is needed when the user makes a selection. I was working on moving this database to Access 2010 and creating a custom ribbon. I started using the technique from here to capture the ribbon object in a global variable when the ribbon loads. I then ran into the problem where I could verify the code was running and the global variable was correctly being assigned the ribbon reference but after the database would run through it's startup routine, that global variable would get reset to Nothing.
To verify what was going on, I created a simple database for testing. In this database, I had a module with a global variable:
Public obj as Object
I then had a function like this:
Public Function SetObj()
Set obj = Application
Debug.Print "IsNothing=" & (obj Is Nothing)
References.AddFromFile "Test.mdb"
Debug.Print "IsNothing=" & (obj Is Nothing)
End Function
Obviously, in my code, "Test.mdb" refers to an actual file. If I run this code, Debug.Print gives me "IsNothing=False" for both instances, but after the function finishes and if I wait a couple seconds, Debug.Print will give me "IsNothing=True". If I comment out References.AddFromFile, Debug.Print gives me "IsNothing=False" no matter how long I wait.
It makes sense to me that since Access has to re-compile the VBA code after loading the library that all global variables are reset. I've experimented with moving the global variable into a class, but since I then need a global variable for the class, the class variable then gets reset instead. I tried using a local variable in the function to save the value of the global variable, but it looks like Access waits a couple seconds after the code is finished running to do the re-compile, so that doesn't work either. Does anyone have any other ideas to accomplish this?
I don't really know if this will solve the problem for this kind of reference, but in general, I don't use public variables for this kind of thing, but instead use a STATIC variable inside your function. It would be something like this:
Public Function SetObj() As Object
Static obj As Object
If (obj Is Nothing) Then
Set obj = Application
End If
Set SetObj = obj
End Function
Then you'd just use SetObj as an object for using your application. In a production app, you'd need tear-down code, too, but I've omitted that here.
I doubt this helps, but your code struck me as rather inefficient and incomplete.
I figured out a solution to my problem, and thanks #David-W-Fenton, as your answer gave me the idea. I use your approach in a library database for caching frequently-accessed values that are stored in a table but don't change after the initial startup. Those values aren't lost every time the references change, and that's when the light bulb lit up.
The solution is to put the global variable in a library database. Access looks to be only resetting global variables in the database that the reference is being loaded into - which makes sense after thinking about it. So since the library database isn't the one being re-compiled, it doesn't get it's global (or private or static) variables reset.
What I ended up doing was creating a new module in an existing library database. It has a private variable and two methods - one to set the variable, one to retrieve the variable value. In my menu front-end database, when the ribbon loads and calls my callback function, rather than saving the ribbon object in the front-end database, I pass it to this module for saving. I now no longer lose that ribbon reference whenever new databases are added to the library references on the fly.