Hi.

I invoke method from other thread which did not create ui.
Appears to work well...

DVG.Invoke((MethodInvoker)(() => dgv.Rows.Add(DGVR)));

But I need return value from Add method, so I do...

int Index = (int)DVG.Invoke((MethodInvoker)(() => dgv.Rows.Add(DGVR)));

Unfortunately I get an error... "Object reference not set to an instance of an object"
I'm having trouble understanding what that means in this context and how it occurs.

Recommended Answers

All 5 Replies

There are many priors on this and most if not all solutions involve more than a line of code. For my app that starts up many threads and even new instances of the app, I used a message file to get it done. This, for that app was not a problem. That is, I wasn't going to get fancy so a file with the answer was my answer.

OK, priors. Hans and Justin at http://stackoverflow.com/questions/2214002/how-to-get-return-value-when-begininvoke-invoke-is-called-in-c-sharp go over invoke, and how to return a value. As you see, it's more than a few lines of code.

commented: Thanks +8

Duh!

MethodInvoker has void return.

Thanks, I must have postef as you did, sorry if I wasted your time.

No, not at all. I didn't deep dive into MethodInvoker but your question is close enough to a system I wrote in about 2008 using VS2008 C#. While not exactly what you did, it did involve threads and entirely new invocations of the same app. Communication was not that involved so I went with a cheap method. Later I would look into other communication methods but never retrofitted the app since it was working.

I find threading ever so convoluted, but better than employing IPC with my c++ code in some module or other.
It's eating into a lot of the little free time I have to code though. Although I do enjoy tackiling something new to me, If it takes to long to collect info and deal with it, it starts to wind me up.

Thank the gods for daniweb, and its mebers :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.