"Simon Hart [MVP]" wrote:
> Firstly your code looks a little odd. I'm not sure if you quickly hacked this
> together to show us a quick sample or what but a couple of things spring to
> mind.
>
> 1. Are you openig and closing Form1?
> 2. You know that calling thread.Abort() will generate an exception?
> 3. Why don't you use lock(obj) instead of Monitor.TryEnter?
> 4. Why is the Thread object static?
> 5. Why this not placed in a business layer.
>
> Sorry for all the questions but it seems like a strange bit of code.
> --
> Simon Hart
> Visual Developer - Device Application Development MVP
>
http://simonrhart.blogspot.com >
>
> "Chris" wrote:
>
> > Once again: there is a pocket pc app acessing data in pocket database. Data
> > stored in this db must be also synchronised with a desktop db.
> > (Synchronization means that data is read from the desktop db and stored in
> > the pocket db).
> > My pocket app has 1 background thread that gets data (by web service) from
> > the desktop db and stores it in the pocket db.
> > On the other hand, user can read (select sql statement) or delete (delete
> > sql statement) data from the pocket db. The problem is (in my opinion) how to
> > handle situation when the background thread is storing data and the user is
> > going to delete it. I think access to the pocket database (its tables) should
> > be done with monitor/mutex/lock keyword.
> > There is some c# code (but I wanted to show you a general idea of the app,
> > not the very code):
> >
> > private static Thread synchroThread;
> > private static object obj=new object();
> >
> > private void Form_Load(object sender, EventArgs e)
> > {
> > synchroThread = new Thread(new ThreadStart(Synchronize));
> > synchroThread.IsBackground = true;
> > synchroThread.Start();
> > }
> >
> > private void Form_Closing(object sender, EventArgs e)
> > {
> > if (synchroThread != null)
> > synchroThread.Abort();
> > }
> >
> > private static void Synchronize()
> > {
> > while (true)
> > {
> > System.Threading.Thread.Sleep(1000 * this.SynchroInterval);
> > if (System.Threading.Monitor.TryEnter(this.obj))
> > {
> > //data is got from the webservice and stored in the pocket db
> > }
> > else
> > MessageBox("cannot synchronize data. sql table in use by other process");
> > }
> > }
> >
> > //on the other hand the user can also synchronze data (synchronzation on
> > demand)
> > private void button_Click(object sender, EventArgs e)
> > {
> > if (System.Threading.Monitor.TryEnter(this.obj))
> > {
> > //sql: delete from table
> > //or
> > //data is got from the webservice and stored in the pocket db
> > }
> > else
> > MessageBox("cannot synchronize data. sql table in use by other process");
> > }
> >
> >
> > In the code above there is Monitor used. It works ok - I mean sometimes I am
> > getting info that: "cannot synchronize data. sql table is use by other
> > process"
> > When I was using Mutex - the method WaitOne always returned true (even if
> > the mutex was locked after I have called WaitOne. What did it happen??)
> >
> > Anyway, now I am using Monitor. But still sometimes my app hangs on section
> > written as:
> > //sql: delete from table
> > //or
> > //data is got from the webservice and stored in the pocket db,
> >
> > and sometimes I am getting error saying (something like that): attempting to
> > read or write protected memory. this usualy means the memory is corrupted.
> >