Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everbody;



my web service code is here;

C#
[WebMethod]
     public List<byte[]> getPicture()
     {
         List<byte[]> img = new List<byte[]>();
         string impath = @"C:\Users\vehbi\Desktop\pictures";
         foreach (string file in Directory.GetFiles(impath))
             img.Add(File.ReadAllBytes(file));

         return img;
     }


my form application code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;


namespace Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

ServiceReference1.Service1SoapClient deneme = new ServiceReference1.Service1SoapClient();


private void Form1_Load(object sender, EventArgs e)
{

}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();

byte[][] img = deneme.getPicture();




flowLayoutPanel1.Controls.Clear();
foreach (byte[] pic in img)
{
MemoryStream ms = new MemoryStream(pic);
PictureBox picbox = new PictureBox();
picbox.Size = new System.Drawing.Size(150, 150);
picbox.Image = Image.FromStream(ms);
picbox.SizeMode = PictureBoxSizeMode.Zoom;
flowLayoutPanel1.Controls.Add(picbox);

}

}


my question is how can i refresh my web service. Server adds a picture per 15 minutes and clients has to see added pictures. without pressing button1
Posted
Comments
Raul Iloc 2-Jul-14 3:32am    
Why did you UnDo the acceptance of my solution?
Member 10902422 2-Jul-14 6:27am    
Don't missunderstand its about me i did not know codeproject.com i'm new for website
Raul Iloc 2-Jul-14 13:13pm    
You should accept a solution if it helps you to solve your problem.
Raul Iloc 3-Jul-14 1:18am    
See my update in my solution below!

1 solution

1.You should use Timer class[^]. So in this way on each 15 minutes (or other value that could be also configurable) the timer action will automatically check, load, then display in your UI the new images from your web service.

2.You should remove the code timer1.Start(); , then in your Form1_Load() event method you have to initialize your Timer object by setting its time interval and more important the timer event, and finally move your code that get the image in this event method like below:
C#
private void Form1_Load(object sender, EventArgs e)
{
        // Create a timer with a 15 minutes interval.
        timer1 = new System.Timers.Timer(900000);
        // Hook up the Elapsed event for the timer.
        timer1.Elapsed += OnTimedEvent;
        timer1.Enabled = true;
}

private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
byte[][] img = deneme.getPicture();
 
flowLayoutPanel1.Controls.Clear();
foreach (byte[] pic in img)
 {
   MemoryStream ms = new MemoryStream(pic);
   PictureBox picbox = new PictureBox();
   picbox.Size = new System.Drawing.Size(150, 150);
   picbox.Image = Image.FromStream(ms);
   picbox.SizeMode = PictureBoxSizeMode.Zoom;
   flowLayoutPanel1.Controls.Add(picbox);
 }
}
 
Share this answer
 
v2
Comments
Member 10902422 2-Jul-14 6:25am    
Where should i use? Web sevice or form application? I tried to use Timer class on form application and i failed before.
Raul Iloc 2-Jul-14 13:06pm    
You have to use the Timer in your form class. You can see example about it in the link from my solution above!

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