Replace build-in C# Threading with Win32 API

ChDeal 1 Reputation point
2022-06-21T20:51:30.527+00:00

I have the following exercise to do(no real life example):
Implement a search method that uses a number of threads provided from user to find all letter of a list that start with a string.
My solution:

   public async void Search(string filterString, int numberOfThreads)  
    {   
        int count = _listOfStrings.Count;  
        IEnumerable<IEnumerable<string>> lists = SplitList(_wrapper.Words, count / numberOfThreads);  
        IEnumerable<string>[] results = await Task.WhenAll(lists.Select(list => FilterList(list, filterString)));  
    	  
        List<string> temp = new List<string>();  
      
        foreach (var item in results.ToList())  
        {  
            temp.AddRange(item);  
        }  
      
        Results = new ObservableCollection<string>(temp);   
    }  
      
    public IEnumerable<IEnumerable<string>> SplitList(IEnumerable<string> source, int chunksize)  
    {  
        while (source.Any())  
        {  
            yield return source.Take(chunksize);  
            source = source.Skip(chunksize);  
        }  
    }  
  
private async Task<IEnumerable<string>> FilterList(IEnumerable<string> list, string filterString)  
 {  
            var filteredList = await Task.Run(() => list.Where(x => x.StartsWith(filterString)));  
            return filteredList;  
 }  

Is this a method that runs on mutiple threads or is something blocking the main thread?

For the second part I need to replace the thread functionality with win32 API Methods.
I tried for starters something like this :

        public async void Search(string filterString, int numberOfThreads)  
        {  
            var lists = SplitList(_listOfStrings, numberOfThreads);  
  
            foreach (var list in lists)  
            {  
                FilterDelegate filterDelegate = (list, filterString) =>  
                {  
                    return FilterList(list, filterString);  
                };  
  
                IntPtr filterPtr = Marshal.GetFunctionPointerForDelegate(filterDelegate);  
                int handle = CreateThread(IntPtr.Zero, 0, filterPtr, IntPtr.Zero, 0, 0);  
            }  
        }  
  
        [DllImport("kernel32")]  
        private static extern int CreateThread(  
           IntPtr lpThreadAttributes, UInt32 dwStackSize, IntPtr lpStartAddress,  
           IntPtr param, UInt32 dwCreationFlags, UInt32 lpThreadId  
         );  
  
        private delegate Task<IEnumerable<string>> FilterDelegate(IEnumerable<string> list, string filter);  

But I do not know how to continue. How is it possible to change the Thread functionality with a WIN32 API implementation?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,352 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,933 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 10,706 Reputation points Microsoft Vendor
    2022-06-22T02:17:17.407+00:00

    Hello,

    Welcome to Microsoft Q&A!

    According to CreateThread [in] lpStartAddress, the FilterDelegate should has the same declaration as ThreadProc callback function and you need to allocate separate storage, consolidate the data( list, filterString, results) and pass separate storage address.

    As far as results concerned, you need Synchronization to coordinate multiple threads of execution for example Using Mutex Objects.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments