vb.net - Simultaneous OleDbDataAdapter.Fill Calls on Separate Threads? -
first timer here, go easy on me. theoretically possible execute 2 oledbdataadapter.fill calls on separate threads simultaneously - or fundamentally flawed?
consider form 2 buttons , 2 datagridviews. each button click launches worker thread using async \ await \ task.run pattern calls method return populated datatable , assigns 1 of datagridviews. .fill in first thread takes 30 seconds complete. .fill in second thread takes 1 second complete. when launched individually, both buttons work expected.
however, if launch first worker thread (30 seconds fill), launch second thread (1 second fill), second datagridview not populated until first .fill call completes. expect second datagridview populate in 1 second, , first datagridview populate ~30 seconds later.
i have duplicated issue in sample code both oledbdataadapter , sqldataadapter. if replace long running query simple thread.sleep(30000), second datagridview populated right away. leads me believe not issue design pattern, rather specific issuing .fill calls simultaneously.
private async sub ultrabutton1_click(sender object, e eventargs) handles ultrabutton1.click dim args new getdataarguments args.connectionstring = "some connection string" args.query = "select longrunningquery table" dim dt datatable = await task.run(function() filldatatable(args)) if datagridview1.datasource nothing datagridview1.datasource = dt else ctype(datagridview1.datasource, datatable).merge(dt) end if end sub function filldatatable(args getdataarguments) datatable dim ds new datatable using connection new oledbconnection(args.connectionstring) using dbcommand new oledbcommand(args.query, connection) using dataadapter new oledbdataadapter(dbcommand) dataadapter.fill(ds) end using end using end using return ds end function private async sub ultrabutton2_click(sender object, e eventargs) handles ultrabutton2.click dim ds datatable = await task.run(function() loadseconddgv("1234")) datagridview2.datasource = ds end sub function loadseconddgv(pnum string) datatable dim dx new datatable using xconn new oledbconnection("some connection string") using dataadapter new oledbdataadapter("select name products pnum = """ & pnum & """", xconn) dataadapter.fill(dx) end using end using return dx end function
this depends on data source is. data sources (like excel) allow 1 connection @ time. other data sources (like access) allow multiple connections, fulfill results in serial, such don't gain anything. other data sources, sql server, allow true parallel activity you're looking for.
in case, mention tried sqldataadapter, indicates me you're talking sql server, , should possible. what's going on here first query locking of data need second query. can past changing transaction isolation level or through careful use of with (nolock)
hint (the former option preferred).
one other thing keep in mind can work if you're using separate connection each query, or if you've enabled multiple active result sets feature. looks you're using separate connection objects here, should fine, it's still thought worth bringing up.
finally, need comment on filldatatable()
method. method requires provide completed sql string, practically forces write code horribly vulnerable sql injection attacks. continuing use method shown practically guarantees app hacked, sooner rather later. need modify method encourages use parameterized queries.
Comments
Post a Comment