> All I seem to get are null values in the MyEvent(i)
Sorry - can you clarify what you mean? is MyEvent null? Or is
something throwing an exception?
Note that with BackgroundWorker, a better way to handle the updates is
to set WorkerReportsProgress = true, and then from your loop call
ReportProgress(value). You can then catch the ProgressChanged event
(which is fired on the UI thread, so no InvokeRequired / Invoke) and
get the value from the event-arg.
Marc
> Thanks for the reply - I'm obviously not doing something right;
Correct. :)
> In my form I have:-
>
> delegate void ProgressBarDelegate(int value);
> BackgroundWorker worker = new BackgroundWorker();
>
> TryClass myClass = new TryClass();
So, in your form, you've created an instance of TryClass() and subscribe
your event handler to the event in that instance.
> [...]
> I am then calling the method from another class in the same project;
>
> TryClass x = new TryClass ();
And then within your background worker thread, you create a new instance
of TryClass().
> x.Method();
And then call the method that raises the event using that new instance,
rather than the instance in which you subscribed your handler.
> All I seem to get are null values in the MyEvent(i) - any ideas what I am
> doing wrong? It's like the MyEvent doesn't connect back to the form.
You are creating a new instance of TryClass. That instance is not the
same one to which you subscribed your event handler, so of course the
event in that instance isn't subscribed.
You need to create just one instance of the class, and then use that
instance when executing the method of interest. Alternatively (and this
comes up more often than one might think) if you can genuinely get away
with creating a new instance each time you want to call the method, then
that method shouldn't be an instance method anyway as you're obviously
not using anything that is unique per-instance.
In this alternative case, just make the method and the event static.
And of course, if you have no other instance members in the class, then
the class itself could be static.
Pete
Andy C - 18 Oct 2007 15:00 GMT
> > Thanks for the reply - I'm obviously not doing something right;
>
[quoted text clipped - 42 lines]
>
> Pete
Hi Peter,
Thanks for the reply - I think I'm nearly there now. I made the class static
but I'm still getting nothing but null values.
In the class I have;
public static class TryClass
{
public static event MyEventHandler MyEvent;
public delegate void MyEventHandler(int value);
public static void Method()
{
for (int i = 0; i < 100; i++)
{
System.Threading.Thread.Sleep(200);
if (MyEvent != null)
{ MyEvent(i); }
}
}
In the form I have;
delegate void ProgressBarDelegate(int value);
BackgroundWorker worker = new BackgroundWorker();
private void Method()
{
TryClass.MyEvent += new TryClass.MyEventHandler(TryClass_MyEvent);
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync();
}
void TryClass_MyEvent(int value)
{
if (prgStatus.InvokeRequired)
prgStatus.Invoke(new ProgressBarDelegate(TryClass_MyEvent),
new object[] { value });
else
prgStatus.Value = value / 2;
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
TryClass.Method();
}
void worker_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Done");
}
I'm now calling TryClass.Method(); and I'm still getting nothing but null
values.
Thanks,
Andrew
Peter Duniho - 18 Oct 2007 17:53 GMT
> [...]
> I'm now calling TryClass.Method(); and I'm still getting nothing but null
> values.
Well, maybe I'm just overlooking something, but I didn't see anything
obviously wrong in the code you posted (other than the fact that you
don't unsubscribe from the event after you're done with it, but that
won't affect whether you can be called when the event is raised).
If no one else catches the error, you should post a concise-but-complete
example of code that demonstrates the problem. Assuming I didn't miss
anything, the fact that it's still not working means that there's
something in the code you didn't post that's causing a problem.
Having a concise-but-complete code example would allow anyone to just
compile the thing and test it themselves. Since there's nothing about
what you're trying to do that requires a form, the code you post should
actually be a console application. Yes, this means you need to
basically write a new code sample, copying the important bits from the
code that already exists.
Of course, that assumes the code you posted is exactly the code you're
using. If it's not strictly a copy-and-paste from your program, that's
obviously going to prevent anyone from saying what's wrong with the code
you're actually using. :)
Pete
Andy C - 19 Oct 2007 15:29 GMT
Many thanks - I got it to work. It helps if you actually call the event in
the first place. Doh!