WinForm and Multi threads

Discussion in 'Microsoft Dot NET Framework Forms' started by rockdale, Oct 17, 2009.

  1. rockdale

    rockdale Guest

    Hi, All:

    If there is a more proper group for multi threads, let me know please.

    I have a winform application written in C#.net with the capability to
    work offline. If there is a network connection, then the application
    will connect to the central database and retrieve data from/save data
    into the central database. If there is no network connection, then the
    application will save the data to a local xml file.

    Here are my questions.

    1. Is there an easy way to detect that the network is disconnected.
    More specifically, the database is disconnected? Do I need to keep try
    to select from a table and wait to the failed result to know it is
    disconnected?

    2. When the network is broken and the appl tries to connect to the
    database, it will take a long time to get the failed connection from
    database. the GUI will not respond in this period. I am thinking using
    another thread to do all the database work.

    3. With another threads doing all the database work, I am from C++
    world, in C++ we have postmessage so that we can communicate between
    two thread so that the GUI can send a message to the database thread
    to retrieve data. And the when the database thread finished retrieve
    data, it will send the result back to the GUI thread so the GUI
    thread can handle the result. But, in C#, how do we implement this
    kind of communication?

    I search the internet and some example just have the worker thread
    doing some sample work without communicate with the main thread at
    all. What I need to know is How the main thread ask the worker thread
    to do some task with parameters passed to the worker thread. Once the
    worker finished the task, how does the work thread inform the main
    thread the work is done and send the result back to main thread to
    process.

    Any examples /links will be appreciated.

    Thanks a lot
    -Rockdale
     
    rockdale, Oct 17, 2009
    #1
    1. Advertisements

  2. rockdale

    Patrice Guest

    Hi,

    > 1. Is there an easy way to detect that the network is disconnected.


    See :
    http://blogs.msdn.com/adarshk/archive/2004/12/16/323162.aspx to test for
    network connectivity. You'll likely still have to what you do at this time
    for db connectivity (but in almost all cases I suppose it should work) as
    most often the real issue is the network not the db.

    > 2. When the network is broken and the appl tries to connect to the
    > database, it will take a long time to get the failed connection from
    > database. the GUI will not respond in this period. I am thinking using
    > another thread to do all the database work.


    It's likely a connection timeout. Testing for network first allow to prevent
    the db driver to run into this (unless the db is really unavailable because
    of some other issue).

    > 3. But, in C#, how do we implement this
    > kind of communication?


    Some more details could be needed. In general ADO.NET provides support for
    async db calls. See http://msdn.microsoft.com/en-us/library/hz8wbs6k.aspx

    --
    Patrice
     
    Patrice, Oct 17, 2009
    #2
    1. Advertisements

  3. rockdale

    rockdale Guest

    HI, Patrice:

    Thanks for the reply. The network available change event is very
    helpful to my situation.

    The ado.net async calls is also an interesting feature. But it is kind
    not what I wanted.

    The GUI is a thread - call it GUIthread, the appl will sprawl another
    thread - let's call it DBthread.

    Say if user clicked a button to retrieve data(suppose this will take a
    very long time), instead of do the data retrieving in the GUIthread -
    which the GUI will freeze to wait for the data to return. I want to
    send an event/message to the dbthread let it does the data retrieving.
    and now GUIthread still can respond to end user. once the dbthread
    finished retrieving, the dbthread will send an event/message back
    GUIthread, inform GUIthread that the data is ready, the GUIthread then
    can process the data.

    I do not know how to pass data (query parameters and query result)
    between those 2 threads, I do not know how GUIthread can send an event/
    message to signal DBThread to start the task.

    in MFC world, we have PostMessage so it is not a problem to me.
    In C#, I can see lot of samples using delegate, but I am not exactly
    know how it works.

    Again, thanks for the links. It is very helpful.

    -Rockdale



    On Oct 17, 12:35 pm, "Patrice" <http://scribe-en.blogspot.com/> wrote:
    > Hi,
    >
    > > 1. Is there an easy way to detect that the network is disconnected.

    >
    > See :http://blogs.msdn.com/adarshk/archive/2004/12/16/323162.aspxto test for
    > network connectivity. You'll likely still have to what you do at this time
    > for db connectivity (but in almost all cases I suppose it should work) as
    > most often the real issue is the network not the db.
    >
    > > 2. When the network is broken and the appl tries to connect to the
    > > database, it will take a long time to get the failed connection from
    > > database. the GUI will not respond in this period. I am thinking using
    > > another thread to do all the database work.

    >
    > It's likely a connection timeout. Testing for network first allow to prevent
    > the db driver to run into this (unless the db is really unavailable because
    > of some other issue).
    >
    > > 3. But, in C#, how do we implement this
    > > kind of communication?

    >
    > Some more details could be needed. In general ADO.NET provides support for
    > async db calls. Seehttp://msdn.microsoft.com/en-us/library/hz8wbs6k.aspx
    >
    > --
    > Patrice
     
    rockdale, Oct 18, 2009
    #3
  4. rockdale

    Patrice Guest

    Do you mean you are looking for a producer/consumer model such as
    http://www.java2s.com/Tutorial/CSharp/0420__Thread/Producerandconsumer.htm ?

    Else try http://msdn.microsoft.com/en-us/library/9xyf641a.aspx in which you
    could find what you are used to. You have also one or two more things that
    could help (such as the BackgroundWorker component that is easy to use and
    could be enough for simple non blocking UI needs).

    Ofr now I thought you were creating something from the ground up using C# or
    do you intract with something else in whihc case the design has some
    constraints on it ?
    --
    Patrice


    "rockdale" <> a écrit dans le message de
    news:...
    HI, Patrice:

    Thanks for the reply. The network available change event is very
    helpful to my situation.

    The ado.net async calls is also an interesting feature. But it is kind
    not what I wanted.

    The GUI is a thread - call it GUIthread, the appl will sprawl another
    thread - let's call it DBthread.

    Say if user clicked a button to retrieve data(suppose this will take a
    very long time), instead of do the data retrieving in the GUIthread -
    which the GUI will freeze to wait for the data to return. I want to
    send an event/message to the dbthread let it does the data retrieving.
    and now GUIthread still can respond to end user. once the dbthread
    finished retrieving, the dbthread will send an event/message back
    GUIthread, inform GUIthread that the data is ready, the GUIthread then
    can process the data.

    I do not know how to pass data (query parameters and query result)
    between those 2 threads, I do not know how GUIthread can send an event/
    message to signal DBThread to start the task.

    in MFC world, we have PostMessage so it is not a problem to me.
    In C#, I can see lot of samples using delegate, but I am not exactly
    know how it works.

    Again, thanks for the links. It is very helpful.

    -Rockdale
     
    Patrice, Oct 18, 2009
    #4
  5. rockdale

    Norman Yuan Guest

    Have you looked at BackgroundWorker component that is available in Win Form
    designer's toolbox since VS2005?

    It does exactly what you want: runs another thread from win form UI. You
    simply use this component to kick off another thread to accessing database
    and when the data access thread returns, you update the UI with data from
    database.


    "rockdale" <> wrote in message
    news:...
    HI, Patrice:

    Thanks for the reply. The network available change event is very
    helpful to my situation.

    The ado.net async calls is also an interesting feature. But it is kind
    not what I wanted.

    The GUI is a thread - call it GUIthread, the appl will sprawl another
    thread - let's call it DBthread.

    Say if user clicked a button to retrieve data(suppose this will take a
    very long time), instead of do the data retrieving in the GUIthread -
    which the GUI will freeze to wait for the data to return. I want to
    send an event/message to the dbthread let it does the data retrieving.
    and now GUIthread still can respond to end user. once the dbthread
    finished retrieving, the dbthread will send an event/message back
    GUIthread, inform GUIthread that the data is ready, the GUIthread then
    can process the data.

    I do not know how to pass data (query parameters and query result)
    between those 2 threads, I do not know how GUIthread can send an event/
    message to signal DBThread to start the task.

    in MFC world, we have PostMessage so it is not a problem to me.
    In C#, I can see lot of samples using delegate, but I am not exactly
    know how it works.

    Again, thanks for the links. It is very helpful.

    -Rockdale



    On Oct 17, 12:35 pm, "Patrice" <http://scribe-en.blogspot.com/> wrote:
    > Hi,
    >
    > > 1. Is there an easy way to detect that the network is disconnected.

    >
    > See :http://blogs.msdn.com/adarshk/archive/2004/12/16/323162.aspxto test
    > for
    > network connectivity. You'll likely still have to what you do at this time
    > for db connectivity (but in almost all cases I suppose it should work) as
    > most often the real issue is the network not the db.
    >
    > > 2. When the network is broken and the appl tries to connect to the
    > > database, it will take a long time to get the failed connection from
    > > database. the GUI will not respond in this period. I am thinking using
    > > another thread to do all the database work.

    >
    > It's likely a connection timeout. Testing for network first allow to
    > prevent
    > the db driver to run into this (unless the db is really unavailable
    > because
    > of some other issue).
    >
    > > 3. But, in C#, how do we implement this
    > > kind of communication?

    >
    > Some more details could be needed. In general ADO.NET provides support for
    > async db calls. Seehttp://msdn.microsoft.com/en-us/library/hz8wbs6k.aspx
    >
    > --
    > Patrice
     
    Norman Yuan, Oct 18, 2009
    #5
  6. rockdale

    Guest


    > I search the internet and some example just have the worker thread
    > doing some sample work without communicate with the main thread at
    > all. What I need to know is How the main thread ask the worker thread
    > to do some task with parameters passed to the worker thread. Once the
    > worker finished the task, how does the work thread inform the main
    > thread the work is done and send the result back to main thread to
    > process.
    >
    > Any examples /links will be appreciated.


    The most adequate alternative in the .NET world for
    - SendMessage(hwnd, ...) is Control.Invoke( ... )
    - PostMessage(hwnd, ...) is Control.BeginInvoke( ... )

    Both functions take a delegate which are ensured to run in the context
    of the thread which created the control/form. Just check MSDN or
    Google for "Invoke".
     
    , Oct 19, 2009
    #6
  7. rockdale

    rockdale Guest

    Guys, thanks for all the reply and I think backgroundworker is what I
    am looking for.

    Thanks
    -Rockdale
     
    rockdale, Oct 19, 2009
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    Saving a WinForm and its controls properties from run to run

    Guest, Dec 18, 2003, in forum: Microsoft Dot NET Framework Forms
    Replies:
    1
    Views:
    201
  2. arkam

    Winform with background image and transparent usercontrols

    arkam, Jan 26, 2004, in forum: Microsoft Dot NET Framework Forms
    Replies:
    0
    Views:
    1,367
    arkam
    Jan 26, 2004
  3. Guest

    invokes and threads and pumps...oh my..

    Guest, May 10, 2005, in forum: Microsoft Dot NET Framework Forms
    Replies:
    1
    Views:
    167
    Francisco Padron
    May 10, 2005
  4. Guest

    Multi-coloured text in a winform application

    Guest, May 11, 2005, in forum: Microsoft Dot NET Framework Forms
    Replies:
    2
    Views:
    290
  5. Steve B.

    .Net 2.0 Pb with multi threads and DataSet dataBinding

    Steve B., Feb 26, 2007, in forum: Microsoft Dot NET Framework Forms
    Replies:
    4
    Views:
    230
    Steve B.
    Mar 6, 2007
Loading...

Share This Page