Form threading update control

Status
Niet open voor verdere reacties.

Threesa

Gebruiker
Lid geworden
2 feb 2007
Berichten
5
Hoi,

ik zit met een vervelend probleem. Ik heb een simpel formuliertje met daarop een picturebox.
Nu laat ik via een aparte thread een functie continu lopen die een UDP socket pollt naar een afbeelding. Telkens een afbeeldingen binnen komt, moet deze in de Picturebox van de form (main thread) geplaatst worden, zodat telkens de nieuwste afbeelding zichtbaar is.

Als ik de code volg via 'Step Into Debugging' gebeurt dit ook zonder fouten, maar op de picturebox zelf gebeurt er helemaal niets.

Wie kan me helpen?

[CPP]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
using System.Reflection;

namespace Digital_Signage_EADS
{
public partial class LargeOutput : Form
{

private int interval;
private int last_milliseconds;
private int selected_output;
private string computer_name;

private UdpClient client;

public LargeOutput(int interval, int selected_output, string computer, UdpClient client)
{
InitializeComponent();


this.interval = interval;
last_milliseconds = (int)DateTime.Now.TimeOfDay.Milliseconds;
this.selected_output = selected_output;
this.Text = Screen.AllScreens[selected_output].DeviceName;
this.computer_name = computer;

this.client = client;

try
{
if (computer_name.ToUpper() != Environment.MachineName.ToUpper())
{
Thread t = new Thread(startListening);
t.IsBackground = true;
t.Start();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}/*LargeOutput*/

private void largePictureBox_Click(object sender, EventArgs e)
{

this.Close();

}/*largePictureBox_Click*/

private void largePictureBox_Click_1(object sender, EventArgs e)
{

this.Dispose();
this.Close();

}/*largePictureBox_Click_1*/

public static byte[] Compress(byte[] buffer)
{
MemoryStream ms = new MemoryStream();
GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
zip.Write(buffer, 0, buffer.Length);
zip.Close();
ms.Position = 0;

MemoryStream outStream = new MemoryStream();

byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);

byte[] gzBuffer = new byte[compressed.Length + 4];
Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return gzBuffer;
}/*Compress*/

public static byte[] Decompress(byte[] gzBuffer)
{
MemoryStream ms = new MemoryStream();
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

byte[] buffer = new byte[msgLength];

ms.Position = 0;
GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);
zip.Read(buffer, 0, buffer.Length);

return buffer;
}/*Decompress*/

public void startListening()
{
while (true)
{
try
{
IPAddress[] adressen = Dns.GetHostAddresses("BE00125A");
IPEndPoint point = new IPEndPoint(adressen[0], 7147);
byte[] pic = client.Receive(ref point);
pic = Decompress(pic);
using (MemoryStream ms = new MemoryStream(pic, 0, pic.Length))
{
ms.Write(pic, 0, pic.Length);
System.Drawing.Image im = System.Drawing.Image.FromStream(ms, true);
if (im != null)
{
largePictureBox.Invoke(new SetImageCallBack(this.SetImage), new object[] { im });
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}/*startListening*/

delegate void SetImageCallBack(Bitmap afb);

private void SetImage(Bitmap afb)
{
largePictureBox.Image = afb;
largePictureBox.Refresh();
}/*SetImage*/


}/*LargeOutput : Form*/
}/*Digital_Signage_EADS*/
[/CPP]
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan