Threading

  • Thread starter One Handed Man \( OHM - Terry Burns \)
  • Start date
O

One Handed Man \( OHM - Terry Burns \)

Sorry if this gets duplicated, but I posted it and cant see it for a long
time so repost. . .

I have an application which is writing to a graphics object, the main UI
thread and a worker thread are writing to the same object, whats the best
way to manage this ?


Thanks OHM
--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
 
C

Cor Ligthert

Terry,

The best way is to avoid it and when you absolute cannot than it is synclock
however I suppose you know that so your message should be something else.

Cor

"One Handed Man ( OHM - Terry Burns )"
...
 
O

One Handed Man \( OHM - Terry Burns \)

Thanks.

Ive got around this by creating a new graphics area for the same control,
this way writing to it does not conflict and produces not exceptions,
however, Im not sure if this is safe or not.

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Cor Ligthert said:
Terry,

The best way is to avoid it and when you absolute cannot than it is
synclock however I suppose you know that so your message should be
something else.

Cor

"One Handed Man ( OHM - Terry Burns )"
..
 
C

CJ Taylor

What about using a mutex and using WaitOne?

One Handed Man ( OHM - Terry Burns ) said:
Thanks.

Ive got around this by creating a new graphics area for the same control,
this way writing to it does not conflict and produces not exceptions,
however, Im not sure if this is safe or not.

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
O

One Handed Man \( OHM - Terry Burns \)

Never done that, how does that work ?

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
C

CJ Taylor

I've only used it a few times.

Basically, it is a little more less primitive lock than the SyncLock
mechnism where the WaitOne enters a queue and waits for the other thread to
unlock the variable.

Its defined as this
----
You can use a mutex object to protect a shared resource from simultaneous
access by multiple threads or processes. The state of a mutex object is
either set to signaled, when it is not owned by any thread, or nonsignaled,
when it is owned. Only one thread at a time can own a mutex object. For
example, to prevent two threads from writing to shared memory at the same
time, each thread waits for ownership of a mutex object before executing the
code that accesses the memory. After writing to the shared memory, the
thread releases the mutex object.

----

I could be wrong in my understanding... but seems pretty cut and dry... now
the WaitOne is the part that gets tricky... Because you have to make sure
your reset the thread signal...

Try this out..it's in C# but easy to understand.

http://msdn.microsoft.com/library/d...y/en-us/csref/html/vcwlkthreadingtutorial.asp

HTH,

CJ
 
C

CJ Taylor

I forgot to mention this in my other post.

I read about this kinda thing in Dan Applemans book. (Moving to VB.NET) and
he shows that it requires a lot less CPU time than most other locks. now
you can roll your own apparently, but I wonldn't want to get into that.

Anyways, again, hope this helps in some way..

-CJ
 
O

One Handed Man \( OHM - Terry Burns \)

Thanks for your Time here CJ

I'll let you know how I get on in a few days

Cheers

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
O

One Handed Man \( OHM - Terry Burns \)

Actually, I think I may need to alter my design.

Two objects Invader and Gun have the 'Space' graphcis object reference
passed to them and each tries to write at will to this object, your mutex
wont work in this scenario. I think I need to find a way of accessing a
command sub in the parent form which mutex can then make use of

Or am I missing something ?

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
C

CJ Taylor

Are you working on a collision algortithm?


One Handed Man ( OHM - Terry Burns ) said:
Actually, I think I may need to alter my design.

Two objects Invader and Gun have the 'Space' graphcis object reference
passed to them and each tries to write at will to this object, your mutex
wont work in this scenario. I think I need to find a way of accessing a
command sub in the parent form which mutex can then make use of

Or am I missing something ?

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


CJ Taylor said:
I've only used it a few times.

Basically, it is a little more less primitive lock than the SyncLock
mechnism where the WaitOne enters a queue and waits for the other thread
to
unlock the variable.

Its defined as this
----
You can use a mutex object to protect a shared resource from simultaneous
access by multiple threads or processes. The state of a mutex object is
either set to signaled, when it is not owned by any thread, or
nonsignaled,
when it is owned. Only one thread at a time can own a mutex object. For
example, to prevent two threads from writing to shared memory at the same
time, each thread waits for ownership of a mutex object before executing
the
code that accesses the memory. After writing to the shared memory, the
thread releases the mutex object.

----

I could be wrong in my understanding... but seems pretty cut and dry...
now
the WaitOne is the part that gets tricky... Because you have to make sure
your reset the thread signal...

Try this out..it's in C# but easy to understand.

http://msdn.microsoft.com/library/d...y/en-us/csref/html/vcwlkthreadingtutorial.asp

HTH,

CJ










"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in
message
Never done that, how does that work ?

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
What about using a mutex and using WaitOne?

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in
message
Thanks.

Ive got around this by creating a new graphics area for the same control,
this way writing to it does not conflict and produces not exceptions,
however, Im not sure if this is safe or not.

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Terry,

The best way is to avoid it and when you absolute cannot than it is
synclock however I suppose you know that so your message should be
something else.

