Data access with thread


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Data access with thread

  1. #1
    Edwin Guest

    Data access with thread


    I want to make a program that retreive data from sql server. Currently, i'm
    using sqlDataReader, dataTable and dataGrind. I try to use thread so the
    program is not locked during the search task and the user have a control
    to stop the search task. In this thread, sqlDataReader search the data from
    sql server and pass it to dataTable. The problem came when i try to bind
    this dataTable to dataGrid. If I bind the datagrid in new() constructor,
    or in form1_load method, i got an error in Form1 (main form) when i execute
    the search task. I also try to make an event in thread, so that when the
    thread has been finished searching data or search is cancelled, then it raise
    an event that pass the dataTable. But when i want to bind this dataTable
    to dataGrid, i got an error which tell me that i can't bind my datagrid
    with an object from different thread. What is i have to do so that i can
    run data access with thread? Is there any other way to get data from server
    while still having a control to cancel it? Thanks.

  2. #2
    Phil Weber Guest

    Re: Data access with thread

    > when I want to bind this dataTable to dataGrid, I got an
    > error which tell me that I can't bind my datagrid with an
    > object from different thread.


    Edwin: See the thread titled, "Threading and the .NET Datagrid" in this
    discussion group:
    http://news.devx.com/cgi-bin/dnewswe...nical&xrelated
    =7680&cmd_related.x=1
    ---
    Phil Weber



  3. #3
    Edwin Guest

    Re: Data access with thread


    "Phil Weber" <pweber@nospam.fawcette.com> wrote:
    > > when I want to bind this dataTable to dataGrid, I got an
    > > error which tell me that I can't bind my datagrid with an
    > > object from different thread.

    >
    >Edwin: See the thread titled, "Threading and the .NET Datagrid" in this
    >discussion group:
    >http://news.devx.com/cgi-bin/dnewswe...nical&xrelated
    >=7680&cmd_related.x=1
    >---
    >Phil Weber
    >
    >



    >Windows forms controls are NOT threadsafe. You'll have to marshall
    >calls from a different thread back to the thread that created the
    >control. See:


    >http://samples.gotdotnet.com/QuickSt.../default.aspx?
    >url=/quickstart/howto/doc/WinForms/WinFormsThreadMarshalling.aspx


    Thanks. Marshall calls has been solve my problem to bind data from other
    thread. But now the thread act just like there is no thread at all. My program
    still lock during the search task (but now i can update my datagrid in this
    thread). Did I miss something about marshall calls to make it act just like
    ordinary thread (release the resource)? I try to make the thread sleep in
    search loops, but it still didn't release the resource. I just want to make
    user have a control to stop the search task. How suppose i do this task?


  4. #4
    Edwin Guest

    Re: Data access with thread


    "Edwin" <edwin.arif@chek.com> wrote:
    >
    >"Phil Weber" <pweber@nospam.fawcette.com> wrote:
    >> > when I want to bind this dataTable to dataGrid, I got an
    >> > error which tell me that I can't bind my datagrid with an
    >> > object from different thread.

    >>
    >>Edwin: See the thread titled, "Threading and the .NET Datagrid" in this
    >>discussion group:
    >>http://news.devx.com/cgi-bin/dnewswe...nical&xrelated
    >>=7680&cmd_related.x=1
    >>---
    >>Phil Weber
    >>
    >>

    >
    >
    >>Windows forms controls are NOT threadsafe. You'll have to marshall
    >>calls from a different thread back to the thread that created the
    >>control. See:

    >
    >>http://samples.gotdotnet.com/QuickSt.../default.aspx?
    >>url=/quickstart/howto/doc/WinForms/WinFormsThreadMarshalling.aspx

    >
    >Thanks. Marshall calls has been solve my problem to bind data from other
    >thread. But now the thread act just like there is no thread at all. My >program

    still lock during the search task (but now i can update my >datagrid in this
    thread). Did I miss something about marshall calls to >make it act just like
    ordinary thread (release the resource)? I try to >make the thread sleep in
    search loops, but it still didn't release the >resource. I just want to make
    user have a control to stop the search >task. How suppose i do this task?

    I'm looking at microsoft product service control just like phil told. Maybe
    there is an answer for me about my problem. I'm a newbie in threading, i
    just hope this thread doesn't kill me. Arrggghh... I hate threading.

  5. #5
    Edwin Guest

    Re: Data access with thread


    "Edwin" <edwin.arif@chek.com> wrote:
    >
    >"Edwin" <edwin.arif@chek.com> wrote:
    >>
    >>"Phil Weber" <pweber@nospam.fawcette.com> wrote:
    >>> > when I want to bind this dataTable to dataGrid, I got an
    >>> > error which tell me that I can't bind my datagrid with an
    >>> > object from different thread.
    >>>
    >>>Edwin: See the thread titled, "Threading and the .NET Datagrid" in this
    >>>discussion group:
    >>>http://news.devx.com/cgi-bin/dnewswe...nical&xrelated
    >>>=7680&cmd_related.x=1
    >>>---
    >>>Phil Weber
    >>>
    >>>

    >>
    >>
    >>>Windows forms controls are NOT threadsafe. You'll have to marshall
    >>>calls from a different thread back to the thread that created the
    >>>control. See:

    >>
    >>>http://samples.gotdotnet.com/QuickSt.../default.aspx?
    >>>url=/quickstart/howto/doc/WinForms/WinFormsThreadMarshalling.aspx

    >>
    >>Thanks. Marshall calls has been solve my problem to bind data from other
    >>thread. But now the thread act just like there is no thread at all. My

    >program
    >still lock during the search task (but now i can update my >datagrid in

    this
    >thread). Did I miss something about marshall calls to >make it act just

    like
    >ordinary thread (release the resource)? I try to >make the thread sleep

    in
    >search loops, but it still didn't release the >resource. I just want to

    make
    >user have a control to stop the search >task. How suppose i do this task?
    >
    >I'm looking at microsoft product service control just like phil told. Maybe
    >there is an answer for me about my problem. I'm a newbie in threading, i
    >just hope this thread doesn't kill me. Arrggghh... I hate threading.



    Thanks again... that article is really helping me with threading. But now
    new problem is coming. I already try to search it from framework sdk quickstart
    and documentation, but i still can't find any reference how i can solve this
    problem. Just like i said before, i want to make a program that search database
    while user still have a control to cancel the operation. The problem is I
    can't just stop closing my dataReader, looks like it always read through
    the database in server until it finished searching. This is the code looks
    like :

    ....
    dim myDataTable as DataTable
    dim myDataReader as DataReader
    dim isCancelled as boolean
    Dim CallDataBindToDataGrid As New MethodInvoker(AddressOf _ Me.DataBindToDataGrid)

    public sub QueryDataBase(){
    .....
    dim strsql as string = "SELECT * FROM Table"
    Dim cmd As New SqlCommand(strSQL, myConnection)
    myDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    While isCancelled = False AndAlso myDataReader.Read = True
    'fill myDataTable
    ......
    end while
    Me.BeginInvoke(CallDataBindToDataGrid)
    }

    Public Sub DataBindToDataGrid()
    DataGrid1.DataSource = myDataTable
    DataGrid1.Update()
    myDataReader.Close() 'This is where the problem happened
    myDataReader = Nothing
    myDataTable = Nothing
    End Sub

    ....

    If user click button btnCancel, then isCancelled = true (out of loop). But
    when the program reach code "myDataReader.close()", it just like we still
    have to wait until the search is completed. Of course with a lot of data
    in database, it really consume a lot of time. If i didn't use myDataReader.close(),
    the program runs just like i want it to. But i only can search one time,
    because when i want to search again, i got an error that tell me dataReader
    is open. How can i use this dataReader the way i want it to? Or maybe there
    is another way to retreive data from database and we can just cancel the
    operation if we want it to? Thanks...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center