473,388 Members | 1,342 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,388 software developers and data experts.

Problem With Threading Using Separate Class Files

I'm writing some sample threading code, and have already got many examples
working that look a lot similar to the ones mentioned here. What I'm seeing
is that most people put their method that will be run as a thread inside the
overall Form's class, which is OK for examples, but not really what you'd
see in real life.

So, I decided to write a class (called BusinessOperation) and have
properties & methods in it. Then, I have 1 method in it called
LongRunningBusinessOp.

So, in my Form UI code I create the thread by instantiating an object of
type BusinessOperation and then using the fpr to the LongRunningBusinessOp
to start my thread.

All works well, except when I want to use delegates to update my UI back
from LongRunningBusinessOp. For whatever reason, by moving the threaded
code outside of the Form, the compiler now says that the method (I call it
UpdateProgressMeter) I point to when creating an instance of the delegate
(not actually defining the delegate), needs to be static. This wasn't the
case before. Problem is, that if I make it static in the Form class, then
it cannot access the controls within the form.

So, what used to work:

public delegate void ProgressDelegate(string strPercentage);
private void LongRunningBusinessOp()
{
// Do code
ProgressDelegate fpr = new ProgressDelegate(UpdateProgressMeter);
// More code then call fpr.Invoke to update the status
}

Now yields a compile error (on the statement after the "//Do code" comment)
when I move the method to outside of the Form class and into it's own class
file. Like I said, all works fine if I just stuff this method and delegate
declaration back into the Form class, but that's not what I want to do.

I know it's probably something simple, like semantics, but sometimes the
obvious ones are the hardest to find when you're the closest to them.

Input would be appreciated.

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/


Nov 16 '05 #1
7 1637
Doug Thews <do*******@removeme.ddconsult.com> wrote:
I'm writing some sample threading code, and have already got many examples
working that look a lot similar to the ones mentioned here. What I'm seeing
is that most people put their method that will be run as a thread inside the
overall Form's class, which is OK for examples, but not really what you'd
see in real life.

So, I decided to write a class (called BusinessOperation) and have
properties & methods in it. Then, I have 1 method in it called
LongRunningBusinessOp.

So, in my Form UI code I create the thread by instantiating an object of
type BusinessOperation and then using the fpr to the LongRunningBusinessOp
to start my thread.

All works well, except when I want to use delegates to update my UI back
from LongRunningBusinessOp. For whatever reason, by moving the threaded
code outside of the Form, the compiler now says that the method (I call it
UpdateProgressMeter) I point to when creating an instance of the delegate
(not actually defining the delegate), needs to be static. This wasn't the
case before. Problem is, that if I make it static in the Form class, then
it cannot access the controls within the form.


I suspect you were trying to use

new ProgressDelegate (MyForm.UpdateProgressMeter) where MyForm is the
name of the class. Instead, what you need is

new ProgressDelegate (someFormInstance.UpdateProgressMeter) where
someFormInstance is a reference to the form you want to update.

When you instantiate BusinessOperation, you need to pass it the form it
"belongs" to so that it knows what to update.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
That sounds very plausible, and it is what I'm doing. This may be a very
stupid question, but how can I get access to the instance of the running
form when I can't pass anything to the thread that's started? I guess I
could make a public property in the BusinessOperation class that holds the
value after I instantiate the object, and then reference it from within the
delegate creation. Is this the "standard" way to do it? How would
something like this actually fix a compiler error? Wouldn't the signatures
be detected the same, thus I'd still get the error about not being a static
method?

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Doug Thews <do*******@removeme.ddconsult.com> wrote:
I'm writing some sample threading code, and have already got many
examples
working that look a lot similar to the ones mentioned here. What I'm
seeing
is that most people put their method that will be run as a thread inside
the
overall Form's class, which is OK for examples, but not really what you'd
see in real life.

So, I decided to write a class (called BusinessOperation) and have
properties & methods in it. Then, I have 1 method in it called
LongRunningBusinessOp.

So, in my Form UI code I create the thread by instantiating an object of
type BusinessOperation and then using the fpr to the
LongRunningBusinessOp
to start my thread.

All works well, except when I want to use delegates to update my UI back
from LongRunningBusinessOp. For whatever reason, by moving the threaded
code outside of the Form, the compiler now says that the method (I call
it
UpdateProgressMeter) I point to when creating an instance of the delegate
(not actually defining the delegate), needs to be static. This wasn't
the
case before. Problem is, that if I make it static in the Form class,
then
it cannot access the controls within the form.


I suspect you were trying to use

new ProgressDelegate (MyForm.UpdateProgressMeter) where MyForm is the
name of the class. Instead, what you need is

new ProgressDelegate (someFormInstance.UpdateProgressMeter) where
someFormInstance is a reference to the form you want to update.

