0

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()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
alex
  • 1,661
  • 2
  • 12
  • 5

2 Answers2

2

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
1

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
gillonba
  • 897
  • 9
  • 24
  • When I do a loop, the program still stops responding, thats what I'm trying to avoid. – alex Sep 03 '11 at 20:06
  • You need to put your code in another thread. Try using a BackgroundWorker – gillonba Sep 03 '11 at 20:11
  • It sounds like you are using Visual Studio. IN your forms designer, int the toolbox, you should find the BackgroundWorker under "all windows forms". Create a "DoWork" event handler and put your code in there. To start the background worker, call "RunWorkerAsync" on the background worker instance (ex: backgroundworker1.RunWorkerAsync()) – gillonba Sep 03 '11 at 20:59
  • Thank you, however I am having one more error - I am getting an error saying 'Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.' Is it fixable? – alex Sep 03 '11 at 21:52
  • You can't update form elements from a thread other than the one they were created on. That means you will have to find a way to update the UI on the main thread. You may find some useful suggestions here: http://stackoverflow.com/questions/5037470/cross-thread-operation-not-valid – gillonba Sep 04 '11 at 15:09