C
C#3y ago
itgel9462

❔ Call a method

Hi. Can I call this Main method in another place by not creating an instance.
15 Replies
Thalnos
Thalnos3y ago
only if you make it static
itgel9462
itgel9462OP3y ago
Sorry. I changed it static and tried to call the method. But I got an error.
Thalnos
Thalnos3y ago
try chaning async public to public async and see if it works then
Kiel
Kiel3y ago
make sure your Program class is public too
Angius
Angius3y ago
Don't use new Task() btw
Thalnos
Thalnos3y ago
whats the difference between doing Task task = StreamSample.StreamSampleMain() and Task task = new Task(StreamSample.StreamSampleMain)? if any
khamas
khamas3y ago
Make sure class is public, and use Task.Run
Pobiega
Pobiega3y ago
Task-based asynchronous programming - .NET
In this article, learn about task-based asynchronous programming through the Task Parallel Library (TPL) in .NET.
Pobiega
Pobiega3y ago
its usually recommended to use TaskFactory.StartNew if you need fine-grained control over your task
Thalnos
Thalnos3y ago
but wouldn't we resole the task using await task either way?
Pobiega
Pobiega3y ago
sure
Thalnos
Thalnos3y ago
so what's the diff
Pobiega
Pobiega3y ago
Task task = new Task(...);
task.Start();
await task();
Task task = new Task(...);
task.Start();
await task();
vs
Task task = AsyncStuff();
await task();
Task task = AsyncStuff();
await task();
Thalnos
Thalnos3y ago
oh okay tyvm
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?