Cor

"One Handed Man ( OHM - Terry Burns )"
..
Sorry if this gets duplicated, but I posted it and cant see it for
a
long
time so repost. . .

I have an application which is writing to a graphics object, the main
UI
thread and a worker thread are writing to the same object, whats
the
best
way to manage this ?


Thanks OHM
--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
L

Larry Serflaten

One Handed Man ( OHM - Terry Burns ) said:
Actually, I think I may need to alter my design.

Two objects Invader and Gun have the 'Space' graphcis object reference
passed to them and each tries to write at will to this object, your mutex
wont work in this scenario. I think I need to find a way of accessing a
command sub in the parent form which mutex can then make use of

Or am I missing something ?


Did anyone mention using a delegate to call on a routine in the UI thread?
Basically the form itself would do the drawing, you would call that
routine from your invaders using the form's Invoke method. That invoke
method would handle the cross threading issues.

I also wondered if it was a good idea to have each invader using its own
thread. With 30 - 40 invaders on the screen at the start, I'd think that's a
bit of threading overhead, and may be a drain on the system....

Have fun!
LFS
 
O

One Handed Man \( OHM - Terry Burns \)

LOL, let u know

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
O

One Handed Man \( OHM - Terry Burns \)

Not yet

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


CJ Taylor said:
Are you working on a collision algortithm?


"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in
message
Actually, I think I may need to alter my design.

Two objects Invader and Gun have the 'Space' graphcis object reference
passed to them and each tries to write at will to this object, your mutex
wont work in this scenario. I think I need to find a way of accessing a
command sub in the parent form which mutex can then make use of

Or am I missing something ?

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


CJ Taylor said:
I've only used it a few times.

Basically, it is a little more less primitive lock than the SyncLock
mechnism where the WaitOne enters a queue and waits for the other
thread
to
unlock the variable.

Its defined as this
----
You can use a mutex object to protect a shared resource from simultaneous
access by multiple threads or processes. The state of a mutex object is
either set to signaled, when it is not owned by any thread, or
nonsignaled,
when it is owned. Only one thread at a time can own a mutex object. For
example, to prevent two threads from writing to shared memory at the same
time, each thread waits for ownership of a mutex object before
executing
the
code that accesses the memory. After writing to the shared memory, the
thread releases the mutex object.

----

I could be wrong in my understanding... but seems pretty cut and dry...
now
the WaitOne is the part that gets tricky... Because you have to make sure
your reset the thread signal...

Try this out..it's in C# but easy to understand.

http://msdn.microsoft.com/library/d...y/en-us/csref/html/vcwlkthreadingtutorial.asp

HTH,

CJ










"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in
message
Never done that, how does that work ?

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
What about using a mutex and using WaitOne?

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in
message
Thanks.

Ive got around this by creating a new graphics area for the same
control,
this way writing to it does not conflict and produces not exceptions,
however, Im not sure if this is safe or not.

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Terry,

The best way is to avoid it and when you absolute cannot than it is
synclock however I suppose you know that so your message should
be
something else.

Cor

"One Handed Man ( OHM - Terry Burns )"
..
Sorry if this gets duplicated, but I posted it and cant see it for
a
long
time so repost. . .

I have an application which is writing to a graphics object, the
main
UI
thread and a worker thread are writing to the same object, whats
the
best
way to manage this ?


Thanks OHM
--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char =
"ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
C

CJ Taylor

Yeah, it solves the cross threading issues, but does it solve the variable
lock issues? Given, I don't understand exactly whats going on with it all
but thats what I understand OHM to be doing.

Perhaps we can clarify this a little Terry.

So you have a common graphics object, which you call space. In which case
you have other graphics objects being drawn on (Invaders and guns), but your
worry is that 2 invaders will attempt to draw on the surface at the same
time which could lead to various problems. Am I correct?

-CJ
 
O

One Handed Man \( OHM - Terry Burns \)

Yes CJ, you are absolutlely correct. I'm doing a couple of days contract
work, so I wont be able to get back to this much before this Afternoon


Cheers - OHM

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
O

One Handed Man \( OHM - Terry Burns \)

I tried creating a shared method of a class external to the invader or the
gun, I still however, get the error.

Public Class SpaceDraw

Public Shared Sub DrawImage(ByVal g As Graphics, ByVal i As Image, ByVal p
As Point)

Dim m As New Threading.Mutex

m.WaitOne()

g.DrawImage(i, p)

m.ReleaseMutex()

End Sub





End Class


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
L

Larry Serflaten

CJ Taylor said:
Yeah, it solves the cross threading issues, but does it solve the variable
lock issues? Given, I don't understand exactly whats going on with it all
but thats what I understand OHM to be doing.

Perhaps we can clarify this a little Terry.

I must have missed this the first time through....

Yes, as the docs state, Invoke can be safely called from any thread. That
makes it suitable for what he wants to do, which is, provide a common
routine to use shared resources, called by many different threads.

I provided an example in a later thread.... <g>

LFS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top