मैं बस एक साधारण साझा वर्ग कि मैं इस तरह कॉल कर सकते हैं बनाने के लिए:
If MyConnectionClass.IsConnected then
'Do connected stuff
Else
'Do local save
End If
फिर
मेरी वास्तविक व्यापार वर्गों के सभी/कार्य यूआई कोड से इस गंदगी को छिपाने के लिए इस का उपयोग कर सकते हैं।
MyConnectionClass 'IsConnected संपत्ति कुछ इस तरह होगा:
Public ReadOnly Property IsConnected As Boolean
Get
Try
Dim HostName As String = Dns.GetHostName()
Dim thisHost As IPHostEntry = Dns.GetHostByName(HostName)
Dim thisIpAddr As String = thisHost.AddressList(0).ToString
return (thisIpAddr <> Net.IPAddress.Parse("127.0.0.1").ToString())
Catch ex As Exception
Return False
End Try
End Get
End Property
यह भी सिफारिश की है कि आप एक पृष्ठभूमि धागा का उपयोग कर कनेक्शन स्थिति के लिए मतदान और फिर जब राज्य मुख्य अनुप्रयोग धागा करने के लिए वापस एक ईवेंट सक्रिय परिवर्तन। यहाँ विस्तृत writeup है:
Testing for and responding to network connections in the .NET Compact Framework
संपादित करें:
अब, जीपीआरएस के लिए समर्थन:
आप वेब अनुरोध या वेब सेवा का उपयोग कर रहे हैं, तो ढांचा संभाल लेंगे आपके लिए कनेक्शनआप डाइविंग TCPClient या UDPClient में गहरी हैं, तो आप तो जैसे कनेक्शन प्रबंधक एपीआई के साथ संभाल करने की जरूरत है अपने आप को:
public class GPRSConnection
{
const int S_OK = 0;
const uint CONNMGR_PARAM_GUIDDESTNET = 0x1;
const uint CONNMGR_FLAG_PROXY_HTTP = 0x1;
const uint CONNMGR_PRIORITY_USERINTERACTIVE = 0x08000;
const uint INFINITE = 0xffffffff;
const uint CONNMGR_STATUS_CONNECTED = 0x10;
static Hashtable ht = new Hashtable();
static GPRSConnection()
{
ManualResetEvent mre = new ManualResetEvent(false);
mre.Handle = ConnMgrApiReadyEvent();
mre.WaitOne();
CloseHandle(mre.Handle);
}
~GPRSConnection()
{
ReleaseAll();
}
public static bool Setup(Uri url)
{
return Setup(url.ToString());
}
public static bool Setup(string urlStr)
{
ConnectionInfo ci = new ConnectionInfo();
IntPtr phConnection = IntPtr.Zero;
uint status = 0;
if (ht[urlStr] != null)
return true;
if (ConnMgrMapURL(urlStr, ref ci.guidDestNet, IntPtr.Zero) != S_OK)
return false;
ci.cbSize = (uint) Marshal.SizeOf(ci);
ci.dwParams = CONNMGR_PARAM_GUIDDESTNET;
ci.dwFlags = CONNMGR_FLAG_PROXY_HTTP;
ci.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
ci.bExclusive = 0;
ci.bDisabled = 0;
ci.hWnd = IntPtr.Zero;
ci.uMsg = 0;
ci.lParam = 0;
if (ConnMgrEstablishConnectionSync(ref ci, ref phConnection, INFINITE, ref status) != S_OK &&
status != CONNMGR_STATUS_CONNECTED)
return false;
ht[urlStr] = phConnection;
return true;
}
public static bool Release(Uri url)
{
return Release(url.ToString());
}
public static bool Release(string urlStr)
{
return Release(urlStr, true);
}
private static bool Release(string urlStr, bool removeNode)
{
bool res = true;
IntPtr ph = IntPtr.Zero;
if (ht[urlStr] == null)
return true;
ph = (IntPtr)ht[urlStr];
if (ConnMgrReleaseConnection(ph, 1) != S_OK)
res = false;
CloseHandle(ph);
if (removeNode)
ht.Remove(urlStr);
return res;
}
public static void ReleaseAll()
{
foreach(DictionaryEntry de in ht)
{
Release((string)de.Key, false);
}
ht.Clear();
}
[StructLayout(LayoutKind.Sequential)]
public struct ConnectionInfo
{
public uint cbSize;
public uint dwParams;
public uint dwFlags;
public uint dwPriority;
public int bExclusive;
public int bDisabled;
public Guid guidDestNet;
public IntPtr hWnd;
public uint uMsg;
public uint lParam;
public uint ulMaxCost;
public uint ulMinRcvBw;
public uint ulMaxConnLatency;
}
[DllImport("cellcore.dll")]
private static extern int ConnMgrMapURL(string pwszURL, ref Guid pguid, IntPtr pdwIndex);
[DllImport("cellcore.dll")]
private static extern int ConnMgrEstablishConnectionSync(ref ConnectionInfo ci, ref IntPtr phConnection, uint dwTimeout, ref uint pdwStatus);
[DllImport("cellcore.dll")]
private static extern IntPtr ConnMgrApiReadyEvent();
[DllImport("cellcore.dll")]
private static extern int ConnMgrReleaseConnection(IntPtr hConnection, int bCache);
[DllImport("coredll.dll")]
private static extern int CloseHandle(IntPtr hObject);
}
और यह उपयोग करने के लिए, ऐसा करते हैं:
public void DoTcpConnection()
{
string url = "www.msn.com";
bool res = GPRSConnection.Setup("http://" + url + "/");
if (res)
{
TcpClient tc = new TcpClient(url, 80);
NetworkStream ns = tc.GetStream();
byte[] buf = new byte[100];
ns.Write(buf, 0, 100);
tc.Client.Shutdown(SocketShutdown.Both);
ns.Close();
tc.Close();
MessageBox.Show("Wrote 100 bytes");
}
else
{
MessageBox.Show("Connection establishment failed");
}
}
यह एंथोनी से था वोंग यहाँ ब्लॉग:
Anthony Wong
और याद रखें आप केवल इस निचले स्तर TCP या UDP सामान के लिए की जरूरत है। HTTPRequests की आवश्यकता नहीं है।
मैं इसे कनेक्ट करने का प्रयास करने के लिए कैसे कहूं (यानी यह उपलब्ध होने पर जीपीआरएस शुरू करें)? पोस्ट की गई विधि (और लिंक) ऐसा लगता है कि केवल पता ही वाईफ़ाई जैसा लगता है जहां कनेक्शन या तो चालू या बंद है। – JasonRShaver