Custom Event for DataSet Read?

  • Thread starter Thread starter localhost
  • Start date Start date
L

localhost

I have a static, thread-safe class with a DataSet as a property.
When the class is instanced into an object, some callers add rows to a
DataTable in the DataSet. Other callers read the DataSet.

When any caller reads the DataSet, I want the object to delete all of
the rows in the DataTable. I think I want to make a custom event but
I don't know where/how to get started. Help?

Thanks.
 
localhost said:
I have a static, thread-safe class with a DataSet as a property.
When the class is instanced into an object, some callers add rows to a
DataTable in the DataSet. Other callers read the DataSet.

When any caller reads the DataSet, I want the object to delete all of
the rows in the DataTable. I think I want to make a custom event but
I don't know where/how to get started. Help?

If it is up to me, I would hide the dataset property, and make it accessible
only from my own defined methods...

for example:

public class CustomDataSet
{
private System.Data.Dataset _myDataset;


public CustomDataSet()
{
myDataSet = new System.Data.DataSet( );
}

// One example for a public function might be:
public bool FillDataSet(System.Data.DataAdapter adapter)
{
// Do whatever you wish here..
// ....
return false;
}

public DataRow GetRow(int index)
{
DataRow ret = _myDataset.Tables[ 0 ].Rows[ index ];
// Do whatever you wish here, for example:
_myDataset.Tables[ 0 ].Rows.Clear( );
return ret;
}
}

Or you might find it more robust to define class indexers for getting rows
at certain indecies, while you will still be able to control access to your
hidden dataset.
 
That is not quite what I am talking about. Thanks for the response,
though.

I'm not doing anything with DataAdapters or anything like that.

What I need to accomplish is a way for the private DataSet, which
exists as a property on the static class (accessed through a public
"get"), to be recreated each time a caller gets an instance of the
class so each caller gets a new DataSet.

Thanks.



localhost said:
I have a static, thread-safe class with a DataSet as a property.
When the class is instanced into an object, some callers add rows to a
DataTable in the DataSet. Other callers read the DataSet.

When any caller reads the DataSet, I want the object to delete all of
the rows in the DataTable. I think I want to make a custom event but
I don't know where/how to get started. Help?

If it is up to me, I would hide the dataset property, and make it accessible
only from my own defined methods...

for example:

public class CustomDataSet
{
private System.Data.Dataset _myDataset;


public CustomDataSet()
{
myDataSet = new System.Data.DataSet( );
}

// One example for a public function might be:
public bool FillDataSet(System.Data.DataAdapter adapter)
{
// Do whatever you wish here..
// ....
return false;
}

public DataRow GetRow(int index)
{
DataRow ret = _myDataset.Tables[ 0 ].Rows[ index ];
// Do whatever you wish here, for example:
_myDataset.Tables[ 0 ].Rows.Clear( );
return ret;
}
}

Or you might find it more robust to define class indexers for getting rows
at certain indecies, while you will still be able to control access to your
hidden dataset.
 
Hi localhost,

If so, can't you create a new dataset in your property's "get" method? Like
this:

public class yourclass
{
private DataSet _ds;

public DataSet dataset
{
get
{
if(something you want)
{
DataSet ds=new DataSet();
//do something to your new dataset
return ds;
}

}
set
{
_ds=value;
}
}
}

In this solution, your public "dataset" property will create a new dataset
when some condition meets.

=========================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
The problem is that the class is instanced only once (Singleton). If
I just use a classic get/set, then a client could populate the DataSet
property and then crash, and then the next client could populate the
DataSet property and get both client DataSet info.

What I need to do is make sure the DataSet is truly new each time a
client instances the object.

I think I need to rework this into two classes, an event raiser and an
event consumer, so each time a client connects an event is raised and
the DataSet property is set to a new one.


How can I do that?





Hi localhost,

If so, can't you create a new dataset in your property's "get" method? Like
this:

public class yourclass
{
private DataSet _ds;

public DataSet dataset
{
get
{
if(something you want)
{
DataSet ds=new DataSet();
//do something to your new dataset
return ds;
}

}
set
{
_ds=value;
}
}
}

In this solution, your public "dataset" property will create a new dataset
when some condition meets.

=========================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no
rights.
 
Hi localhost,

Thanks very much for your feedback.

Sorry, I think I did not fully understand your point.

Actually, if you use the sample code I provide, whenever you visit the
dataset property, a new dataset will return. Event you use the same class
instance, each time you invoke this property, a new dataset will return, so
I can not find any problem.

