Click here to Skip to main content
16,020,114 members
Home / Discussions / C#
   

C#

 
AnswerRe: How does onActivated works? Pin
Rob Graham13-Jul-06 12:31
Rob Graham13-Jul-06 12:31 
Questionits regarding Multi-threading Pin
Mystic_13-Jul-06 11:49
Mystic_13-Jul-06 11:49 
AnswerUse the class Pin
Ennis Ray Lynch, Jr.13-Jul-06 11:55
Ennis Ray Lynch, Jr.13-Jul-06 11:55 
GeneralRe: Use the class Pin
Mystic_13-Jul-06 12:27
Mystic_13-Jul-06 12:27 
GeneralRe: Use the class Pin
Judah Gabriel Himango13-Jul-06 12:42
sponsorJudah Gabriel Himango13-Jul-06 12:42 
GeneralRe: Use the class Pin
Mystic_13-Jul-06 13:03
Mystic_13-Jul-06 13:03 
GeneralRe: Use the class Pin
Mystic_13-Jul-06 13:10
Mystic_13-Jul-06 13:10 
GeneralRe: Use the class Pin
Jun Du13-Jul-06 14:22
Jun Du13-Jul-06 14:22 
You don't have to use global data. Instaed, you can create a wrapper class which contains your thread function and all other related methods and data members. The only constraint is that all of the methods and data members should be static. The following C++ code snippets are extracted from my CP article http://www.codeproject.com/useritems/xlogoff.asp[^]. My thread function processes lots of data within the class. One thing to note is that I used mutex to protect the data accessed by more than one thread.

Here is the OnStart() in my Windows service code, which simply starts a thread:
void OnStart(String* args[])
{
  // Start the communication thread
  Thread *oThread = new Thread(new ThreadStart(0, &XLogoffThread::ThreadProc));
  oThread->Start();

  // some code omitted...

}


Here is the thread class which contains a thread function as the delegate function to ThreadStart and all other methods and data:
public __gc class XLogoffThread 
{		
  private: static String* prevSession;
  private: static String* DATA_FILE = S"session.dat";
  private: static ArrayList* baseline = new ArrayList;
  public: static Mutex * mut = new Mutex();
  private: static SnippetsCPU::ProcessAsUser *cpau = new SnippetsCPU::ProcessAsUser(); 
  // The ThreadProc method is called when the thread starts. 
 
  public: static void ThreadProc()
  {
    try 
    {
      // Create the session data file
      if ( !File::Exists(DATA_FILE) )
      {
        File::Create(DATA_FILE);
      }

      // Set the server to listen on port 30000
      Int32 port = 30000;
      IPAddress* localAddr = IPAddress::Parse(S"127.0.0.1");
      TcpListener* server = new TcpListener(localAddr, port);
      Byte bytes[] = new Byte[128];

      // Start listening for connections
      server->Start();

      // Enter the listening loop.
      while (true) 
      {
        // Perform a blocking call to accept requests.
        TcpClient* client = server->AcceptTcpClient();            
        String* data = 0;

	// Get a stream for reading and writing
	NetworkStream* stream = client->GetStream();

	// Loop to receive all the data sent by the client.
	do
  	{
	  // Read bytes
	  Int32 i = stream->Read(bytes, 0, bytes->Length);					 
	  // Convert to an ASCII string.
	  data = Text::Encoding::ASCII->GetString(bytes, 0, i);
	  // Process the command
	  ProcessCmd(data);
	} while (stream->DataAvailable);

	// Shutdown and end connection
	client->Close();
      }
    } catch (SocketException* except) 
    {
      MessageBox::Show( except->get_Message(), "XLogoff Service Error" );
    }
  }

  // more code omitted...

}

Hope this helps.

Best,
Jun
GeneralRe: Use the class Pin
Mystic_13-Jul-06 23:19
Mystic_13-Jul-06 23:19 
GeneralRe: Use the class Pin
Jun Du14-Jul-06 3:14
Jun Du14-Jul-06 3:14 
GeneralRe: Use the class Pin
Gavin Roberts14-Jul-06 3:39
Gavin Roberts14-Jul-06 3:39 
GeneralRe: Use the class Pin
Mystic_14-Jul-06 14:36
Mystic_14-Jul-06 14:36 
GeneralRe: Use the class Pin
Mystic_14-Jul-06 14:39
Mystic_14-Jul-06 14:39 
GeneralRe: Use the class Pin
Judah Gabriel Himango14-Jul-06 4:42
sponsorJudah Gabriel Himango14-Jul-06 4:42 
GeneralRe: Use the class Pin
Mystic_14-Jul-06 13:34
Mystic_14-Jul-06 13:34 
QuestionListView Sorting Problem Pin
SteveZWI13-Jul-06 11:44
SteveZWI13-Jul-06 11:44 
AnswerRe: ListView Sorting Problem Pin
Josh Smith13-Jul-06 12:12
Josh Smith13-Jul-06 12:12 
QuestionMulti-Select TreeView? Pin
AngryC13-Jul-06 11:31
AngryC13-Jul-06 11:31 
AnswerRe: Multi-Select TreeView? Pin
Dave Kreskowiak13-Jul-06 14:24
mveDave Kreskowiak13-Jul-06 14:24 
QuestionResponse redirect paramters concatenating Pin
leckey13-Jul-06 10:56
leckey13-Jul-06 10:56 
AnswerRe: Response redirect paramters concatenating Pin
Ennis Ray Lynch, Jr.13-Jul-06 11:02
Ennis Ray Lynch, Jr.13-Jul-06 11:02 
GeneralRe: Response redirect paramters concatenating Pin
leckey13-Jul-06 11:07
leckey13-Jul-06 11:07 
QuestionSending Email with Attachment from Winform App Using Default Client Pin
smcneese13-Jul-06 10:07
smcneese13-Jul-06 10:07 
AnswerIf you don't want to use the API Pin
Ennis Ray Lynch, Jr.13-Jul-06 10:17
Ennis Ray Lynch, Jr.13-Jul-06 10:17 
GeneralRe: If you don't want to use the API Pin
smcneese13-Jul-06 10:38
smcneese13-Jul-06 10:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.