Synchronized dataset

  • Thread starter Thread starter Zürcher See
  • Start date Start date
Z

Zürcher See

I have in a form a datagrid that display a dataset, but if I start a new
thread that fill the dataset the program will crash, that's because the
dataset is not synchronized, is not thread safe.
I tryed to use the monitor.enter(dataset) and monitor.exit(dataset) befor
updating the dataset but was useless.
I think the problem is the paint method of the datagrid, that you can not
override, so you can not synchronize the dataset.
Also there is only one paint event, you need at least two, one befor and one
after painting.

Someone has a suggestion?
 
Zurcher,

The problem is not that the data set is not synchronized, but when you
bind the data set to a grid, then the grid sets up event handlers on the
data set. When you change the data, the events fire, and the grid tries to
update itself. If you want to do anything that affects the state of the UI,
then that call must be made on the UI thread. What you need to do is
marshal the call to the UI thread. In order to do this, create a method
which will change your data. Then create a delegate which matches the
signature of that method. Once you have that, on your thread that is
performing the calcs, call the Invoke method on the grid, passing the
delegate wrapping the method, and any parameters you need to pass.

Hope this helps.
 
Thank's, I'll try.
Can I ask you something else, there is a difference between this two way to
start a new thread (to do not use that from the UI):

System.Threading.ThreadPool.QueueUserWorkItem(new
System.Threading.WaitCallback(this.MyFunction),null);

or

System.Threading.Thread tNewThread=new System.Threading.Thread(new
System.Threading.ThreadStart(this.MyFunction));
tNewThread.Start();

Nicholas Paldino said:
Zurcher,

The problem is not that the data set is not synchronized, but when you
bind the data set to a grid, then the grid sets up event handlers on the
data set. When you change the data, the events fire, and the grid tries to
update itself. If you want to do anything that affects the state of the UI,
then that call must be made on the UI thread. What you need to do is
marshal the call to the UI thread. In order to do this, create a method
which will change your data. Then create a delegate which matches the
signature of that method. Once you have that, on your thread that is
performing the calcs, call the Invoke method on the grid, passing the
delegate wrapping the method, and any parameters you need to pass.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Zürcher See said:
I have in a form a datagrid that display a dataset, but if I start a new
thread that fill the dataset the program will crash, that's because the
dataset is not synchronized, is not thread safe.
I tryed to use the monitor.enter(dataset) and monitor.exit(dataset) befor
updating the dataset but was useless.
I think the problem is the paint method of the datagrid, that you can not
override, so you can not synchronize the dataset.
Also there is only one paint event, you need at least two, one befor and one
after painting.

Someone has a suggestion?
 
Zurcher,

Generally speaking, the ThreadPool should be used, as it eliminates the
overhead of having to create a thread every time you want to perform a
finite task. If you needed a thread which ran in a loop, over and over,
then you would create a new instance of the Thread class. However, if you
just want to perform a finite task asynchronously, then you would use the
ThreadPool class.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Zürcher See said:
Thank's, I'll try.
Can I ask you something else, there is a difference between this two way to
start a new thread (to do not use that from the UI):

System.Threading.ThreadPool.QueueUserWorkItem(new
System.Threading.WaitCallback(this.MyFunction),null);

or

System.Threading.Thread tNewThread=new System.Threading.Thread(new
System.Threading.ThreadStart(this.MyFunction));
tNewThread.Start();

Nicholas Paldino said:
Zurcher,

The problem is not that the data set is not synchronized, but when you
bind the data set to a grid, then the grid sets up event handlers on the
data set. When you change the data, the events fire, and the grid tries to
update itself. If you want to do anything that affects the state of the UI,
then that call must be made on the UI thread. What you need to do is
marshal the call to the UI thread. In order to do this, create a method
which will change your data. Then create a delegate which matches the
signature of that method. Once you have that, on your thread that is
performing the calcs, call the Invoke method on the grid, passing the
delegate wrapping the method, and any parameters you need to pass.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Zürcher See said:
I have in a form a datagrid that display a dataset, but if I start a new
thread that fill the dataset the program will crash, that's because the
dataset is not synchronized, is not thread safe.
I tryed to use the monitor.enter(dataset) and monitor.exit(dataset) befor
updating the dataset but was useless.
I think the problem is the paint method of the datagrid, that you can not
override, so you can not synchronize the dataset.
Also there is only one paint event, you need at least two, one befor
and
one
after painting.

Someone has a suggestion?
 
Thanks for everything

Nicholas Paldino said:
Zurcher,

Generally speaking, the ThreadPool should be used, as it eliminates the
overhead of having to create a thread every time you want to perform a
finite task. If you needed a thread which ran in a loop, over and over,
then you would create a new instance of the Thread class. However, if you
just want to perform a finite task asynchronously, then you would use the
ThreadPool class.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Zürcher See said:
Thank's, I'll try.
Can I ask you something else, there is a difference between this two way to
start a new thread (to do not use that from the UI):

System.Threading.ThreadPool.QueueUserWorkItem(new
System.Threading.WaitCallback(this.MyFunction),null);

or

System.Threading.Thread tNewThread=new System.Threading.Thread(new
System.Threading.ThreadStart(this.MyFunction));
tNewThread.Start();

im Newsbeitrag news:[email protected]...
Zurcher,

The problem is not that the data set is not synchronized, but when you
bind the data set to a grid, then the grid sets up event handlers on the
data set. When you change the data, the events fire, and the grid
tries
to
update itself. If you want to do anything that affects the state of
the
UI,
then that call must be made on the UI thread. What you need to do is
marshal the call to the UI thread. In order to do this, create a method
which will change your data. Then create a delegate which matches the
signature of that method. Once you have that, on your thread that is
performing the calcs, call the Invoke method on the grid, passing the
delegate wrapping the method, and any parameters you need to pass.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have in a form a datagrid that display a dataset, but if I start a new
thread that fill the dataset the program will crash, that's because the
dataset is not synchronized, is not thread safe.
I tryed to use the monitor.enter(dataset) and monitor.exit(dataset) befor
updating the dataset but was useless.
I think the problem is the paint method of the datagrid, that you
can
not
override, so you can not synchronize the dataset.
Also there is only one paint event, you need at least two, one befor and
one
after painting.

Someone has a suggestion?
 
Back
Top