0

Introduction: I have a Server [WCF service library] and Client [Winform], client connects to server using netTcpBinding.

the server job is to share Computer Files to the client with a file manager features { New Folder,Move,Copy,Delete,Properties, Attributes and SEARCH }.

enter image description here

The Problem : the search function is a recursive function, it adds item to client ListView instantly (using client CALLBACK) when it finds a (folder/file) name that contains the search key.
so it was all working perfect, until I added a stop search button, which suppose to allow the user to stop the recursive function _Search(), what happens when I try to stop searching is freezing the GUI and never get back from freezing mode till I "STOP Debugging".
In fact when I set points to see what's wrong with the search function in debugging mode, it works and the search stops.

This's the Code I use for searching:

WCF lib Side:

     [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession)]
  public class MainService : IFileManager,ITaskManager
  {

    IFileManagerCallback callback = OperationContext.Current.GetCallbackChannel<IFileManagerCallback>();

 bool stopSearch = false;

    public void StopSearch()    //client call this function to stop SEARCHING.
    {
        stopSearch = true;
    }

    public void Search(string path, string name)     //client call this function to start searching
    {
        _Search(path, name);
        callback.SearchEnd();
        if (stopSearch)
        {
            callback.InfoLabel("Search Cancelled", InfoState.Info);
            stopSearch = false;
            return;
        }
        callback.InfoLabel("Search Done.", InfoState.Done);
    }

    private void _Search(string path, string name)    //the evil recursive function
    {
        if (stopSearch) return;
        DirectoryInfo Roots = new DirectoryInfo(path);

        foreach (FileInfo file in Roots.GetFiles())
        {
            if (stopSearch) return;
            if (file.Name.IndexOf(name, StringComparison.InvariantCultureIgnoreCase) > -1)
            {
                _File item = new _File();
                item.Name = file.Name;
                item.Size = file.Length;
                item.Path = file.FullName;
                callback.File(item);
            }
        }
        foreach (DirectoryInfo folder in Roots.GetDirectories())
        {
            if (stopSearch) return;
            if (folder.Name.IndexOf(name, StringComparison.InvariantCultureIgnoreCase) > -1)
            {
                _Folder item = new _Folder();
                item.Name = folder.Name;
                item.Path = folder.FullName;
                callback.Folder(item);
            }
            _Search(folder.FullName, name);
        }
    } 
 }

WCF Interface:

  [ServiceContract(CallbackContract = typeof(IFileManagerCallback))]
public interface IFileManager
{
    [OperationContract]
    void StopSearch();
     [OperationContract(IsOneWay = true)]
    void Search(string path, string name);
}

     public interface IFileManagerCallback
{
    [OperationContract]
    void File(_File file);

    [OperationContract]
    void Folder(_Folder folder);

    [OperationContract]
    void InfoLabel(string value, InfoState state);

    [OperationContract]
    void SearchEnd();

}

Client side :

 class Callback : IFileManagerCallback
{
    public delegate void OnFileReceived(object sender, _File item);
    private OnFileReceived _fileReceivedHandler = null;
    public event OnFileReceived OnFileReceivedEvent
    {
        add { _fileReceivedHandler += value; }
        remove { _fileReceivedHandler -= value; }
    }
    private void RaiseFileEvents(_File file)
    {
        if (_fileReceivedHandler != null)
        {
            _fileReceivedHandler(this, file);
        }
    }
    public void File(_File file)
    {
        RaiseFileEvents(file);
    }
    // **I WILL AVOID POSTING Folder event and handler it's the same of the file.**
    public void Folder(_Folder folder)
    {
        RaiseFolderEvents(folder);
    }

Client Form1.cs :

  public partial class Form1 : Form
{