Can you show me the problem?

Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
/*

Below is a stripped-down version of the class.
It is named singlestonstuff.cs

The usage pattern is:
1. Callers try to get the single instance of the class.
2. The class sets its DataSet property to new.
3. Caller makes multiple method calls to add info to the
DataSet property.
4. Caller requests the entire populated DataSet.
5. Caller disconnects.

I need to guarantee that every time (1) happens,
(2) also happens. Right now, (1) can happen and then
the caller can exit (from a crash, etc), and then when
a new caller appears they will get the same DataSet
populated by a earlier caller.

Is it possible for you to show how a delegate (with
events) can be hooked into this so the DataSet is
always given fresh for each caller?


Right now my workaround is for each caller to
get an instance of the class and then touch the
DsClear() method. I want DsClear to happen
internally with this class instead - can you
please show how this can be done with an event?

This compiles with
"csc /target:library singletonstuff.cs"

Thanks.

*/


using System;
using System.Data;
using System.Collections;

public sealed class DataSetStuffer
{
// Thread-safe usage without using locks
static readonly DataSetStuffer dssInstance =
new DataSetStuffer();

// Explicit static constructor tells compiler
// not to mark type as BeforeFieldInit
// Thanks to Jon at http://www.yoda.arachsys.com/
static DataSetStuffer()
{
}
DataSetStuffer()
{
PrepDs();
}
public static DataSetStuffer GetInstance()
{
return dssInstance;
}
private DataSet _dataSet = new DataSet();
public DataSet dataSet
{
get
{
DataSet outSet = _dataSet.Copy();
DsClear();
return outSet;
}
}
private void PrepDs()
{
string callerName =
new System.Diagnostics.StackFrame(3).GetMethod().Name;
DataTable dataTable = new DataTable();
dataTable.Columns.Add( "Something" ,
typeof(System.String) );
_dataSet.Tables.Add( dataTable );
}
public void TakeInfo( string clientInfo )
{
_dataSet.Tables[0].Rows.Add
( new Object[] { clientInfo } );
}
public void DsClear()
{
_dataSet = new DataSet();
PrepDs();
}
}
 
Hi localhost,

Thanks very much for your feedback.

I have seen your code, also have seen your problem.

I think your problem may only occur under multithread environment, at the
sequence below:

Thread1: DataSet outSet = _dataSet.Copy();
Thread1: Collapse, or sleep etc..
Thread2: DataSet outSet = _dataSet.Copy();
Thread2: DsClear();
Thread2: return outSet;

Then, thread2 will get the same instance as thread1.

I think the problem is due to the 2 statements below did not executive as
an atom operation
DataSet outSet = _dataSet.Copy();
DsClear();

The solution is make these 2 statements into an atom operation to make
threads access this section synchronously.
In .Net, you can use System.Threading.Mutext class to do synchronization,
like this:

private static Mutex mut = new Mutex();

private DataSet _dataSet = new DataSet();
public DataSet dataSet
{
get
{
mut.WaitOne();
DataSet outSet = _dataSet.Copy();
DsClear();
mut.ReleaseMutex();
return outSet;
}
}

===============================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
That looks good. Yes, this stand-alone DLL will be used by ASP.NET
callers, so it must be fully thread safe and support multithreading.

Is try...finally functionality necessary inside the get?

Thanks.



Hi localhost,

Thanks very much for your feedback.

I have seen your code, also have seen your problem.

I think your problem may only occur under multithread environment, at the
sequence below:

Thread1: DataSet outSet = _dataSet.Copy();
Thread1: Collapse, or sleep etc..
Thread2: DataSet outSet = _dataSet.Copy();
Thread2: DsClear();
Thread2: return outSet;

Then, thread2 will get the same instance as thread1.

I think the problem is due to the 2 statements below did not executive as
an atom operation
DataSet outSet = _dataSet.Copy();
DsClear();

The solution is make these 2 statements into an atom operation to make
threads access this section synchronously.
In .Net, you can use System.Threading.Mutext class to do synchronization,
like this:

private static Mutex mut = new Mutex();

private DataSet _dataSet = new DataSet();
public DataSet dataSet
{
get
{
mut.WaitOne();
DataSet outSet = _dataSet.Copy();
DsClear();
mut.ReleaseMutex();
return outSet;
}
}

===============================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no
rights.
 
Hi localhost,

Thanks very much for your feedback.

I am glad my reply makes sense to you.

Yes, use try...catch in the code is a good habit which enable robust code
in your application.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi localhost,

Do you still have any concern?

Please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top