1

I'm writing a server-client and want to run operation using Task.Run(() => {SomeCode}); this Is my code

Task.Run(() =>
{
_control_com_peer2peer.VerifyConnection(port: 6001, IpAddressClient: ComboBoxRemoteIP.Text);
});

the inner code is basically

public void VerifyConnection(int port, string IpAddressClient)
{
Control_PeerSessionListCheckCreate(RemoteIP: IpAddressClient);
Domain_Peer2PeerCom.peerSessionList.Find(x => x.remoteIP == IpAddressClient).CommandLocker("Hello");
Domain_Peer2PeerCom.PeerSessionChangeIsConnected(IpAddressClient, true);             
} 

I've break points inside VerifyConnection and none of them fire .

EDIT

entire code for the button firing the Task

private void Connect_Click(object sender, EventArgs e)
{
    string s_RemoteIP;



    if (ComboBoxRemoteIP.Text != "")
    {
        if (Network.startPing(ComboBoxRemoteIP.Text))
        {
            Control_Peer2peerCom.Control_PeerSessionListCheckCreate(ComboBoxRemoteIP.Text);
            //bool IspeerReal = Control_Peer2peerCom.peerSessionListGet(ComboBoxRemoteIP.Text, out peerSessionCurrentParam);
            if (/*IspeerReal&&*/!(Control_Peer2peerCom.Control_PeerSessionCheckIsConnected(ComboBoxRemoteIP.Text)))//peerSessionCurrentParam.IsConnetcted))//(!Peer2peerCom.isPeerConnetionVerifaied)
            {
                SelectiveEnable();
                tbStatus.Text = "Wait...";
                tbStatus.BackColor = Color.Yellow;
                s_RemoteIP = ComboBoxRemoteIP.Text;
                Task.Run(() =>
                {
                 _control_com_peer2peer.VerifyConnection(port: 6001, IpAddressClient: 
                 ComboBoxRemoteIP.Text);
                });
             //more code here , Not related
             }

of course if I run the method out side of the Task.Run it runs fine

what can be the cause of it

11
  • 4
    @KarolMarianSłuszniak Why ? Commented Jul 8, 2014 at 7:50
  • @KarolMarianSłuszniak: Such an advise is useless without any explanation or example how to do it then. Commented Jul 8, 2014 at 7:52
  • 1
    @LordTitiKaka: Try to add a try ... catch inside the Task.Run and check if there is an error. Commented Jul 8, 2014 at 7:53
  • @PatrickHofman did that and yet nothing happens , Should I add more code ? Commented Jul 8, 2014 at 7:57
  • Did you write the error message to Console or MessageBox? Commented Jul 8, 2014 at 7:59

1 Answer 1

1

the problem was in the task.run of course , once I changed to simple thread exception thrown for cross thread ! my mistake was sending ComboBoxRemoteIP.Text to task

the task didnt trow any exception so I want able to catch the problem

the way I've cerrected it was just adding

String Ipddress = ComboBoxRemoteIP.Text

and sending the string instead

Sign up to request clarification or add additional context in comments.

5 Comments

the problem is not with task.run, the problem was you used Taks for a procedural action and didn't check for errors...
@AK_ can you explain how I correctly collect errors from Task ?? I've scatters those bloody monsters all over my code ... :)
msdn.microsoft.com/en-us/library/dd997415(v=vs.110).aspx and msdn.microsoft.com/en-us/library/dd537614(v=vs.110).aspx There are also some good blog posts about this around the internet... In general Tasks are OK for functions that return something, but you should be very careful and think through every possible scenario when your'e using tasks for procedural stuff
or just use a better programming language like F# : tomasp.net/blog/csharp-async-gotchas.aspx the link also has a lot C# async pitfalls...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.