vb.net - Main form freezing due to backgroundwork -
scenario: making login form connects mysql database account , password confirmation.
trying: trying show animated loading gif , loading text while form connecting , getting result database.
did far: used background worker: login button:
private sub loginbtn_click(byval sender system.object, byval e system.eventargs) handles loginbtn.click picturebox2.visible = true label6.visible = true backgroundworker1.runworkerasync() end sub
background worker dowork:
private sub backgroundworker1_dowork(sender object, e doworkeventargs) handles backgroundworker1.dowork accesscontrol() end sub private sub accesscontrol() if me.invokerequired me.invoke(new methodinvoker(addressof accesscontrol)) else con.open() dim user, pass string user = usernamebox.text pass = crypt(passwordbox.text) cmd = new mysqlcommand("select * users name ='" + user + "' , password ='" + pass + "'", con) dr = cmd.executereader end if end sub
background runworkercompleted:
private sub backgroundworker1_runworkercompleted(sender object, e runworkercompletedeventargs) handles backgroundworker1.runworkercompleted if (dr.read()) fadingform() else picturebox2.visible = false label6.visible = false wrnpnl.visible = true wrnlbl.visible = true wrnpnlimg.visible = true passwordbox.text = "" usernamebox.text = "" usernamebox.focus() end if con.close() end sub
what should happen: after entering credentials when user clicks login button animated loading gif , loading text should appear , background process should start, if credentials right fadingform() should fired or if not else part should.
problem: problem when press login button after entering corrent credentials, loading gif , text shows form freezes. , after process completed main form gets normal. sure form freezes animated gif stops animating , non of controls usable. have no idea possible reason or how can fixed.
any appreciated. ton!
the first thing inside background worker force work ui thread. why it's hanging ui.
if me.invokerequired me.invoke(new methodinvoker(addressof accesscontrol))
you should invoke access ui, not sql work. since need access username , password fields should pass them arguments, or pull them out of controls , in local variables before background worker begins. way can access values without having invoke anything.
declare user , pass @ form level and, inside click event.
user = usernamebox.text pass = crypt(passwordbox.text)
Comments
Post a Comment