| | |
Asynchronous methods - callback method is never called
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 26
Reputation:
Solved Threads: 0
Hi.
I have this console based Asynchronous VS project that works fine.
And then i have a remoting project which also works fine. I want to edit the remoting project so it is non-blocking, using asynchronous calls.
I´ve tried different examples, but I cant get it to work. None of them seems to call my callback method back.
Here is the relevant snippets:
The remote object:
The client which accesses the remote object with asyn call:
Been sitting with this problem for hours, hope you can help!
Thanks alot
I have this console based Asynchronous VS project that works fine.
And then i have a remoting project which also works fine. I want to edit the remoting project so it is non-blocking, using asynchronous calls.
I´ve tried different examples, but I cant get it to work. None of them seems to call my callback method back.
Here is the relevant snippets:
The remote object:
C# Syntax (Toggle Plain Text)
.... public class MultiContainer : MarshalByRefObject, IMultiContainer { public string GetClientsList() { Thread.Sleep(1000); return Program.ServerForm.GetClientsList(); } ....
The client which accesses the remote object with asyn call:
C# Syntax (Toggle Plain Text)
namespace Client { public partial class Form1 : Form { Server.IMultiContainer mc; private delegate string Delegate(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { mc = new Server.MultiContainer(); Delegate del = new Delegate(mc.GetClientsList); AsyncCallback callback = new AsyncCallback(Callback); del.BeginInvoke(callback, null); } private void Callback(IAsyncResult ar) { //this callback method is never called... Delegate del = (Delegate)((AsyncResult)ar).AsyncDelegate; richTextBox1.Text = del.EndInvoke(ar); } ...
Been sitting with this problem for hours, hope you can help!
Thanks alot
0
#2 Oct 7th, 2009
Maybe the delegate never finishes running.... Have you debugged it? This works:
Results in:
The two red lines were output generate from the callback method. The code sample I posted also shows you how to get the exception, if any, that was thrown in your delegate method performing work.
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.Remoting.Messaging; namespace daniweb { public partial class frmAsync : Form { private delegate string Delegate(); public frmAsync() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { List<string> clients = new List<string>(); clients.Add("1"); clients.Add("2"); clients.Add("3"); //This is a valid call new Func<string[], string>(DoWork).BeginInvoke( clients.ToArray(), new AsyncCallback(CleanupCallback), null); //This will throw an exception for demonstration purposes new Func<string[], string>(DoWork).BeginInvoke( default(string[]), new AsyncCallback(CleanupCallback), null); } private static string DoWork(string[] clients) { //clients will be null in one call, resulting in an exception. This is intended Console.WriteLine("Work beginning on " + clients.Length.ToString() + " clients."); System.Threading.Thread.Sleep(2000); return string.Join(", ", clients); } private static void CleanupCallback(IAsyncResult ar) { AsyncResult result = (AsyncResult)ar; Func<string[], string> action = (Func<string[], string>)result.AsyncDelegate; try { string returnValue = action.EndInvoke(ar); Console.WriteLine("finished : " + returnValue); } catch (Exception Ex) { Console.WriteLine("An error occured: " + Ex.Message); } } } }
Results in:
A first chance exception of type 'System.NullReferenceException' occurred in daniweb.exe Work beginning on 3 clients. An error occured: Object reference not set to an instance of an object. A first chance exception of type 'System.NullReferenceException' occurred in mscorlib.dll finished : 1, 2, 3
The two red lines were output generate from the callback method. The code sample I posted also shows you how to get the exception, if any, that was thrown in your delegate method performing work.
•
•
Join Date: Oct 2009
Posts: 26
Reputation:
Solved Threads: 0
0
#3 Oct 7th, 2009
Hi, and thank you for the answer!
Yes i have tried debugging it, and it doesent hang or anything, it just doesent call back.
I´ve tried to insert try catches like this:
The client:
the object:
.. but that returns nothing either.
Am i doing something wrong here?
Thanks alot for your time
Yes i have tried debugging it, and it doesent hang or anything, it just doesent call back.
I´ve tried to insert try catches like this:
The client:
C# Syntax (Toggle Plain Text)
private void button1_Click(object sender, EventArgs e) { try { mc = new Server.MultiContainer(); Delegate del = new Delegate(mc.GetClientsList); AsyncCallback callback = new AsyncCallback(Callback); del.BeginInvoke(callback, null); } catch(Exception e1) { Console.writeline(e1.ToString()); } }
the object:
C# Syntax (Toggle Plain Text)
public string GetClientsList() { try { Thread.Sleep(1000); } catch (Exception e1) { Console.writeline(e1.ToString()); } return Program.ServerForm.GetClientsList(); }
.. but that returns nothing either.
Am i doing something wrong here?
Thanks alot for your time
0
#4 Oct 7th, 2009
Create a new project and move enough of your code over to demonstrate the problem. At a glance your code looks OK. Upload your project here once you have moved the code. To upload you can click on "Go Advanced" then "Manage Attachments"
1
#5 Oct 7th, 2009
Hello.
Isn't this non-stop recursive call to the same function:
Isn't this non-stop recursive call to the same function:
c# Syntax (Toggle Plain Text)
public string GetClientsList() { try { Thread.Sleep(1000); } catch (Exception e1) { Console.writeline(e1.ToString()); } return Program.ServerForm.GetClientsList(); }
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
•
•
Join Date: Oct 2009
Posts: 26
Reputation:
Solved Threads: 0
0
#7 Oct 7th, 2009
Well it never ends in the catch, so I dont think so? 

•
•
•
•
Hello.
Isn't this non-stop recursive call to the same function:
c# Syntax (Toggle Plain Text)
public string GetClientsList() { try { Thread.Sleep(1000); } catch (Exception e1) { Console.writeline(e1.ToString()); } return Program.ServerForm.GetClientsList(); }
0
#8 Oct 7th, 2009
No it was running but the text was failing to set because of illegal cross-threading calls. This works:
[edit]
Main Thread: Invokes delegate on another thread
Delegate Thread: runs delegate, runs callback (cant modify UI ctrls on this thread)
You only modify controls from the UI thread. This is accomplished by calling Control.Invoke() as above.
[/edit]
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.Remoting.Messaging; using System.Text; using System.Windows.Forms; namespace RemoteAsyncTest { public partial class Form1 : Form { private delegate string Delegate(); Server.IMultiContainer mc; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { mc = new Server.MultiContainer(); new Delegate(mc.GetClientsList).BeginInvoke(new AsyncCallback(Callback), null); } private void Callback(IAsyncResult ar) { System.Diagnostics.Debugger.Break(); //proves it is called Delegate del = (Delegate)((AsyncResult)ar).AsyncDelegate; string s = del.EndInvoke(ar); richTextBox1.Invoke(new MethodInvoker( delegate() { richTextBox1.Text = s; } )); } } }
[edit]
Main Thread: Invokes delegate on another thread
Delegate Thread: runs delegate, runs callback (cant modify UI ctrls on this thread)
You only modify controls from the UI thread. This is accomplished by calling Control.Invoke() as above.
[/edit]
Last edited by sknake; Oct 7th, 2009 at 7:05 pm.
0
#10 Oct 8th, 2009
It did complain if you looked at your "Output" window in the IDE. As far as I know exceptions thrown from the thread pool are supressed by the runtime. If you manually create a thread and it raises an unhandled exception it will bring your entire application down -- and you can't stop it. So instead of bringing the application down they handle all exceptions from the threadpool in this case. You can re-raise the exception on the callback to "catch" it instead of wrapping your delegate in a try..catch, plus if you're calling a delegate from someone elses code you need a way to handle it. The framers thought of a brilliant way of using remoting to pass the exception to the callback so you don't have to create delegates to wrap delegates for exception handling 
Please mark this thread as solved if you have found an answer to your question and good luck!

Please mark this thread as solved if you have found an answer to your question and good luck!
![]() |
Similar Threads
- get/set Methods in java (Java)
- Making OnLoad and OnUnload methods in webservice (RSS, Web Services and SOAP)
- Client monitoring - Keylogging problem (C#)
- Javascript and Ajax callback (JavaScript / DHTML / AJAX)
- Need help with adding a page hit counter that uses the Application Bean (JSP)
- calling methods. (Java)
- special keys as inputs (Game Development)
Other Threads in the C# Forum
- Previous Thread: Showing the output in proper format.
- Next Thread: proxy option with in my app
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