When you instantiate BusinessOperation, you need to pass it the form it
"belongs" to so that it knows what to update.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
BTW ... I did try it and it seemed to work. I saved off a ptr to the form's
class inside my business operation class, and then referenced it within the
create. It's interesting that the method signatures were the same, so I'm
still a little confused on why I got the compiler error (I would've expected
a null reference exception given the problem)

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Doug Thews" <do*******@removeme.ddconsult.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
That sounds very plausible, and it is what I'm doing. This may be a very
stupid question, but how can I get access to the instance of the running
form when I can't pass anything to the thread that's started? I guess I
could make a public property in the BusinessOperation class that holds the
value after I instantiate the object, and then reference it from within
the delegate creation. Is this the "standard" way to do it? How would
something like this actually fix a compiler error? Wouldn't the
signatures be detected the same, thus I'd still get the error about not
being a static method?

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Doug Thews <do*******@removeme.ddconsult.com> wrote:
I'm writing some sample threading code, and have already got many
examples
working that look a lot similar to the ones mentioned here. What I'm
seeing
is that most people put their method that will be run as a thread inside
the
overall Form's class, which is OK for examples, but not really what
you'd
see in real life.

So, I decided to write a class (called BusinessOperation) and have
properties & methods in it. Then, I have 1 method in it called
LongRunningBusinessOp.

So, in my Form UI code I create the thread by instantiating an object of
type BusinessOperation and then using the fpr to the
LongRunningBusinessOp
to start my thread.

All works well, except when I want to use delegates to update my UI back
from LongRunningBusinessOp. For whatever reason, by moving the threaded
code outside of the Form, the compiler now says that the method (I call
it
UpdateProgressMeter) I point to when creating an instance of the
delegate
(not actually defining the delegate), needs to be static. This wasn't
the
case before. Problem is, that if I make it static in the Form class,
then
it cannot access the controls within the form.


I suspect you were trying to use

new ProgressDelegate (MyForm.UpdateProgressMeter) where MyForm is the
name of the class. Instead, what you need is

new ProgressDelegate (someFormInstance.UpdateProgressMeter) where
someFormInstance is a reference to the form you want to update.

When you instantiate BusinessOperation, you need to pass it the form it
"belongs" to so that it knows what to update.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #4
It is not semantics, it is OOPS. Since the method is in another class,
you need to use an object reference to pass its address to the delegate
as below

ProgressDelegate fpr = new ProgressDelegate(ob.UpdateProgressMeter);

where ob is the instance of the class where you have declared the
method.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
Doug Thews <do*******@removeme.ddconsult.com> wrote:
That sounds very plausible, and it is what I'm doing. This may be a very
stupid question, but how can I get access to the instance of the running
form when I can't pass anything to the thread that's started?
See http://www.pobox.com/~skeet/csharp/t...arameters.html
I guess I
could make a public property in the BusinessOperation class that holds the
value after I instantiate the object, and then reference it from within the
delegate creation. Is this the "standard" way to do it?
Well, there are lots of ways of passing parameters to methods, but
effectively you'll need a reference to the appropriate form
*somewhere*.
How would
something like this actually fix a compiler error? Wouldn't the signatures
be detected the same, thus I'd still get the error about not being a static
method?


No, because you wouldn't be trying to access it as if it *were* a
static method.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Doug Thews <do*******@removeme.ddconsult.com> wrote:
BTW ... I did try it and it seemed to work. I saved off a ptr to the form's
class inside my business operation class, and then referenced it within the
create. It's interesting that the method signatures were the same, so I'm
still a little confused on why I got the compiler error (I would've expected
a null reference exception given the problem)


Why would you expect a NullReferenceException, if the reference wasn't
null?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Great. Thanks for the assistance everyone.

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Ravichandran J.V." <jv************@yahoo.com> wrote in message
news:e2**************@TK2MSFTNGP12.phx.gbl...
It is not semantics, it is OOPS. Since the method is in another class,
you need to use an object reference to pass its address to the delegate
as below

ProgressDelegate fpr = new ProgressDelegate(ob.UpdateProgressMeter);

where ob is the instance of the class where you have declared the
method.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #8

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

Similar topics

3
by: Elliot Rodriguez | last post by:
Hi: I am writing a WinForm app that contains a DataGrid control and a StatusBar control. My goal is to update the status bar using events from a separate class, as well as some other simple...
2
by: Don Tucker | last post by:
Hello, I am using Visual Studio 2005 .Net, coding in C#. I am working through the threading walkthrough: ...
1
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C# so I wanted some advice. I am writing an application that communicates with a panel over ethernet, collects data and writes it...
7
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
0
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode...
3
by: Keith Mills | last post by:
Hello, please find attached a basic outline of what I am attempting to accomplish... basically I want to create a number of THREADS (which I can do fine), but I then need a method for them to be...
2
by: hecklar | last post by:
This is my first time posting here, so i apologize if i'm posting in the wrong subgroup or whatever, but here goes... I’m having a problem with threading and events (permissions?) in a VB.net...
9
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my...
4
by: MR | last post by:
I have two C#.Net projects, both of which create instances of the same C# class from a separate assembly; the class creates its own thread. When either project is run alone, it works correctly. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.