473,383 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

BeginInvoke with a property?

Hello everyone,

How do I access a property from another thread in VB'05? Thanks

public class frmblah
inherits system.windows.forms.form

private mCount as integer

private property Count as integer
get
return mCount
end get
set (value as integer)
mCount = value
end set
end property

private sub updatecount
if me.InvokeRequired then
'
'???????
'
else
me.count += 1
end if
end sub

end class
Apr 28 '06 #1
8 2044
Well, I am begininvoking the UpdateCount instead and it appears to be
working.
"AMDRIT" <am****@hotmail.com> wrote in message
news:en**************@TK2MSFTNGP02.phx.gbl...
Hello everyone,

How do I access a property from another thread in VB'05? Thanks

public class frmblah
inherits system.windows.forms.form

private mCount as integer

private property Count as integer
get
return mCount
end get
set (value as integer)
mCount = value
end set
end property

private sub updatecount
if me.InvokeRequired then
'
'???????
'
else
me.count += 1
end if
end sub

end class

Apr 28 '06 #2
This is for .NET not VB Classic, you might want to try a classic VB group
instead

"AMDRIT" <am****@hotmail.com> wrote in message
news:Og****************@TK2MSFTNGP03.phx.gbl...
Well, I am begininvoking the UpdateCount instead and it appears to be
working.
"AMDRIT" <am****@hotmail.com> wrote in message
news:en**************@TK2MSFTNGP02.phx.gbl...
Hello everyone,

How do I access a property from another thread in VB'05? Thanks

public class frmblah
inherits system.windows.forms.form

private mCount as integer

private property Count as integer
get
return mCount
end get
set (value as integer)
mCount = value
end set
end property

private sub updatecount
if me.InvokeRequired then
'
'???????
'
else
me.count += 1
end if
end sub

end class


Apr 28 '06 #3
On 2006-04-28, AMDRIT <am****@hotmail.com> wrote:
Hello everyone,

How do I access a property from another thread in VB'05? Thanks

public class frmblah
inherits system.windows.forms.form

private mCount as integer

private property Count as integer
get
return mCount
end get
set (value as integer)
mCount = value
end set
end property

private sub updatecount
if me.InvokeRequired then
'
'???????
'
else
me.count += 1
end if
end sub

end class
You don't need to worry about InvokeRequired here, that's for accessing
UI components on the UI thread. For ordinary properties, even if they
are properties of a Form class, you can just set them from any thread
(watching out for thread contention of course).


Apr 28 '06 #4
David,

Thanks for the response. The actual issue I am working through is a splash
screen.

I have a component library that has a base form and a splash screen form.
All my application forms derive from the custom form, which now has two
additional methods, (ShowWait, HideWait). These methods will be used
primarly for long running processes called by the custom form, however, the
derived form could make use of it as well.

In VB'03 is was working great, my problem is in VB'05. The splash screen is
supposed to fade in and out over time while it is displayed. I encountered
two issues from the conversion.

1. I do not have access to doevents. Since control libraries do not have
access to the My namespace, I have been exploring
Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase. I
haven't found a way to associate my ExecutingAssembly with my library.
Threading.Thread.Sleep doesn't seem to do the job, of cource that could be a
result of my second issue.

2. Accessing the splash screens form methods and properties cause:
a. System.Threading.ThreadAbortException
b. System.ArgumentException
c. System.InvalidOperationException
d. System.Transactions Critical at
System.Windows.Forms.Control.SetVisibleCore(Boolea n value)

What I have going on is a do...while loop that is calling the fade routine
when the splash screen is visible.

public sub DoFading
do while not done
call Fade
loop

me.hide()
me.close()

end sub

private sub Fade

dim fadedirection as fadetypes

if me.opacity <= 0 then
fadedirection = fadetypes.up
elseif me.opacity >= 1 then
fadedirection = fadetypes.down
end if

select case fadedirection
case fadetypes.up
me.opacity += pintStepping
case else
me.opacity -= pintStepping
end select

end sub

I changed my call to fade to Me.Invoke(New
Windows.Forms.MethodInvoker(AddressOf Fade))

as well as the logic to hide and close the form

I don't know what I am doing wrong, but it is not behaving like it used to.

"david" <da***@woofix.local.dom> wrote in message
news:sl******************@localhost.localdomain...
On 2006-04-28, AMDRIT <am****@hotmail.com> wrote:
Hello everyone,

