Question

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?

Was it helpful?

Solution

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)

OTHER TIPS

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);
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top