Thread for interrupt

  • Thread starter Thread starter Viki
  • Start date Start date
V

Viki

Hello all

I have a operation, which must be able to interrupt. I did a Thread ( form
with button waiting for click).
If I start it from basic form (click on button) or from menu, it is ok.
But if I start it from ContextMenu, it isn't started Thread-form (all is
skipped).

Is it any difference between using it in ContextMenu and in Menu?

Viki
 
Personally I do not understand you question. Could you please rephrase it
and provide some sample code of what is going wrong? How are you showing the
ContextMenu?

Cheers
Daniel
 
Not sure what all those Application.DoEvents are doing and I also cannot run
anything because it is an incomplete sample (just two standalone files) ,
but your code answers the question of where the ContextMenu is shown; it is
shown from a button click.

There are differences with manually showing contextmenus compared to normal
menus e.g. look here:
http://www.danielmoth.com/Blog/2004/11/contextmenushow.html

So, completely shooting in the dark why don't you try this code:
In Button3_Click Instead of :
ContextMenu1.Show(Me, New Point(10, 10))

Put this line:
System.Threading.ThreadPool.QueueUserWorkItem(New
Threading.WaitCallback(AddressOf PopItUp))

Add these two methods to the same form:
Private Sub PopItUp(ByVal s As Object)
Me.Invoke(New EventHandler(AddressOf RealPopItUp))
End Sub

Private Sub RealPopItUp(ByVal s As Object, ByVal e As EventArgs)
ContextMenu1.Show(Me, New Point(10, 10))
End Sub

Cheers
Daniel
 
Daniel,

I tried to use your changes, but it is the same. Calling "frm_interr=new
Form_interr" isn't used.
Viki
 
Previously you said:
The sample you attached does not work at all regardless of which path we
follow (button, menu, contextmenu). frm_interr is never created so it will
always throw a nullreferenceexception (you are not giving the thread time to
start, so the InterruptAction is not being called before the RunninAction).

So a *quick* fix to that is to add a this line before
frm_action.RunningAction in the StartAction method:
System.Threading.Thread.Sleep(1000)

However now I notice you are ShowDialog from a thread and then try to touch
the form from the primary thread (via the StartAction). You should not do
that. Redesign your app so only the main thread accesses all UI elements.

If you describe what it is you are trying to do we can give you more
complete advice. At this point my reccomendation is to throw away the
running_prepare.vb and start again.

Cheers
Daniel
 
It is strange. I try the same and it is running for menu and button
(frm_interr is setting), only for contextmenu is frm_interr = nothing during
a Thread.

If you tried all 3 eventuality (menu, button, contextmenu), it should notify
of error only for contextmenu.

I tried to use Sleep, but it doesn't help.

What I want to do:
to run action A and can interrupt action A at any time (-> set
IsInterrupt=true -> action A is finished )
if action A is finished alone, automatically close form_interr

In my example there is action A fictional procedure, to delay only.now. In
future it will be more complicated.

I try it on PPC2003 and on WCE4.2 and I have problem only for contextmenu.

Thanks

Viki
 
Not only does it fail 100% on all methods over here but looking at the code,
as I said, I am not surprised.

If this code you wrote is fictitious and you simply want to set something
running and be able to cancel it (or let it finish whenever) I suggest
looking at the BackgroundWorker. Please read the documentation for it and
study the sample:
http://www.danielmoth.com/Blog/2004/12/backgroundworker-for-cf-10.html
http://www.danielmoth.com/Blog/2004/12/backgroundworker-sample.html

If you want to explicitly use a thread, check this out:
http://www.danielmoth.com/Blog/2004/11/dont-poll.html

And remember what I wrote previously (which is the major flaw of your code
as it stands), you cannot touch UI elements from other than the thread they
were created on. You must use Control.Invoke:
http://www.danielmoth.com/Blog/2004/10/invoke-cf-and-full-fx.html

Cheers
Daniel
 
Viki

Your usage of opennetcf had nothing to do with it.

The fact that with a thread.sleep you can see it work sometimes with
button/menu but never with contextmenu has nothing to do with it.

Please read the following 5 statements carefully:

1. You are creating and starting a thread.
2. The thread points to a method
3. In that method you are creating and showing a form.
4. From your main thread you are then trying to access the form you created
and showed from the thread.
5. This is not supported._

Feel free to post back with a sample that doesn't violate the unsupported
scenario.

Cheers
Daniel
 
Back
Top