Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
hai all please help me am trying to connect my app with openfire server here.
[ignore my button click event]
im having trouble in:


1. Thread.Sleep(2000); //the name thread does not exist in the current context

2.this.Dispatcher.BeginInvoke(() =>//error! windows.ui.core.core dispatcher
//doesnot contains the definition for begin invoke and no extension methode
//'begin invoke accepting a first argument type windows.ui.core.coredispatcher'
{ //coul be found( are you missing a using directive or assembly refference?)


<pre>my code:




using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Net.XMPP;
using System.Threading;


// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641

namespace chat_tral
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{



/// <summary>
/// Xmpp Client
/// </summary>
public XMPPClient ObjXmppClient { get; set; }

/// <summary>
/// XMPP Connection
/// </summary>
public XMPPConnection ObjXmppCon { get; set; }
public string username;
public string password;
public readonly string server = "taurus";
private Boolean IsXmppSuccess { get; set; }

public MainPage()
{
this.InitializeComponent();

this.NavigationCacheMode = NavigationCacheMode.Required;
}



private void IsXmppValid()
{
ObjXmppClient = new XMPPClient();
//initializing the xmpp client with credentials
ObjXmppClient.JID = username + "@" + server;
ObjXmppClient.Password = password ;
ObjXmppClient.Server = server; //user server for emulator and ip address for device.
ObjXmppClient.AutoReconnect = true;
ObjXmppClient.RetrieveRoster = true;
ObjXmppClient.PresenceStatus = new PresenceStatus() { PresenceType = PresenceType.available, IsOnline = true };
ObjXmppClient.AutoAcceptPresenceSubscribe = true;
ObjXmppClient.AttemptReconnectOnBadPing = true;
XMPPConnection ObjXmppCon = new XMPPConnection(ObjXmppClient);
ObjXmppCon.Connect();
ObjXmppClient.Connect();

//initializing the xmpp connection

ObjXmppCon.OnAsyncConnectFinished += ObjXmppCon_OnAsyncConnectFinished;
ObjXmppClient.OnStateChanged += new EventHandler(xMPPClient_OnStateChanged);

Thread.Sleep(2000); //the name thread does not exist in the current context

}

void ObjXmppCon_OnAsyncConnectFinished(xmedianet.socketserver.SocketClient client, bool bSuccess, string strErrors)
{
IsXmppSuccess = client.Connected;
}



void xMPPClient_OnStateChanged(object sender, EventArgs e)
{
switch (ObjXmppClient.XMPPState)
{
case XMPPState.Ready:
if (IsXmppSuccess)
{

this.Dispatcher.BeginInvoke(() =>//error! windows.ui.core.core dispatcher
//doesnot contains the definition for begin invoke and no extension methode
//'begin invoke accepting a first argument type windows.ui.core.coredispatcher'
{ //coul be found( are you missing a using directive or assembly refference?)



NavigationService.Navigate((new Uri("/Output.xaml?key=success", UriKind.Relative)));
//return;
});
}
else
{

this.Dispatcher.BeginInvoke(() =>//error! windows.ui.core.core dispatcher
//doesnot contains the definition for begin invoke and no extension methode
//'begin invoke accepting a first argument type windows.ui.core.coredispatcher'
//coul be found( are you missing a using directive or assembly refference?)
{
MessageBox.Show("Check server name/IpAddress");
//NavigationService.Navigate((new Uri("/Output.xaml?key=failed", UriKind.Relative)));
return;
});
}
break;

case XMPPState.AuthenticationFailed: this.Dispatcher.BeginInvoke(() => //error! windows.ui.core.core dispatcher
//doesnot contains the definition for begin invoke and no extension methode
//'begin invoke accepting a first argument type windows.ui.core.coredispatcher'
//coul be found( are you missing a using directive or assembly refference?)
{
MessageBox.Show("Enter valid username and password");
//NavigationService.Navigate((new Uri("/Output.xaml?key=wrong", UriKind.Relative)));
return;

}); break;
}
}




protected override void OnNavigatedTo(NavigationEventArgs e)
{

}

private void Button_Click(object sender, RoutedEventArgs e)
{




}
}
}&lt;/pre&gt;</pre>
Posted
Updated 24-Sep-15 1:53am
v2

1 solution

This is simple, but it would take long time to explain all the detail, so here is an example for you:

Suppose you have some code like
C#
void MyFunction(A a, B b, C c) {/* ... */}
//...
A aValue = //...
B bValue = //...
C cValue = //...
MyFunction(aValue, bValue, cValue);

How to delegate it to the UI thread with this.Dispatcher.BeginInvoke? You should pass a delegate instance of appropriate delegate type, followed by the set of parameters to be passed to the delegate instance. This is how:
C#
this.Dispatcher.BeginInvoke(new System.Action<A, B, C>((a, b, c)=>{
   // some code using a, b and c
}), aValue, bValue, cValue);

Method BeginInvoke will pass aValue, bValue and cValue — actual parameters — to the invocation of the delegate instance as the parameters a, b and c. Note, that in this form of delegate description, the compiler uses type inference. I suggest you learn how it works and analyze the role of all types (Action, delegate type, delegate instance object type and so on) by yourself.

—SA
 
Share this answer
 
Comments
[no name] 25-Sep-15 0:12am    
thanks a lot...and why my thread.sleep doesnt work? i added using system.thread
Sergey Alexandrovich Kryukov 25-Sep-15 9:08am    
You are welcome.

It always works. The full naming is: System.Threading.Thread.Sleep(/*...*/) (This is a static method; it puts in a wait state the thread calling it.)

Or use "using System.Threading" or, say "using Thread = System.Threading.Thread". It's NOT "using system.thread" (what it would supposed to mean? :-) Read about namespaces; this a very simple topic.

Will you accept the answer formally now?

—SA
[no name] 28-Sep-15 8:09am    
yup..vote of thanks
Sergey Alexandrovich Kryukov 28-Sep-15 11:55am    
You are very welcome.
Good luck, call again.
—SA
[no name] 29-Sep-15 0:45am    
now i am getting this error
Error 1 The type 'System.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. C:\Users\Taurus2\Desktop\sree\xmpp_sample\agsxmpp_demo\agsxmpp_demo\App.xaml.cs 26 33 agsxmpp_demo

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900