       private void callback_FileReceivedEvent(object sender, _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(CnvrtUnit(file.Size));
        item.Group = listView1.Groups[0];
        item.ImageIndex = _iconListManager.AddFileIcon(file.Path);
        listView1.Items.Add(item);
    }
    bool IsSearch = false;
    private void btnSearch_Click(object sender, EventArgs e)
    {
        if (!IsSearch)
        {
           IsSearch = true;
           listView1.Items.Clear();
           client.Search(currAddress, txtAddress.Text);
           return;
        }
        client.StopSearch();
    }
    public void StopSearching()
    {
        UpdateLabel();    //updating GUI label "Selected 0:,Items: 0"
        IsSearch = false;
    }
}

I'm really confused about fixing it, I'm not sure if I did choose the right title for my question ,so if that's happening because I need an Async callback, how would I convert my search function and Async Callback with WCF?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Murhaf Sousli
  • 12,622
  • 20
  • 119
  • 185
  • Have you tried to set the `stopSearch` to be static? Also, what if you run in debug mode and just wait for the UI to freeze, then pause the execution to see if it is locked somewhere? – Justin Pihony May 05 '12 at 03:25
  • @JustinPihony i sat stopSearch to be STATIC, it still freezing, then i paused the execution , it points to this line `client.StopSearch();` which is in `private void btnSearch_Click` – Murhaf Sousli May 05 '12 at 03:32
  • @JustinPihony It is waiting for WCF service to do the `StopSearch` function – Murhaf Sousli May 05 '12 at 03:34
  • i also paused the WCF service when the client freezed, it points to `callback.File(item);` which is in `_Search()` – Murhaf Sousli May 05 '12 at 03:37
  • The static thing was just a shot in the dark really. However, you are definitely locking in the callbacks. I am curious to see how you set up client. Can you post that code? – Justin Pihony May 05 '12 at 04:16
  • @JustinPihony i tried the one way call for `stopsearch` the same thing.. what code do you want to post exactly? – Murhaf Sousli May 05 '12 at 04:30

2 Answers2

1

I think I know what is happening here, but it is just an educated guess. Since your WCF code is set to be one instance per session and you are in that same session, you are creating a circular lock. Basically, you are hitting a point where callback.File(item) is calling back into your client code at the same time that your client code is calling into StopSearch. So, the client code will not respond until it hears back from StopSearch and the server will not get to the if(stopSearch)/stopSearch = true until it hears back from the client (which is busy waiting for the WCF, which is busy waiting for the client, which is....get the point). Try marking the StopSearch as OneWay so that (I believe, my WCF isnt too strong) the call in the client will immediately return (and not wait for the WCF), thereby unlocking the client.

Alternatively, you could make your WCF code more multithreaded, however you might run into more issues that way.

UPDATE

Based on your response, it does seem that you will need to make either your WCF service call truly async, or make the client code more async (using the TPL or backgroundworkers or just a new thread spun up)

Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • as i told you, it still the same when i used `OneWay` for StopSearch, but it worked when called `client.StopSearch()` into a separate thread. so what can we conclude from this?, can i get it work without initializing a new thread! – Murhaf Sousli May 05 '12 at 05:53
  • @MurHafSoz I have updated my answer, but it does appear that you will need to make your code async – Justin Pihony May 05 '12 at 17:28
  • It works great now, but im not sure if using Thread.Abort is a good idea, check my answer. – Murhaf Sousli May 06 '12 at 06:42
  • Indeed, thread.abort is NOT a good idea and is discouraged extremely. – Ahmed ilyas Dec 08 '13 at 22:00
0

I've found a solution, maybe not a perfect, but i had to use Thread.Abort to stop it.

    public void Search(string path, string name)
    {
        Thread th = new Thread(s => SearchThread(path, name));
        th.Start();
    }

    private void SearchThread(string path, string name)
    {
        try
        {
            _Search(path, name);
        }
        finally
        {
            callback.SearchEnd();
            if (stopSearch)
            {
                callback.InfoLabel("Search Cancelled", InfoState.Info);
                stopSearch = false;
            }
            else
                callback.InfoLabel("Search Done.", InfoState.Done);
        }
    }

i used Thread.Abort() instead if return

private void _Search(string path, string name)
    {
        if (stopSearch) Thread.CurrentThread.Abort();
        DirectoryInfo Roots = new DirectoryInfo(path);

        foreach (FileInfo file in Roots.GetFiles())
        {
            if (stopSearch) Thread.CurrentThread.Abort(); 
            if (file.Name.IndexOf(name, StringComparison.InvariantCultureIgnoreCase) > -1)
            {
                _File item = new _File();
                item.Name = file.Name;
                item.Size = file.Length;
                item.Path = file.FullName;
                callback.File(item);
            }
        }
        foreach (DirectoryInfo folder in Roots.GetDirectories())
        {
            if (stopSearch) Thread.CurrentThread.Abort();
            if (folder.Name.IndexOf(name, StringComparison.InvariantCultureIgnoreCase) > -1)
            {
                _Folder item = new _Folder();
                item.Name = folder.Name;
                item.Path = folder.FullName;
                callback.Folder(item);
            }
            _Search(folder.FullName, name);
        }
    }
Murhaf Sousli
  • 12,622
  • 20
  • 119
  • 185