How do I access a property from another thread in VB'05? Thanks

public class frmblah
inherits system.windows.forms.form

private mCount as integer

private property Count as integer
get
return mCount
end get
set (value as integer)
mCount = value
end set
end property

private sub updatecount
if me.InvokeRequired then
'
'???????
'
else
me.count += 1
end if
end sub

end class


You don't need to worry about InvokeRequired here, that's for accessing
UI components on the UI thread. For ordinary properties, even if they
are properties of a Form class, you can just set them from any thread
(watching out for thread contention of course).


Apr 28 '06 #5
"Brian Henry" <no****@nospam.com> schrieb:
This is for .NET not VB Classic, you might want to try a classic VB group
instead


Mhm... The question is clearly related to VB.NET.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Apr 28 '06 #6
On 2006-04-28, AMDRIT <am****@hotmail.com> wrote:
David,

Thanks for the response. The actual issue I am working through is a splash
screen.

I have a component library that has a base form and a splash screen form.
All my application forms derive from the custom form, which now has two
additional methods, (ShowWait, HideWait). These methods will be used
primarly for long running processes called by the custom form, however, the
derived form could make use of it as well.

In VB'03 is was working great, my problem is in VB'05. The splash screen is
supposed to fade in and out over time while it is displayed. I encountered
two issues from the conversion.
VB05 is a bit more persnickety about calling UI components from the
wrong thread.
1. I do not have access to doevents. Since control libraries do not have
access to the My namespace, I have been exploring
Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase. I
haven't found a way to associate my ExecutingAssembly with my library.
I'm not sure why you need doEvents here, but it can be found at
System.Windows.Forms.Application.DoEvents().

Of course, it needs to be called from the UI thread.

Threading.Thread.Sleep doesn't seem to do the job, of cource that could be a
result of my second issue.

2. Accessing the splash screens form methods and properties cause:
a. System.Threading.ThreadAbortException
b. System.ArgumentException
c. System.InvalidOperationException
d. System.Transactions Critical at
System.Windows.Forms.Control.SetVisibleCore(Boolea n value)

What I have going on is a do...while loop that is calling the fade routine
when the splash screen is visible.
Seems like a timer would be more appropriate. In fact, the Forms.Timer
component will even handle calling you on the correct thread.
public sub DoFading
do while not done
call Fade
loop

me.hide()
me.close()

end sub

private sub Fade

dim fadedirection as fadetypes

if me.opacity <= 0 then
fadedirection = fadetypes.up
elseif me.opacity >= 1 then
fadedirection = fadetypes.down
end if

select case fadedirection
case fadetypes.up
me.opacity += pintStepping
case else
me.opacity -= pintStepping
end select

end sub

I changed my call to fade to Me.Invoke(New
Windows.Forms.MethodInvoker(AddressOf Fade))

as well as the logic to hide and close the form

I don't know what I am doing wrong, but it is not behaving like it used to.
It's hard to tell from this snippet. I'm not even sure the threading is
the error here is from the UI thread issues (usually you get an
InvalidOperationException when that happens). One trick I like to do
for these types of problems is have the form methods handle their own
invoke requirements, like...
Private Sub Fade()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf Fade))
Exit Sub
End If
' Do the real work



"david" <da***@woofix.local.dom> wrote in message
news:sl******************@localhost.localdomain...
On 2006-04-28, AMDRIT <am****@hotmail.com> wrote:
Hello everyone,

How do I access a property from another thread in VB'05? Thanks

public class frmblah
inherits system.windows.forms.form

private mCount as integer

private property Count as integer
get
return mCount
end get
set (value as integer)
mCount = value
end set
end property

private sub updatecount
if me.InvokeRequired then
'
'???????
'
else
me.count += 1
end if
end sub

end class


You don't need to worry about InvokeRequired here, that's for accessing
UI components on the UI thread. For ordinary properties, even if they
are properties of a Form class, you can just set them from any thread
(watching out for thread contention of course).



Apr 29 '06 #7
On Fri, 28 Apr 2006 16:34:12 -0500, "AMDRIT" <am****@hotmail.com> wrote:
1. I do not have access to doevents.
You don't need it if your fade code is running on a thread you've created.

