Hi guys following is the windows form code I am using.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using System.Deployment;
using System.Threading;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using System.Windows.Forms;
namespace FORMBROADCASTER
{
public partial class Form1 : Form
{
BackGroundProcess obj = new BackGroundProcess();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
this.obj.backgroundWorker1.RunWorkerAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
if (obj.backgroundWorker1.IsBusy)
{
obj.backgroundWorker1.CancelAsync();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"Error");
}
}
}
}
Now from the above form I am creating the Object of the following Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.ComponentModel;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace FORMBROADCASTER
{
class BackGroundProcess
{
public BackgroundWorker backgroundWorker1 ;
public static long iRecPack = 0;
public static long i7201 = 0;
public static long i7202 = 0;
public static long i7208 = 0;
public static long i6541 = 0;
public static long i7207 = 0;
public static long iOther = 0;
int n;
public static long iSentPack = 0;
public static market_Indices[] mIndices;
public static UdpClient sendBrod = new UdpClient("192.168.201.24", 7864);
public static UdpClient sendBrod1 = new UdpClient("192.168.201.24", 7865);
UdpClient peer ;
IPAddress multicastaddress ;
IPEndPoint ep ;
public BackGroundProcess()
{
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
peer = new UdpClient(34074);
multicastaddress = IPAddress.Parse("233.1.2.5");
ep = null;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
peer.JoinMulticastGroup(multicastaddress);
int j = 0;
Stopwatch stopwatch = new Stopwatch();
while (!backgroundWorker1.CancellationPending)
{
try
{
ep = new IPEndPoint(IPAddress.Any, 34074);
stopwatch.Start();
byte[] data = peer.Receive(ref ep);
stopwatch.Stop();
MWProperties[] mwproc = new MWProperties[2];
int i = processByte(ref data, ref mwproc);
j++;
}
finally
{
}
stopwatch.Reset();
}
peer = null;
}
public static void TwidleBytes(Byte[] b, int fStart, int fSize)
{
Array.Reverse(b, fStart - 1, fSize);
}
public int processByte(ref byte[] byt, ref MWProperties[] newprop)
{
#region "Variable Declaration"
byte[] Ibyt = new byte[byt.Length];
int iLineCount = 0;
String _Detail;
String _Summary = "";
IntPtr p;
Byte[] bytDecompressed;
BcastCmpPacket Bcp = new BcastCmpPacket();
BcastPackData Bpd = new BcastPackData();
#endregion
try
{
Array.Copy(byt, Ibyt, byt.Length);
TwidleBytes(byt, 1, 2);
TwidleBytes(byt, 3, 2);
p = Marshal.AllocHGlobal(Marshal.SizeOf(Bpd));
Marshal.Copy(byt, 0, p, byt.Length);
Bpd = (BcastPackData)Marshal.PtrToStructure(p, Bpd.GetType());
Marshal.FreeHGlobal(p);
byt = Bpd.cPackData;
TwidleBytes(byt, 1, 2);
Bpd = new BcastPackData();
p = Marshal.AllocHGlobal(Marshal.SizeOf(Bcp));
Marshal.Copy(byt, 0, p, byt.Length);
Bcp = (BcastCmpPacket)Marshal.PtrToStructure(p, Bcp.GetType());
if (Bcp.iCompLen > 0)
{
Console.WriteLine(p.ToString());
Marshal.FreeHGlobal(p);
bytDecompressed = new Byte[Bcp.iCompLen];
byt = new Byte[Bcp.iCompLen];
Array.Copy(Bcp.cCompData, byt, Bcp.iCompLen);
int length = LatestLZO.lzo11_decomp(ref byt, out bytDecompressed);
Bcp = new BcastCmpPacket();
byt = new Byte[56];
Array.Copy(bytDecompressed, 8, byt, 0, 56);
my_Message_Header Mh = new my_Message_Header();
TwidleBytes(byt, 9, 4);
TwidleBytes(byt, 37, 2);
TwidleBytes(byt, 39, 2);
p = Marshal.AllocHGlobal(Marshal.SizeOf(Mh));
Marshal.Copy(byt, 0, p, byt.Length);
Mh = (my_Message_Header)Marshal.PtrToStructure(p, Mh.GetType());
Marshal.FreeHGlobal(p);
int StartIndex = 48;
int i = Int16.Parse(bytDecompressed[49].ToString());
Bcast_Header Bh = new Bcast_Header();
if (Mh.TransactionCode == 7208)
{
i7208++;
newprop = new MWProperties[i];
for (int n = 0; n < i; n++)
{
byt = new Byte[212];
Array.Copy(bytDecompressed, 50 + (n * 212), byt, 0, 212);
sendBrod.Send(byt, byt.Length);
}
return 7208;
}
}
else
{
bytDecompressed = new byte[506];
Array.Copy(Bcp.cCompData, bytDecompressed, bytDecompressed.Length);
Bcp = new BcastCmpPacket();
byt = new Byte[56];
Array.Copy(bytDecompressed, 8, byt, 0, 56);
my_Message_Header Mh = new my_Message_Header();
Marshal.FreeHGlobal(p);
TwidleBytes(byt, 9, 4);
TwidleBytes(byt, 37, 2);
TwidleBytes(byt, 39, 2);
p = Marshal.AllocHGlobal(Marshal.SizeOf(Mh));
Marshal.Copy(byt, 0, p, byt.Length);
Mh = (my_Message_Header)Marshal.PtrToStructure(p, Mh.GetType());
Marshal.FreeHGlobal(p);
int StartIndex = 48;
int i = Int16.Parse(bytDecompressed[49].ToString());
Bcast_Header Bh = new Bcast_Header();
if (Mh.TransactionCode == 7207)
{
i7207++;
mIndices = new market_Indices[i];
for (int m = 0; m < i; m++)
{
mIndices[m] = new market_Indices();
Ticker_7207 indices = new Ticker_7207();
byte[] indbyte = new byte[72];
Array.Copy(bytDecompressed, 50 + (m * 72), indbyte, 0, 72);
sendBrod1.Send(indbyte, indbyte.Length);
}
return 7207;
}
}
}
catch (Exception ex)
{
}
finally
{
}
return 0;
}
Now I want that when I click the button in form, backgroundWorker process should start. then Inside
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
I am receiving the broadcast packets from the multicast Ip address.
Then I make a call to the function
public int processByte(ref byte[] byt, ref MWProperties[] newprop)
which processes this bytes and sends it as broadcast packet.
But my application is not behaving properly some time when I click button1 it want start. Sometime it hangs after running some times. Some time it gives me the error
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
And all this things happens mostly in Run mode means when I press ctr + f5. & In debug mode application runs perfactly