[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Threading Helper Class?

Posted on 2007-07-21
10
Medium Priority
?
487 Views
Last Modified: 2012-05-05
I would like to eliminate repeticous code on my main from and thought a seperate 'Threading Helper Class' might be the answer.

Problem: Passing a method name to the ThreadStart.

Here's the code so far:

[Code]
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace myNameSpace
{
    public class ThreadStarter
    {
        Thread t1;

        public void Start(object method)
        {            
            Thread t1 = new Thread(new ThreadStart(method));
            t1.IsBackground = true;
            t1.Priority = ThreadPriority.Normal;
            t1.Start();
        }
    }
}
0
Comment
Question by:kvnsdr
  • 6
  • 3
10 Comments
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19540339
One possibility is to develop an interface, say:
public interface ISupportThreadHelper
{
   void StartThread();
}

Then derive your threaded classes from this interface, and implement that method. Then change to:
public void Start(ISupportThreadHelper object)
        {            
            Thread t1 = new Thread(new ThreadStart(object.StartThread));
            t1.IsBackground = true;
            t1.Priority = ThreadPriority.Normal;
            t1.Start();
        }
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 19540388
Error: 'Object' is a keyword

        public void Start(ISupportThreadHelper object)
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19540406
I should have cought that.

public void Start(ISupportThreadHelper threadObject)
        {            
            Thread t1 = new Thread(new ThreadStart(threadObject.StartThread));
            t1.IsBackground = true;
            t1.Priority = ThreadPriority.Normal;
            t1.Start();
        }


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:kvnsdr
ID: 19540734
Okay, makes sense so far.

Cannot successfully 'call' a method within my form1.

[Code]
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace myNameSpace
{
    public class ThreadStarter
    {
        public ThreadStarter()
        {
            // Constructor / Initialize
        }

        public interface ISupportThreadHelper
        {
            void StartThread();
        }

        public void Start(ISupportThreadHelper threadObject)
        {
            Thread t1 = new Thread(new ThreadStart(threadObject.StartThread));
            t1.IsBackground = true;
            t1.Priority = ThreadPriority.Normal;
            t1.Start();
        }
    }
}

[Form1]
ThreadStarter.Start(LocateMissingFile);

Error      2      Argument '1': cannot convert from 'method group'

Note: For some reason, .Start is not accessable from Form1..

0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19540788
I see several problems.
1. The interface needs to be declared outside the ThreadStarter class - could be in the same cs file, but generally they are declared in separate files, just like a very small class.

2. LocateMissingFile looks like it might be a method in the form class, rather than a different class. If it is a different class, and this is an instance of that class, try casting it to the interface type, as:
ThreadStarter.Start((ISupportThreadHelper)LocateMissingFile);

3. In the same line (ThreadStarter.Start(...) it appears that you are invoking a static method on the ThreadStarter class. Start is not static. So, either change the declaration of Start to:
public static void Start(ISupportThreadHelper threadObject)
Or, in your form class:
ThreadStarter myStarter = new ThreadStarter();
myStarter.Start( nameOfTheObjectYouCreatedThatSupportsISupportThreadHelper )

By the way, the problems you are encountering seem to indicate that you are relatively new to OO programming in general, and C# as well. Threading is a challenging task for a new language. Most of us spent a lot of time learning with simpler tasks before we had to tackle the tough ones. So, in short, it looks like you have set the bar pretty high for yourself.

0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19540798
One more I just noticed. Thread t1; in your ThreadStarter class is going to be overwritten as soon as you invoke another Start from the same instance of ThreadStarter. How do you plan to stop the thread, assuming that's one of your goals?
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 19540930
The general idea is to call a Form1 method from Form1 like so:

ThreadStarter.Start(LocateMissingFile);

I've been designing OO projects for several years now, but this ThreadHelper class is a real mind twister.

I'm not sure it can be done with .NET 2.0
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19541068
My mistake. Threading is a challenge. You can call it like this:
1. Make sure your interface is declared outside the ThreadStarter class.
2 Change the declaration of your Form1 class to look like this:
public class Form1: System.Windows.Forms.Form : IsupportThreadHelper.
3. Add this in Form1:
public void StartThread()
{
   LocateMissingFile();
}

0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19541120
Missed one...
If you made the method static, change to this:
ThreadStarter.Start(this);
Otherwise change to this:
ThreadStarter myStarter = new ThreadStarter();
myStarter.Start(this);
0
 
LVL 19

Accepted Solution

by:
drichards earned 1000 total points
ID: 19543099
Be aware that the IsupportThreadHelper implementation will have two potentially serious limitations:

1) You won't be able to use a static method as the thread routine.  You could add a method to the interface to overcome this.
2) You will be limited to one thread per class since it only calls the StartThread method (or some other fixed number if you added methods to the interface).

and

3) Start should return the thread or else you have no way to get a handle to the new thread.

I'd be inclined to either:

1) Call the class ThreadStarter and give it a static method that looks like:

        public static Thread StartBackground(ThreadStart method)
        {            
            Thread t1 = new Thread(method);
            t1.IsBackground = true;
            t1.Start();
            return t1;
        }

or

2) Call the class MyThread and make it look like:

    public class MyThread
    {
        Thread t1;

        public void StartBackground(ThreadStart method)
        {            
            t1 = new Thread(method);
            t1.IsBackground = true;
            t1.Start();
        }
    }

Either of these would not have the limitations above and would allow easy expansion to handle ParameterizedThreadStart  MyThread is ultimately more flexible, but you may not need that.

Also, if you are interested in getting feedback from your background threads, you should look at BackgroundWorker.  It's new in .NET 2.0.  Might be overkill for what you're doing - I don't know enough details on your problem.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question