PC Review


Reply
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average.

WinForm and Multi threads

 
 
rockdale
Guest
Posts: n/a
 
      17th Oct 2009
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

 
Reply With Quote
 
 
 
 
Patrice
Guest
Posts: n/a
 
      17th Oct 2009
Hi,

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


See :
http://blogs.msdn.com/adarshk/archiv...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

 
Reply With Quote
 
 
 
 
rockdale
Guest
Posts: n/a
 
      18th Oct 2009
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/archiv.../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


 
Reply With Quote
 
Patrice
Guest
Posts: n/a
 
      18th Oct 2009
Do you mean you are looking for a producer/consumer model such as
http://www.java2s.com/Tutorial/CShar...ndconsumer.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" <(E-Mail Removed)> a écrit dans le message de
news:(E-Mail Removed)...
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


 
Reply With Quote
 
Norman Yuan
Guest
Posts: n/a
 
      18th Oct 2009
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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/archiv.../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


 
Reply With Quote
 
ib.dangelmeyr@googlemail.com
Guest
Posts: n/a
 
      19th Oct 2009

> 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".
 
Reply With Quote
 
rockdale
Guest
Posts: n/a
 
      19th Oct 2009
Guys, thanks for all the reply and I think backgroundworker is what I
am looking for.

Thanks
-Rockdale
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread Local Storage and Managed Threads vs O/S Threads Chris Mullins [MVP] Microsoft Dot NET Framework 5 3rd Mar 2007 10:48 PM
Do-Nothing WinForm App Using 4 Threads? gsimmons Microsoft C# .NET 4 16th Jan 2007 01:50 AM
terminate all threads in winform Tom Microsoft C# .NET 2 15th Sep 2004 02:38 AM
Winform>GUI>hide the frame in winform ? Tom Microsoft C# .NET 3 13th Sep 2004 02:25 PM
Threads and winform Sunny Microsoft C# .NET 5 7th Nov 2003 03:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:20 AM.