473,769 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multithreading problem

Hey

..NET 2.0

I'm developing an application which will perform some webservice calls and I
believe having those calls in a separate thread may help the app run
smoother

No user are waiting for the result of these webservice calls, Each night
this code calls some webservices, which return a result I need to store in
the database (hence I mention "_db" in the context below).

So I'm trying to figure out how to implement this, and have come across a
few issues regarding this:

PROBLEM 1:
This app has an class variable called _db, it holds a reference to the
dataset. When the app is initialized this variable is initialized too.

I created a static method which I try to access this _db variable from, but
I get an compile error because I'm trying to access the _db variable from a
static method.

I believe it's possible to create a local version of _db in the static
method. But then there will exist 2 references to the dataset and might
(AFIAK) result where one main thread reads from the table while another
thread updates the table. (dbms is sql server 2005)

PROBLEM 2:
Do you recommend me using delegate and begininvoke etc.. or create a new
thread each time this will execute - an thread which ends when the
webservice return and database is updated?

any suggestions?

Dec 28 '07 #1
2 1646
On Fri, 28 Dec 2007 01:34:59 -0800, Jeff <do***@spam.mew rote:
[...]
PROBLEM 1:
This app has an class variable called _db, it holds a reference to the
dataset. When the app is initialized this variable is initialized too.

I created a static method which I try to access this _db variable from,
but
I get an compile error because I'm trying to access the _db variable
from a
static method.

I believe it's possible to create a local version of _db in the static
method. But then there will exist 2 references to the dataset and might
(AFIAK) result where one main thread reads from the table while another
thread updates the table. (dbms is sql server 2005)
If that's a risk, then you likely would have to perform synchronization on
any single instance of _db anyway.

The more general issue is: why are you writing a static method that needs
access to instance members of a class? This suggests that somewhere,
there's something broken about the overall design (hopefully something a
minor change can fix).

There are (at least) three different scenarios that might guide the design
of your class:

1) Common data shared among all clients of the class, with no chance
of ever needing to inherit the class

2) Common data shared among all clients of the class, but with some
chance that the class may benefit from serving as the base class for some
other new class

3) Data that is specific to each client of the class

Scenario #1 argues for a plain old static class, with everything being
static. #2 argues for a singleton, with a static accessor to retrieve a
single, shared instance of the class. #3 argues for the usual non-static,
non-singleton class.

You seem to be dealing with a non-static, non-singleton class, so
hopefully there's some reason you want to have multiple instances of the
class. But one way or the other, you need access to your "dataset" (is
this an actual DataSet class instance?). If you see a need to store it in
an instance of the class, then you'll obviously need to have that instance
to get at the variable.

The singleton approach solves this by ensuring there's only ever one
instance, one that all the clients use. Then it's a simple matter of
getting that instance when you need it.

If you really want multiple instances of the class, but only one DataSet
shared by all, it's simple enough to make that DataSet variable static.
Then it would be accessible by all, including any static methods. Note,
however, that doing it this way you would have to synchronize any write
operations with the DataSet (read operations are thread-safe, according to
MSDN).

Finally, it seems to me that you should be able to have multiple DataSet
instances, because SQL server itself is (or should be) multi-connection
safe. As long as each use of the DataSet represents a new connection to
the database, having multiple connections shouldn't hurt (other than
whatever cost there is for a connection), and without you having to do
anything special to synchronize the accesses.

But even in that latter case, it begs the question as to why you want to
access this thing that is normally something per-instance, but from a
static method. Hopefully somewhere in the above possibilities, you'll see
a design that is more appropriate than whatever you've got now that led
you down this dead-end road.
PROBLEM 2:
Do you recommend me using delegate and begininvoke etc.. or create a new
thread each time this will execute - an thread which ends when the
webservice return and database is updated?
I probably wouldn't bother creating a new thread, as the existing thread
pool stuff should handle this sort of thing just fine. BeginInvoke()
called on a delegate instance uses the thread pool automatically, or you
can use BackgroundWorke r, or you can queue a work item directly with the
ThreadPool class. Which works best for you would probably depend on your
specific needs. If it simplifies synchronization to have events
(progress, done) raised from the worker thread, but executed on the thread
that created the worker, then BackgroundWorke r is a nice solution for
that. Otherwise, the other two alternatives would be fine (and IMHO not
terribly different from each other).

Pete
Dec 28 '07 #2
Is this "db" a Linq to sql classes context?
If so, there is no need to hold it. Just create when you need to do your
unit of work and dispose using the "using" pattern.

"Jeff" <do***@spam.mew rote in message
news:Oq******** ********@TK2MSF TNGP03.phx.gbl. ..
Hey

.NET 2.0

I'm developing an application which will perform some webservice calls and
I believe having those calls in a separate thread may help the app run
smoother
Dec 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4269
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
16
8509
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
6
2316
by: Michael C | last post by:
Hello Can someone please tell me what I'm doing wrong? I'm writing an application that should be using callbacks to perform asynchronous calls to the Win32 API. Problem is it never reaches my callback function. Any feedback is appreciated. Also, any references to websites with examples of .NET async multithreading would be appreciated. Thanks in advance. public serverInfo getSession(string servername) {
5
2139
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up when I got two threads going at the same time. What I have is two text boxes (textBox1 and textBox2) and four buttons(cmdStartThread1, cmdStartThread2, cmdStopThread1, cmdStopThread2)
2
3741
by: shonend | last post by:
**** sorry about the length of the message. If you can't read the whole thing and still willing to help, read the last 2 paragraphs where the main problem is described. The introduction story is mentioned to, as much clear as possible, give a picture in what environment the problems rise**** Hello experts! I would appreciate if you can address this problem I have and give a hint what could be wrong.
2
3845
by: Multithreading problem in vb.net | last post by:
Greetings, I am new to multithreading and I am trying to implement it in my app. This application is distributed application which needs to refresh every say 5 secs to show some activities in the datagrid. I have implemented querying the database in a separate thread and and then showing it in the datagrid in the UI thread. It all works fine and the datagrid gets updated every 5 secs. This happens in the desktop (Main form) of the...
4
1571
by: Michael | last post by:
Hi, I am trying to create a multithreaded VB 2005 application which attempts to create a new thread per Domain Controller (DC) in my environment. Each thread connects to its allocated DC and enumerates all computer objects and extracts the 'LastLogon' property. The results from each thread is then consolidated so that I can get the true lastlogon date for each computer object. However in my routine thats get actioned per thread, I have...
0
1261
by: denis.cornehl | last post by:
Hi, I have an unusual Problem with DB2. It is DB2 Version 7 and Fixpack 13 under Windows. We have written an application server which is accessing db2 via c++ and the cli interface. We used IBM Visual Age as our compiler, because we had to support OS/2. Now we ported it this year to windows and the microsoft-compiler (V8).
2
2264
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I divided the complete drawing into 3 parts..1st will be done by main thread and other two are done in these procedures - <1LongTimeTask <2LongTimeTask2 I have invoked the threads using below method. **************
7
16312
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or Exceptional C++, for example). Platform-specific (e.g.: Win32, POSIX) is OK, as long as it's good :) Thank you, Ray
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.