Click here to Skip to main content
14,977,979 members
Please Sign up or sign in to vote.
4.20/5 (3 votes)
See more:
Hi guys
I'm new to C# threads so forgive me If you feel my question is very simple.Can we call a function in thread who has parameters in it? Thanks in advance.
Posted
Updated 27-Dec-13 21:58pm
v3

Yes. Exactly as you would from the "normal" UI thread - all code runs on a thread, so it doesn't matter which thread you call a method from: if it returns a string, it returns a string to the caller on the same thread.
C#
private void button1_Click(object sender, EventArgs e)
    {
    BackgroundWorker work = new BackgroundWorker();
    work.DoWork += new DoWorkEventHandler(work_DoWork);
    work.RunWorkerAsync();
    }

void work_DoWork(object sender, DoWorkEventArgs e)
    {
    Console.WriteLine(GetMyString());
    }
string GetMyString()
    {
    return DateTime.Now.ToString();
    }
   
Comments
agent_kruger 28-Dec-13 3:59am
   
can you tell difference between backgroundworker and a thread?
OriginalGriff 28-Dec-13 4:03am
   
Well, they are spelled differently, which is a big clue...:laugh:
agent_kruger 28-Dec-13 4:06am
   
oh, thank you for that precious little peace of information.
agent_kruger 28-Dec-13 4:06am
   
without you i would be stuck in that problem for the whole life :laugh.
OriginalGriff 28-Dec-13 4:20am
   
Glad to oblige! :laugh:
OriginalGriff 28-Dec-13 4:20am
   
So what did you actually want to know?
agent_kruger 28-Dec-13 4:23am
   
difference between backgroundworker and a thread?
OriginalGriff 28-Dec-13 4:50am
   
See here:
http://www.codeproject.com/Articles/628237/Thread-vs-BackgroundWorker
But it misses quite a few details and gets some bits wrong: A Background worker doesn't encapsulate a Thread, it uses the ThreadPool - which means that it is terminated when the application is. A direct Thread has be be closed down by you as it is independent of the application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900