2. Accessing the splash screens form methods and properties cause:
a. System.Threading.ThreadAbortException
b. System.ArgumentException
c. System.InvalidOperationException
d. System.Transactions Critical at
System.Windows.Forms.Control.SetVisibleCore(Boole an value)

What I have going on is a do...while loop that is calling the fade routine
when the splash screen is visible.

public sub DoFading
do while not done
Me.Invoke(New Windows.Forms.MethodInvoker(AddressOf Fade))
loop

me.hide()
me.close()

end sub


If you look at that code (assuming you've wrapped that call to Fade() inside of an invoke like you described) you will
see that your calls to hide() and close() are not using a MethodInvoker. These calls need to be executed from the
controls thread and will need to be invoked as well:

public sub DoFading
MethodInvoker fader = New MethodInvoker(AddressOf Fade)

do while not done
Me.Invoke(fader)
'Really should think about a sleep here so as not to eat cpu time
loop

Me.Invoke(New MethodInvoker(AddressOf Close())
end sub

This is making the assumption you have launched DoFading on a new thread, which it seems like you have done.
Apr 29 '06 #8
Thanks David and Chris, everything appears to be right again.

"Chris Chilvers" <ke****@dynafus.com> wrote in message
news:ov********************************@4ax.com...
On Fri, 28 Apr 2006 16:34:12 -0500, "AMDRIT" <am****@hotmail.com> wrote:
1. I do not have access to doevents.


You don't need it if your fade code is running on a thread you've created.

2. Accessing the splash screens form methods and properties cause:
a. System.Threading.ThreadAbortException
b. System.ArgumentException
c. System.InvalidOperationException
d. System.Transactions Critical at
System.Windows.Forms.Control.SetVisibleCore(Bool ean value)

What I have going on is a do...while loop that is calling the fade routine
when the splash screen is visible.

public sub DoFading
do while not done
Me.Invoke(New Windows.Forms.MethodInvoker(AddressOf Fade))
loop

me.hide()
me.close()

end sub


If you look at that code (assuming you've wrapped that call to Fade()
inside of an invoke like you described) you will
see that your calls to hide() and close() are not using a MethodInvoker.
These calls need to be executed from the
controls thread and will need to be invoked as well:

public sub DoFading
MethodInvoker fader = New MethodInvoker(AddressOf Fade)

do while not done
Me.Invoke(fader)
'Really should think about a sleep here so as not to eat cpu time
loop

Me.Invoke(New MethodInvoker(AddressOf Close())
end sub

This is making the assumption you have launched DoFading on a new thread,
which it seems like you have done.

May 1 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gomaw Beoyr | last post by:
Hello The book "C# Black Book" chapter about Threads says that a "background thread" cannot communicate directly with a visual element, e.g. a label, and thus has to use the BeginInvoke method,...
6
by: arkam | last post by:
Hi, I found a sample on internet : formMain.BeginInvoke(new MyClickHandler(this.OnMyClick)); I would like to do the same but in a class library where there is no forms ! Where can I find...
9
by: David Sworder | last post by:
Hi, I have a form that displays data (is that vague enough for you?). The data comes in on a thread-pool thread. Since the thread pool thread is not the same as the UI thread, the callback...
6
by: Valerie Hough | last post by:
I'm not entirely sure what the difference is between these two approaches. In order to avoid reentrant code, I was using Control.BeginInvoke in my UI to cause an asynchronous activity to be done...
9
by: john doe | last post by:
I have a question about BeginInvoke method found on the Control class. To quote the docs: "Executes the specified delegate asynchronously with the specified arguments, on the thread that the...
3
by: RWF | last post by:
I have a windows form of which I will be saving the user's selections from DropDownBoxes, CheckBoxes, and RadioButtons. Once the choices are collected, I really will have no use for any of the...
5
by: GT | last post by:
Could someone please explain the difference of (refer to example code below) d.BeginInvoke("some text", null,null); //Alternative A and BeginInvoke( d, new object { "some text" } ...
2
by: Flack | last post by:
Hello, If I understand BeginInvoke correctly, when it is called your delegate is run on a thread pool thread. Now, if you supplied a callback delegate, that too is called on the same thread...
2
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small...
7
by: Ben Voigt [C++ MVP] | last post by:
As much as the CLR team assures us that it's ok to fire-and-forget Control.BeginInvoke, it seems it isn't. Maybe this is a bug. See for example: the comments in...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.