• 0

2 .NET Questions


Question

Using C#,

1) Is there any way to create a thread like this: ThreadStart(Function("abc", 123)) instead of ThreadStart(Function) ?

2) Is there any AND operator for Regex patterns? Using IsMatch, I want to see if the input has all of the words inside the pattern. Right now I use | in the pattern to see if the input has any of the words in the pattern.

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Well, Since threading is done the same in vb.net, I'll have to answer with vb.net :)

for the first one, No.

I'm reading out of this book that you need to create a class for the thread. Then, you need to pass the info to an instance of the class, which will create the thread.

(sorry, I don't know C#, but I assume that you should be able to get what I'm trying to say from this)

Imports System.Threading
Module Module1

    Sub Main()
        Dim x As New TClass
        Dim y As New TClass
        x.StartThread(5)
        y.StartThread(100)
    End Sub

End Module

Class TClass
    Dim v As Thread = New Thread(AddressOf Go)
    Dim mydata As Integer

    Sub StartThread(ByVal data As Integer)
        mydata = data
        v.Start()
    End Sub

    Private Sub Go()
        While mydata <> 0
            System.Console.WriteLine(mydata)
            Thread.CurrentThread.Sleep(500)
            mydata -= 1
        End While
    End Sub

End Class

Link to comment
Share on other sites

  • 0

I couldn't figure out how to create a new thread in c# :p This should be close...i think.....

using System;
using System.Threading;
namespace MyApp
{
	class Class1
	{
  [STAThread]
  static void Main(string[] args)
  {
 	 TClass x = new TClass(100);
 	 TClass y = new TClass(5);
 	 x.StartT();
 	 y.StartT();
  }
	}

	public class TClass        {
  
thread v = new Thread(????????);
    
  int mydata;
  public void StartT()
  {
 	 v.Start();
  }
  public TClass(int i)
  {
 	 mydata = i;
  }

  void xxx()
  {
 	 while (mydata != 0) 
 	 {
    System.Console.WriteLine(mydata);
    mydata -= 1;
 	 }
  }
	}
}

Link to comment
Share on other sites

  • 0

Ya I read that up, but instead of creating a new class I was wondering if there was a way to just call it from ThreadStart to save some time. I'll give the new class a try.

Link to comment
Share on other sites

  • 0

I usually create a private wrapper class inside the class I want to call threads I'd like to pass params to. Like:

public class Foo
{
    private Thread thread;

    private class ThreadClass
    {
        public void Run()
        {
            ...
        }

        public ThreadClass(...)
        {
            ...
        }
    }

    public void SomeFunc()
    {
        thread = new Thread(new ThreadStart(new ThreadClass(...params...).Run));
        thread.Start();
    }
}

Link to comment
Share on other sites

  • 0

Inside the new private class is there any way that I can do stuff with the form? I'm adding things to a listview on the form from a number of sites, and I want to give multithreading a try rather than just one thread.

Link to comment
Share on other sites

  • 0

You may be able to start a Thread using a string method name using Reflection. It's slower than just passing in the address of your method though.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.