-1

What is the problem inside my work, I tried to use a UI Thread.

private void btn_Call_Click(object sender, EventArgs e)
    {
        MakeCall();
    }

private void MakeCall()
    {
        try
        {
            //Get the IP we want to call.
            otherPartyIP = new IPEndPoint(IPAddress.Parse(txtCallToIP.Text), 1450);
            otherPartyEP = (EndPoint)otherPartyIP;

            //Get the vocoder to be used.
            if (cmbCodecs.SelectedText == "A-Law")
            {
                vocoder = Vocoder.ALaw;
            }
               else if (cmbCodecs.SelectedText == "u-Law")
            {
                vocoder = Vocoder.uLaw;
            }
            else if (cmbCodecs.SelectedText == "None")
            {
                vocoder = Vocoder.None;
            }

            //Send an invite message.
            SendMessage(Command.Invite, otherPartyEP);             
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "VoiceChat-Call ()", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }          
    }

That returns the error:

Cross-thread operation not valid: Control ‘btn_Call’ accessed from a thread other than the thread it was created on

Cœur
  • 37,241
  • 25
  • 195
  • 267
usminuru
  • 335
  • 4
  • 8
  • 19

1 Answers1

0

Invoke the line that causes the problem in a call to the dispatcher:

 Application.Current.Dispatcher.Invoke(...

From the error message you posted it's not clear what assignment is causing your issues. Might be one of these:

otherPartyIP = new IPEndPoint(IPAddress.Parse(txtCallToIP.Text), 1450);
otherPartyEP = (EndPoint)otherPartyIP;
vocoder = Vocoder.ALaw;
vocoder = Vocoder.uLaw;
vocoder = Vocoder.None;
flayn
  • 5,272
  • 4
  • 48
  • 69
  • I think the issue is how can I make controls delegate to use UI-Thread? – usminuru Jun 05 '15 at 12:54
  • Florian, can you help me by reading this http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the, post I can't figure out to apply for me. – usminuru Jun 05 '15 at 12:56
  • Making the whole delegate run in the UI thread will block it. I dont know what SendMessage(Command.Invite, otherPartyEP); does, but your UI might get unresponsive when it takes long. I would suggest only to invoke the relevant stuff in the UI thread. – flayn Jun 05 '15 at 13:15
  • Why the downvote btw.? – flayn Jun 05 '15 at 13:15
  • try it by editing my post to solve some problem. – usminuru Jun 05 '15 at 13:28
  • Please post more exception information. – flayn Jun 05 '15 at 13:59
  • no more exception because the only one Exception is generated from: catch (Exception ex) { MessageBox.Show(ex.Message, "VoiceChat-Call ()", MessageBoxButtons.OK, MessageBoxIcon.Error); } – usminuru Jun 05 '15 at 14:04
  • Change that line to MessageBox.Show(ex.ToString(), "VoiceChat-Call ()", MessageBoxButtons.OK, MessageBoxIcon.Error); – flayn Jun 06 '15 at 20:04