Question

I have a WCF service lib and client[Winform] that connect using netTCPbinding.
the client called a function Search(string txtSearch,string path) and the server should send back the result, but i don't want the client to wait for results, i need the server to send it back Independently. so whenever the server send back an item, the client should add it to listView1 .

here's a little code of the client application, So how could i need to access listView1 from Callback class if it's possible!

public partial class Form1 : Form
{
    FileManagerClient client;
    public Form1()
    {
        InitializeComponent();
        InstanceContext ctx = new InstanceContext(new Callback());
        client = new FileManagerClient(ctx);
    }
}

class Callback : IFileManagerCallback
{
   public void Folder(_Folder folder)
   {
         ListViewItem item = new ListViewItem();
         item.Text = folder.Name;
         item.ToolTipText = folder.Path;
         item.Tag = item.ImageIndex;
         item.Name = item.Text;
         //Add item to listView1
   }

   public void File(_File file)
   {
         ListViewItem item = new ListViewItem();
         item.Text = file.Name;
         item.ToolTipText = file.Path;
         item.Tag = item.ImageIndex;
         item.Name = item.Text;
         //Add item to listView1
   }
}
Was it helpful?

Solution

Implement event

 public partial class Form1 : Form
{
    FileManagerClient client;
    public Form1()
    {
        InitializeComponent();
        Callback callback = new Callback();
        //Subscribe event for notification
        callback.OnDataReceivedEvent +=new Callback.OnDataReceived(callback_OnDataReceivedEvent);
        InstanceContext ctx = new InstanceContext(callback);
        client = new FileManagerClient(ctx);
    }

    private void callback_OnDataReceivedEvent(object sender, ListViewItem item)
    {
        //Add item here
    }
}

class Callback : IFileManagerCallback
{
    private delegate void OnDataReceived(object sender, ListViewItem item);

    private OnDataReceived _dataReceivedHandler = null;
    public event OnDataReceived OnDataReceivedEvent {
        add { _dataReceivedHandler += value; }
        remove { _dataReceivedHandler -= value; }
    }

    public void Folder(_Folder folder)
    {
        ListViewItem item = new ListViewItem();
        item.Text = folder.Name;
        item.ToolTipText = folder.Path;
        item.Tag = item.ImageIndex;
        item.Name = item.Text;
        RaiseEvents(item);
        //Add item to listView1
    }

    public void File(_File file)
    {
        ListViewItem item = new ListViewItem();
        item.Text = file.Name;
        item.ToolTipText = file.Path;
        item.Tag = item.ImageIndex;
        item.Name = item.Text;
        RaiseEvents(item);
        //Add item to listView1
    }

    private void RaiseEvents(ListViewItem item)
    {
        if (_dataReceivedHandler != null)
        {
            _dataReceivedHandler(this, item);
        }
    }
}

OTHER TIPS

since there's many other thing i would need to access from Form1.cs then i think this's the best way.

Program.cs:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run((MainForm = new Form1()));
    }
    public static Form1 MainForm;
}

and i changed listView1 modifier to Public, so i can access Form1 public items from any class:

   public void Folder(_Folder folder)
    {
        ListViewItem item = new ListViewItem();
        item.Text = folder.Name;
        item.ToolTipText = folder.Path;
        item.Tag = item.ImageIndex;
        item.Name = item.Text;
        item.Group = Program.MainForm.listView1.Groups[1];
        item.ImageIndex = Program.MainForm._iconListManager.AddFolderIcon(folder.Path);
        Program.MainForm.listView1.Items.Add(item);
    }

    public void File(_File file)
    {
        ListViewItem item = new ListViewItem();
        item.Text = file.Name;
        item.ToolTipText = file.Path;
        item.Tag = item.ImageIndex;
        item.Name = item.Text;
        item.SubItems.Add(Program.MainForm.CnvrtUnit(file.Size));
        item.Group = Program.MainForm.listView1.Groups[0];
        item.ImageIndex = Program.MainForm._iconListManager.AddFileIcon(file.Path);
        Program.MainForm.listView1.Items.Add(item);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top